blib/lib/OrePAN2/Repository/Cache.pm

filter: Statementstmt 97.4% 76 / 78 Branchbran 55.0% 11 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 4 totaltotal 82.8% 111 / 134 CC 14 SCAR 17.1
CC
14
Cov
86%
is_hit
14.1
set
3.1
calc_md5
2.1
CRAP 5.6
« blib/lib/OrePAN2/CLI/Inject.pm ↑ Summary blib/lib/OrePAN2/Index.pm »
1 package OrePAN2::Repository::Cache;
2  
3 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Digest::MD5 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::stat qw( stat );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use IO::File::AtomicChange ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Types::Standard qw( Bool HashRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Types::Path::Tiny qw( Path );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 has directory => ( is => 'ro', isa => Path, coerce => 1, required => 1 );
20 6 has data => ( is => 'lazy', isa => HashRef, builder => 1 );
21 6 has filename => ( is => 'lazy', isa => Str, builder => 1 );
22 6 has is_dirty => ( is => 'rw', isa => Bool, default => !!0 );
23  
24 5 ▶sub _build_data {
Subroutine _build_data: called CC=2 SCAR=6.9
25 5 my $self = shift;
26 5 return do {
27 5 ▶ if ( open my $fh, '<', $self->filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $self->filename) { } 1 4 5
28 JSON::PP->new->utf8->decode(
29 1 do { local $/; <$fh> }
30 );
31 }
32 else {
33 4 +{};
34 }
35 };
36 }
37  
38 5 ▶sub _build_filename {
Subroutine _build_filename: called CC=1 SCAR=0.0
39 5 my $self = shift;
40 5 return File::Spec->catfile( $self->directory, 'orepan2-cache.json' );
41 }
42  
43 2 ▶sub is_hit {
Subroutine is_hit: called CC=10 SCAR=26.4
Pod undocumented
44 2 my ( $self, $stuff ) = @_;
45  
46 2 my $entry = $self->data->{$stuff};
47  
48 2 ▶ return 0 unless $entry && $entry->{filename} && $entry->{md5};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry and $entry->{"filename"} and $entry->{"md5"} 1 1 2
Condition66%4/6
$entry and $entry->{"filename"} and $entry->{"md5"}
!ll&&!rl&&r
101
$entry and $entry->{"filename"}
!ll&&!rl&&r
101
MC/DC33%1/3
$entry and $entry->{"filename"} and $entry->{"md5"}
$entry $entry->{"filename"} $entry->{"md5"}
Truth table50%2/4
A and B and C
A= $entryB= $entry->{"filename"}C= $entry->{"md5"}
ABCresult
0XX0
10X0
1100
1111
49  
50 my $fullpath
51 1 = File::Spec->catfile( $self->directory, $entry->{filename} );
52 1 ▶ return 0 unless -f $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fullpath 0 1 1
53  
54 1 ▶ if ( my $stat = stat($stuff) && defined( $entry->{mtime} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stat = &OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{"mtime"}) 0 1 1
Condition33%1/3
&OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{'mtime'}
!ll&&!rl&&r
010
MC/DC0%0/2
&OrePAN2::Repository::Cache::stat($stuff) && defined $entry->{'mtime'}
&OrePAN2::Repository::Cache::stat($stuff) defined $entry->{'mtime'}
Truth table33%1/3
A && B
A= &OrePAN2::Repository::Cache::stat($stuff)B= defined $entry->{'mtime'}
ABresult
0X0
100
111
55 0 ▶ return 0 if $stat->mtime ne $entry->{mtime};
Branch: 1 branch
BranchTrueFalseTotal
if $stat->mtime ne $entry->{"mtime"} 0 0 0
56 }
57  
58 1 my $md5 = $self->calc_md5($fullpath);
59 1 ▶ return 0 unless $md5;
Branch: 1 branch
BranchTrueFalseTotal
unless $md5 0 1 1
60 1 ▶ return 0 if $md5 ne $entry->{md5};
Branch: 1 branch
BranchTrueFalseTotal
if $md5 ne $entry->{"md5"} 0 1 1
61 1 return 1;
62 }
63  
64 5 ▶sub calc_md5 {
Subroutine calc_md5: called CC=2 SCAR=7.2
Pod undocumented
65 5 my ( $self, $filename ) = @_;
66  
67 open my $fh, '<', $filename
68 5 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or do { return } 5 0 5
69 0 return;
70 };
71  
72 5 my $md5 = Digest::MD5->new();
73 5 $md5->addfile($fh);
74 5 return $md5->hexdigest;
75 }
76  
77 4 ▶sub set {
Subroutine set: called CC=3 SCAR=11.4
Pod undocumented
78 4 my ( $self, $stuff, $filename ) = @_;
79  
80 4 ▶ my $md5
Branch: 1 branch
BranchTrueFalseTotal
my $md5 = $self->calc_md5("File::Spec"->catfile($self->directory, $filename)) or Carp::croak("Cannot calculate MD5 for '${filename}'") 4 0 4
81 = $self->calc_md5(
82 File::Spec->catfile( $self->directory, $filename ) )
83 or Carp::croak("Cannot calculate MD5 for '$filename'");
84 4 ▶ $self->data->{$stuff} = +{
Branch: 1 branch
BranchTrueFalseTotal
-f $filename ? : 0 4 4
85 filename => $filename,
86 md5 => $md5,
87 ( -f $filename ? ( mtime => stat($filename)->mtime ) : () ),
88 };
89 4 $self->is_dirty(1);
90 }
91  
92 2 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod undocumented
93 2 my ($self) = @_;
94  
95 2 my $filename = $self->filename;
96 2 my $json
97 = JSON::PP->new->pretty(1)->canonical(1)->encode( $self->data );
98  
99 2 File::Path::mkpath( File::Basename::dirname($filename) );
100  
101 2 my $fh = IO::File::AtomicChange->new( $filename, 'w' );
102 2 $fh->print($json);
103 2 $fh->close(); # MUST CALL close EXPLICITLY
104 }
105  
106 6 1;
107  
« blib/lib/OrePAN2/CLI/Inject.pm ↑ Summary blib/lib/OrePAN2/Index.pm »