UUID/File.pm

filter: Statementstmt 35.6% 26 / 73 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 40.0% 6 / 15 Podpod 100% 5 / 5 totaltotal 27.4% 37 / 135 CC 17 SCAR 29.1
CC
17
Cov
24%
remove
42.0
extension
30.0
write_metadata
20.0
CRAP 18.3
↑ Summary UUID.pm »
1 package Cantella::Store::UUID::File;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::MimeInfo::Magic ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooseX::Types::Data::GUID qw/GUID/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooseX::Types::Path::Class qw/Dir File/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.003003';
13  
14 1 has uuid => (is => 'ro', isa => GUID, coerce => 1, required => 1);
15 1 has dir => (is => 'ro', isa => Dir, coerce => 1);
16  
17 1 has path => (is => 'ro', isa => File, coerce => 1, lazy_build => 1);
18 1 has _meta_file => (is => 'ro', isa => File, coerce => 1, lazy_build => 1);
19  
20 has metadata => (
21 traits => ['Hash'],
22 is => 'rw',
23 isa => 'HashRef',
24 lazy_build => 1,
25 0 ▶ trigger => sub { shift->write_metadata },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 1 handles => {
27 'get_property' => 'get',
28 'set_property' => 'set',
29 'has_property' => 'exists',
30 'clear_property', => 'delete',
31 },
32 );
33  
34 0 ▶sub _build_path {
Subroutine _build_path: not called CC=1 SCAR=6.9
35 0 my $self = shift;
36 0 $self->dir->file( $self->uuid->as_string );
37 }
38  
39 0 ▶sub _build__meta_file {
Subroutine _build__meta_file: not called CC=1 SCAR=6.9
40 0 my $self = shift;
41 0 $self->dir->file((join '.', $self->uuid->as_string, 'meta' ));
42 }
43  
44 0 ▶sub _build_metadata {
Subroutine _build_metadata: not called CC=3 SCAR=24.8
45 0 my $self = shift;
46 0 my $file = $self->_meta_file;
47 0 ▶ if( my $json = $file->slurp ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = $file->slurp) 0 0 0
48 0 ▶ if( my $perl = eval { JSON::from_json( $json ) }){
Branch: 1 branch
BranchTrueFalseTotal
if (my $perl = eval { do { &JSON::from_json($json) } }) 0 0 0
49 0 return $perl;
50 }
51 0 die("Failed to parse contents of meta file $file: ${@}");
52 }
53 0 die("Failed to read file $file: ${!}");
54 }
55  
56 0 ▶sub write_metadata {
Subroutine write_metadata: not called CC=4 SCAR=30.0
Pod documented
57 0 my $self = shift;
58 0 my $file = $self->_meta_file;
59 0 ▶ if (my $json = JSON::to_json( $self->metadata || {} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $json = &JSON::to_json(scalar($self->metadata || {}))) 0 0 0
Condition0%0/2
$self->metadata || {}
l!l
00
MC/DC0%0/1
$self->metadata || {}
$self->metadata
Truth table0%0/2
A
A= $self->metadata
Aresult
11
00
60 0 ▶ if( my $fh = $file->openw ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $file->openw) 0 0 0
61 0 print $fh $json;
62 0 return 1;
63 }
64 0 die("Failed to write meta file '${file}' Contents: '${json}': ${!}");
65 }
66 0 die("Failed to serialize metadata");
67 }
68  
69 0 ▶sub remove {
Subroutine remove: not called CC=6 SCAR=37.4
Pod documented
70 0 my $self = shift;
71 0 my $uuid = $self->uuid;
72 0 my $file_path = $self->path;
73 0 my $meta_path = $self->_meta_file;
74  
75 0 ▶ if( -e $meta_path && !$meta_path->remove ){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $meta_path and not $meta_path->remove) 0 0 0
Condition0%0/3
-e $meta_path and not $meta_path->remove
!ll&&!rl&&r
000
MC/DC0%0/2
-e $meta_path and not $meta_path->remove
-e $meta_path not $meta_path->remove
Truth table0%0/3
A and not(B)
A= -e $meta_pathB= not $meta_path->remove
ABresult
0X0
100
111
76 0 die("Can't remove '${uuid}': Failed to delete '${meta_path}': ${!}");
77 }
78 0 ▶ if (-e $file_path && !$file_path->remove){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file_path and not $file_path->remove) 0 0 0
Condition0%0/3
-e $file_path and not $file_path->remove
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file_path and not $file_path->remove
-e $file_path not $file_path->remove
Truth table0%0/3
A and not(B)
A= -e $file_pathB= not $file_path->remove
ABresult
0X0
100
111
79 0 die("Can't remove '${uuid}': Failed to delete '${file_path}': ${!}");
80 }
81  
82 0 ▶ return ! (-e $self->path || -e $self->_meta_file);
Condition0%0/3
-e $self->path || -e $self->_meta_file
l!l&&r!l&&!r
000
MC/DC0%0/2
-e $self->path || -e $self->_meta_file
-e $self->path -e $self->_meta_file
Truth table0%0/3
A || B
A= -e $self->pathB= -e $self->_meta_file
ABresult
1X1
011
000
83 }
84  
85 0 ▶sub exists {
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
86 0 my $self = shift;
87 0 return -e $self->path and -e $self->_meta_file;
88 }
89  
90 0 ▶sub extension {
Subroutine extension: not called CC=5 SCAR=34.0
Pod documented
91 0 my $self = shift;
92 0 ▶ if( my $type = $self->mime_type ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $self->mime_type) 0 0 0
93 0 ▶ if( my $ext = File::MimeInfo::extensions( $type ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ext = File::MimeInfo::extensions($type)) 0 0 0
94 0 return $ext;
95 }
96 }
97 0 ▶ if( $self->has_property('original_name') ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_property("original_name")) 0 0 0
98 0 ▶ if( $self->get_property('original_name') =~ /\.(\w+)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_property("original_name") =~ /\.(\w+)$/) 0 0 0
99 0 return $1;
100 }
101 }
102 0 return '';
103 }
104  
105 0 ▶sub mime_type {
Subroutine mime_type: not called CC=3 SCAR=24.8
Pod documented
106 0 my $self = shift;
107 0 ▶ return $self->get_property('mime-type') if $self->has_property('mime-type');
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_property("mime-type") 0 0 0
108 0 ▶ if( my $type = File::MimeInfo::Magic::mimetype( $self->path->stringify ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = File::MimeInfo::Magic::mimetype($self->path->stringify)) 0 0 0
109 0 $self->set_property('mime-type', $type);
110 0 $self->write_metadata;
111 0 return $type;
112 }
113 0 return;
114 }
115  
116 1 __PACKAGE__->meta->make_immutable;
117  
118 1 1;
119  
120 __END__;
↑ Summary UUID.pm »