blib/lib/OrePAN2/Repository/Cache.pm

filter: Statementstmt 95.0% 76 / 80 Branchbran 50.0% 12 / 24 Conditioncond 50.0% 6 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 6 totaltotal 76.8% 113 / 147 CC 18 SCAR 17.7
CC
18
Cov
81%
is_hit
15.3
new
4.5
set
3.1
CRAP 5.9
« blib/lib/OrePAN2/Auditor.pm ↑ Summary blib/lib/OrePAN2/Index.pm »
1 package OrePAN2::Repository::Cache;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use Class::Accessor::Lite 0.05 (
10 3 rw => [qw(is_dirty directory)],
11 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use IO::File::AtomicChange;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub new {
20 2 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=15.0
Pod undocumented
21 2 ▶ my %args = @_ == 1 ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 2 2
22  
23 2 for my $key (qw(directory)) {
24 2 ▶ unless ( exists $args{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{$key}) 0 2 2
25 0 Carp::croak("Missing mandatory parameter: $key");
26 }
27 }
28 2 my $self = bless {
29 %args,
30 }, $class;
31 $self->{filename}
32 2 = File::Spec->catfile( $self->{directory}, 'orepan2-cache.json' );
33 2 return $self;
34 }
35  
36 sub data {
37 2 ▶ my $self = shift;
Subroutine data: called CC=3 SCAR=11.1
Pod undocumented
38 2 ▶ $self->{data} ||= do {
Condition33%1/3
$self->{"data"} ||= do {
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"data"} ||= do { if (open my $fh, "<", $self->{"filename"}) { JSON::PP::decode_json(do { local $/; readline $fh }); } else { {}; } }
$self->{"data"} do { if (open my $fh, "<", $self->{"filename"}) { JSON::PP::decode_json(do { local $/; readline $fh }); } else { {}; } }
Truth table33%1/3
A ||= B
A= $self->{"data"}B= do { if (open my $fh, "<", $self->{"filename"}) { JSON::PP::decode_json(do { local $/; readline $fh }); } else { {}; } }
ABresult
1X1
011
000
39 2 ▶ if ( open my $fh, '<', $self->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $self->{'filename'}) { } 1 1 2
40 JSON::PP::decode_json(
41 1 do { local $/; <$fh> }
42 );
43 }
44 else {
45 1 +{};
46 }
47 };
48 }
49  
50 sub is_hit {
51 2 ▶ my ( $self, $stuff ) = @_;
Subroutine is_hit: called CC=10 SCAR=27.3
Pod undocumented
52  
53 2 my $entry = $self->data->{$stuff};
54  
55 2 ▶ return 0 unless $entry && $entry->{filename} && $entry->{md5};
Branch: 1 branch
BranchTrueFalseTotal
unless $entry and $entry->{"filename"} and $entry->{"md5"} 0 1 1
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
56  
57 my $fullpath
58 1 = File::Spec->catfile( $self->directory, $entry->{filename} );
59 1 ▶ return 0 unless -f $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fullpath 0 1 1
60  
61 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
62 0 ▶ return 0 if $stat->mtime ne $entry->{mtime};
Branch: 1 branch
BranchTrueFalseTotal
if $stat->mtime ne $entry->{"mtime"} 0 0 0
63 }
64  
65 1 my $md5 = $self->calc_md5($fullpath);
66 1 ▶ return 0 unless $md5;
Branch: 1 branch
BranchTrueFalseTotal
unless $md5 0 1 1
67 1 ▶ return 0 if $md5 ne $entry->{md5};
Branch: 1 branch
BranchTrueFalseTotal
if $md5 ne $entry->{"md5"} 0 1 1
68 1 return 1;
69 }
70  
71 sub calc_md5 {
72 2 ▶ my ( $self, $filename ) = @_;
Subroutine calc_md5: called CC=2 SCAR=7.2
Pod undocumented
73  
74 open my $fh, '<', $filename
75 2 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or do { return } 0 2 2
76 0 return;
77 };
78  
79 2 my $md5 = Digest::MD5->new();
80 2 $md5->addfile($fh);
81 2 return $md5->hexdigest;
82 }
83  
84 sub set {
85 1 ▶ my ( $self, $stuff, $filename ) = @_;
Subroutine set: called CC=3 SCAR=11.4
Pod undocumented
86  
87 1 ▶ my $md5
Branch: 1 branch
BranchTrueFalseTotal
my $md5 = $self->calc_md5("File::Spec"->catfile($self->directory, $filename)) or Carp::croak("Cannot calcurate MD5 for '${filename}'") 0 1 1
88 = $self->calc_md5(
89 File::Spec->catfile( $self->directory, $filename ) )
90 or Carp::croak("Cannot calcurate MD5 for '$filename'");
91 1 ▶ $self->{data}->{$stuff} = +{
Branch: 1 branch
BranchTrueFalseTotal
-f $filename ? : 0 1 1
92 filename => $filename,
93 md5 => $md5,
94 ( -f $filename ? ( mtime => stat($filename)->mtime ) : () ),
95 };
96 1 $self->is_dirty(1);
97 }
98  
99 sub save {
100 1 ▶ my ($self) = @_;
Subroutine save: called CC=1 SCAR=0.0
Pod undocumented
101  
102 1 my $filename = $self->{filename};
103 my $json
104 1 = JSON::PP->new->pretty(1)->canonical(1)->encode( $self->{data} );
105  
106 1 File::Path::mkpath( File::Basename::dirname($filename) );
107  
108 1 my $fh = IO::File::AtomicChange->new( $filename, 'w' );
109 1 $fh->print($json);
110 1 $fh->close(); # MUST CALL close EXPLICITLY
111 }
112  
113 3 1;
114  
« blib/lib/OrePAN2/Auditor.pm ↑ Summary blib/lib/OrePAN2/Index.pm »