OrePAN2/Repository/Cache.pm

filter: Statementstmt 75.9% 63 / 83 Branchbran 25.0% 5 / 20 Conditioncond 7.1% 1 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 89.4% 17 / 19 Podpod 40.0% 2 / 5 totaltotal 59.0% 88 / 149 CC 16 SCAR 35.1
CC
16
Cov
59%
is_hit
110.0
set
3.1
new
3.1
CRAP 33.6
↑ Summary OrePAN2/Injector.pm »
1 package OrePAN2::Repository::Cache;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use English q(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::stat qw( stat );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::File::AtomicChange ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use parent qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 __PACKAGE__->mk_accessors(
20 qw(
21 directory
22 data
23 filename
24 is_dirty
25 )
26 );
27  
28 2 our $VERSION = '2.0.0';
29  
30 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
31 2 my ( $class, @args ) = @_;
32 2 ▶ my $options = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 2 2
33  
34 2 ▶ $options->{is_dirty} //= 0;
Condition50%1/2
$options->{"is_dirty"} //= 0
l!l
02
MC/DC0%0/1
$options->{"is_dirty"} //= 0
$options->{"is_dirty"}
Truth table50%1/2
A
A= $options->{"is_dirty"}
Aresult
11
00
35  
36 2 my $self = $class->SUPER::new($options);
37 2 $self->_build_filename;
38 2 $self->_build_data;
39  
40 2 return $self;
41 }
42  
43 2 ▶sub _build_data {
Subroutine _build_data: called CC=2 SCAR=7.8
44 2 my ($self) = @_;
45  
46 2 my $data = do {
47 2 ▶ if ( open my $fh, '<', $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $self->filename) { } 0 2 2
48 JSON::PP->new->utf8->decode(
49 0 do { local $RS; <$fh> }
50 );
51 }
52 else {
53 2 +{};
54 }
55 };
56  
57 2 $self->data($data);
58  
59 2 return $data;
60 }
61  
62 2 ▶sub _build_filename {
Subroutine _build_filename: called CC=1 SCAR=0.0
63 2 my ($self) = @_;
64  
65 2 return $self->filename( File::Spec->catfile( $self->directory, 'orepan2-cache.json' ) );
66 }
67  
68 0 ▶sub is_hit {
Subroutine is_hit: not called CC=10 SCAR=47.0
Pod undocumented
69 0 my ( $self, $stuff ) = @_;
70  
71 0 my $entry = $self->data->{$stuff};
72  
73 0 ▶ return 0 if !$entry || !$entry->{filename} || !$entry->{md5};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry and $entry->{"filename"} and $entry->{"md5"} 0 0 0
Condition0%0/6
$entry and $entry->{"filename"} and $entry->{"md5"}
!ll&&!rl&&r
000
$entry and $entry->{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/3
$entry and $entry->{"filename"} and $entry->{"md5"}
$entry $entry->{"filename"} $entry->{"md5"}
Truth table0%0/4
A and B and C
A= $entryB= $entry->{"filename"}C= $entry->{"md5"}
ABCresult
0XX0
10X0
1100
1111
74  
75 0 my $fullpath = File::Spec->catfile( $self->directory, $entry->{filename} );
76 0 ▶ return 0 if !-f $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fullpath 0 0 0
77  
78 0 ▶ if ( my $stat = stat($stuff) && defined( $entry->{mtime} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stat = &OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{"mtime"}) 0 0 0
Condition0%0/3
&OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{'mtime'}
!ll&&!rl&&r
000
MC/DC0%0/2
&OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{'mtime'}
&OrePAN2::Repository::Cache::stat($stuff) defined $entry->{'mtime'}
Truth table0%0/3
A && B
A= &OrePAN2::Repository::Cache::stat($stuff)B= defined $entry->{'mtime'}
ABresult
0X0
100
111
79 return 0
80 0 ▶ if $stat->mtime ne $entry->{mtime};
Branch: 1 branch
BranchTrueFalseTotal
if $stat->mtime ne $entry->{"mtime"} 0 0 0
81 }
82  
83 0 my $md5 = $self->calc_md5($fullpath);
84  
85 0 ▶ return ( !$md5 || $md5 ne $entry->{md5} ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
!$md5 || $md5 ne $entry->{'md5'} ? : 0 0 0
Condition0%0/3
not $md5 or $md5 ne $entry->{"md5"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $md5 or $md5 ne $entry->{"md5"}
not $md5 $md5 ne $entry->{"md5"}
Truth table0%0/3
not(A) or B
A= not $md5B= $md5 ne $entry->{"md5"}
ABresult
1X1
011
000
86 }
87  
88 2 ▶sub calc_md5 {
Subroutine calc_md5: called CC=2 SCAR=7.2
Pod undocumented
89 2 my ( $self, $filename ) = @_;
90  
91 open my $fh, '<', $filename
92 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or do { return } 2 0 2
93 0 return;
94 };
95  
96 2 my $md5 = Digest::MD5->new();
97 2 $md5->addfile($fh);
98  
99 2 return $md5->hexdigest;
100 }
101  
102 2 ▶sub set {
Subroutine set: called CC=3 SCAR=11.4
Pod documented
103 2 my ( $self, $stuff, $filename ) = @_;
104  
105 2 ▶ my $md5 = $self->calc_md5( File::Spec->catfile( $self->directory, $filename ) )
Branch: 1 branch
BranchTrueFalseTotal
my $md5 = $self->calc_md5("File::Spec"->catfile($self->directory, $filename)) or Carp::croak("Cannot calculate MD5 for '${filename}'") 2 0 2
106 or Carp::croak("Cannot calculate MD5 for '$filename'");
107 2 ▶ $self->data->{$stuff} = +{
Branch: 1 branch
BranchTrueFalseTotal
-f $filename ? : 0 2 2
108 filename => $filename,
109 md5 => $md5,
110 ( -f $filename ? ( mtime => stat($filename)->mtime ) : () ),
111 };
112  
113 2 return $self->is_dirty(1);
114 }
115  
116 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
117 0 my ($self) = @_;
118  
119 0 my $filename = $self->filename;
120  
121 0 my $json = JSON::PP->new->pretty(1)->canonical(1)->encode( $self->data );
122  
123 0 File::Path::mkpath( File::Basename::dirname($filename) );
124  
125 0 my $fh = IO::File::AtomicChange->new( $filename, 'w' );
126 0 $fh->print($json);
127  
128 0 return $fh->close(); # MUST CALL close EXPLICITLY
129 }
130  
131 2 1;
132  
↑ Summary OrePAN2/Injector.pm »