ArchivableFormats.pm

filter: Statementstmt 94.5% 52 / 55 Branchbran 50.0% 3 / 6 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 86.6% 78 / 90 CC 7 SCAR 6.2
CC
7
Cov
86%
identify_from_fh
3.6
identify_from_mimetype
3.3
_driver_mimetype
2.0
CRAP 1.9
↑ Summary ArchivableFormats/Plugin.pm »
1 package File::ArchivableFormats;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Be able to select archivable formats
6  
7 5 ▶use File::Basename qw(fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::MimeInfo::Magic;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '1.8';
14  
15 subtype 'PluginRole'
16 => as 'Object'
17 1 ▶ => where sub { $_->does('File::ArchivableFormats::Plugin') };
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 5 has driver => (
20 is => 'ro',
21 isa => 'Str',
22 predicate => 'has_driver',
23 );
24  
25 5 has _driver => (
26 is => 'ro',
27 isa => 'PluginRole',
28 builder => '_build_driver',
29 lazy => 1,
30 );
31  
32 1 ▶sub _build_driver {
Subroutine _build_driver: called CC=1 SCAR=0.0
33 1 my $self = shift;
34 1 ▶ return first { $_->name eq $self->driver } $self->installed_drivers;
Subroutine __ANON__: called CC=1 SCAR=0.0
35 }
36  
37 5 my @DRIVERS;
38  
39 3 ▶sub installed_drivers {
Subroutine installed_drivers: called CC=2 SCAR=7.0
Pod documented
40 3 ▶ if (!@DRIVERS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@DRIVERS) 3 0 3
41 3 my $finder = Module::Pluggable::Object->new(
42 search_path => 'File::ArchivableFormats::Plugin',
43 instantiate => 'new',
44 );
45 3 @DRIVERS = $finder->plugins;
46 }
47 3 return @DRIVERS;
48 }
49  
50 2 ▶sub identify_from_fh {
Subroutine identify_from_fh: called CC=3 SCAR=12.7
Pod documented
51 2 my ($self, $fh) = @_;
52  
53 2 ▶ if (blessed($fh) && $fh->isa('Path::Tiny')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($fh) and $fh->isa("Path::Tiny")) 0 2 2
Condition33%1/3
builtin::blessed($fh) and $fh->isa("Path::Tiny")
!ll&&!rl&&r
200
MC/DC0%0/2
builtin::blessed($fh) and $fh->isa("Path::Tiny")
builtin::blessed($fh) $fh->isa("Path::Tiny")
Truth table33%1/3
A and B
A= builtin::blessed($fh)B= $fh->isa("Path::Tiny")
ABresult
0X0
100
111
54 # Path::Tiny doesn't have a seek and it doesn't play well with .docx as
55 # it than identifies as a zip file. See t/200-dans.t for the test.
56 0 return $self->identify_from_fh("$fh");
57 }
58  
59 2 my $info = { mime_type => mimetype($fh)} ;
60 2 return $self->identify_from_mimetype($info->{mime_type});
61 }
62  
63 1 ▶sub identify_from_path {
Subroutine identify_from_path: called CC=1 SCAR=0.0
Pod documented
64 1 my ($self, $path) = @_;
65 1 return $self->identify_from_fh($path);
66 }
67  
68 1 ▶sub parse_extension {
Subroutine parse_extension: called CC=1 SCAR=0.0
Pod documented
69 1 my ($self, $filename) = @_;
70  
71 1 my (undef, undef, $ext) = fileparse($filename, '\.[^\.]*');
72 1 return lc($ext);
73 }
74  
75 2 ▶sub identify_from_mimetype {
Subroutine identify_from_mimetype: called CC=3 SCAR=12.0
Pod documented
76 2 my ($self, $mimetype) = @_;
77  
78 2 my %rv = ( mime_type => $mimetype );
79  
80 2 ▶ if ($self->has_driver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_driver) { } 2 0 2
81 2 $rv{ $self->driver } = $self->_driver_mimetype($self->_driver, $mimetype);
82 }
83 else {
84 0 for my $driver ($self->installed_drivers) {
85 0 $rv{ $driver->name } = $self->_driver_mimetype($driver, $mimetype);
86 }
87 }
88  
89 2 return \%rv;
90 }
91  
92 2 ▶sub _driver_mimetype {
Subroutine _driver_mimetype: called CC=2 SCAR=7.1
93 2 my ($self, $driver, $mimetype) = @_;
94 return {
95 archivable => $driver->is_archivable($mimetype) || 0,
96 2 ▶ %{ $driver->allowed_extensions($mimetype) },
Condition50%1/2
$driver->is_archivable($mimetype) || 0
l!l
20
MC/DC0%0/1
$driver->is_archivable($mimetype) || 0
$driver->is_archivable($mimetype)
Truth table50%1/2
A
A= $driver->is_archivable($mimetype)
Aresult
11
00
97 };
98  
99 }
100  
101 5 __PACKAGE__->meta->make_immutable;
102  
103 __END__
↑ Summary ArchivableFormats/Plugin.pm »