StaticCompressor/File.pm

filter: Statementstmt 92.0% 93 / 101 Branchbran 68.7% 22 / 32 Conditioncond 57.1% 12 / 21 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 94.1% 16 / 17 Podpod 100% 5 / 5 totaltotal 80.3% 151 / 188 CC 26 SCAR 18.3
CC
26
Cov
82%
_load_file
10.6
new
10.5
_process
6.0
CRAP 6.2
« StaticCompressor.pm ↑ Summary StaticCompressor/Container.pm »
1 package Mojolicious::Plugin::StaticCompressor::File;
2 # Single-file cache container
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use JavaScript::Minifier qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use CSS::Minifier qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Mojo::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶sub new {
Subroutine new: called CC=10 SCAR=23.5
Pod documented
15 14 my ($class, %hash) = @_;
16 14 my $s = bless({}, $class);
17  
18 # Initialize
19 14 ▶ $s->{mojo_static} = $hash{config}->{mojo_static} || die('Not specified mojo_static');
Condition50%1/2
$hash{'config'}{'mojo_static'} || die('Not specified mojo_static')
l!l
140
MC/DC0%0/1
$hash{'config'}{'mojo_static'} || die('Not specified mojo_static')
$hash{'config'}{'mojo_static'}
Truth table50%1/2
A
A= $hash{'config'}{'mojo_static'}
Aresult
11
00
20 14 ▶ $s->{path_cache_dir} = $hash{config}->{path_cache_dir} || die('Not specified path_cache_dir');
Condition50%1/2
$hash{'config'}{'path_cache_dir'} || die('Not specified path_cache_dir')
l!l
140
MC/DC0%0/1
$hash{'config'}{'path_cache_dir'} || die('Not specified path_cache_dir')
$hash{'config'}{'path_cache_dir'}
Truth table50%1/2
A
A= $hash{'config'}{'path_cache_dir'}
Aresult
11
00
21 14 ▶ $s->{path_single_cache_dir} = $hash{config}->{path_single_cache_dir} || die('Not specified path_single_cache_dir');
Condition50%1/2
$hash{'config'}{'path_single_cache_dir'} || die('Not specified path_single_cache_dir')
l!l
140
MC/DC0%0/1
$hash{'config'}{'path_single_cache_dir'} || die('Not specified path_single_cache_dir')
$hash{'config'}{'path_single_cache_dir'}
Truth table50%1/2
A
A= $hash{'config'}{'path_single_cache_dir'}
Aresult
11
00
22  
23 14 $s->{file_key} = undef;
24 14 ▶ $s->{path_file} = $hash{path_file} || die('Not specified path_file'); # Path of raw file
Condition50%1/2
$hash{'path_file'} || die('Not specified path_file')
l!l
140
MC/DC0%0/1
$hash{'path_file'} || die('Not specified path_file')
$hash{'path_file'}
Truth table50%1/2
A
A= $hash{'path_file'}
Aresult
11
00
25 14 ▶ $s->{extension} = $hash{extension} || die('Not specified extension');
Condition50%1/2
$hash{'extension'} || die('Not specified extension')
l!l
140
MC/DC0%0/1
$hash{'extension'} || die('Not specified extension')
$hash{'extension'}
Truth table50%1/2
A
A= $hash{'extension'}
Aresult
11
00
26 14 ▶ $s->{is_minify} = $hash{is_minify} || 0;
Condition100%2/2
$hash{'is_minify'} || 0
l!l
86
MC/DC100%1/1
$hash{'is_minify'} || 0
$hash{'is_minify'}
Truth table100%2/2
A
A= $hash{'is_minify'}
Aresult
11
00
27  
28 14 ▶ $s->{is_use_cache} = ($s->{is_minify}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s->{'is_minify'} ? : 8 6 14
29 14 $s->{updated_at} = undef; # Updated_at of processed (epoch sec)
30 14 $s->{content} = undef; # processed content
31 14 $s->{path_cached_file} = undef;
32  
33 14 ▶ if(defined $s->{path_file} && $s->{is_use_cache}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"path_file"} and $s->{"is_use_cache"}) 8 6 14
Condition66%2/3
defined $s->{"path_file"} and $s->{"is_use_cache"}
!ll&&!rl&&r
068
MC/DC50%1/2
defined $s->{"path_file"} and $s->{"is_use_cache"}
defined $s->{"path_file"} $s->{"is_use_cache"}
Truth table66%2/3
A and B
A= defined $s->{"path_file"}B= $s->{"is_use_cache"}
ABresult
0X0
100
111
34 8 $s->_load_file();
35 }
36  
37 14 return $s;
38 }
39  
40 # Generate the key of file
41 19 ▶sub get_key {
Subroutine get_key: called CC=3 SCAR=11.2
Pod documented
42 19 my $s = shift;
43 19 ▶ if($s->{is_use_cache}){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'is_use_cache'}) { } 19 0 19
44 19 ▶ if(!defined $s->{file_key}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->{"file_key"}) 8 11 19
45 8 $s->{file_key} = Mojo::Util::sha1_sum( $s->{path_file} ).'.'.$s->{extension};
46 }
47 19 return $s->{file_key};
48 } else {
49 0 return;
50 }
51 }
52  
53 # Get the processed content
54 11 ▶sub get_content {
Subroutine get_content: called CC=1 SCAR=0.0
Pod documented
55 11 my $s = shift;
56 # Check for update of source file
57 11 $s->_load_file();
58 11 return $s->{content};
59 }
60  
61 # Get for updated_at
62 6 ▶sub get_updated_at {
Subroutine get_updated_at: called CC=1 SCAR=0.0
Pod documented
63 6 my $s = shift;
64 # Check for update of source file
65 6 $s->_load_file();
66 6 return $s->{updated_at};
67 }
68  
69 # Get the path of file (raw)
70 18 ▶sub get_raw_path {
Subroutine get_raw_path: called CC=1 SCAR=0.0
Pod documented
71 18 my $s = shift;
72 18 return $s->{path_file};
73 }
74  
75 # Processing the file
76 0 ▶sub _process {
Subroutine _process: not called CC=2 SCAR=17.9
77 0 my $s = shift;
78 0 $s->_load_file();
79 0 ▶ if($s->{is_minify}){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"is_minify"}) 0 0 0
80 0 $s->_minify();
81 }
82 }
83  
84 # Check and make the single cache directory
85 19 ▶sub _check_cache_dir {
Subroutine _check_cache_dir: called CC=3 SCAR=11.1
86 19 my $s = shift;
87 19 ▶ if(! -d $s->{path_single_cache_dir}){
Branch: 1 branch
BranchTrueFalseTotal
if (not -d $s->{"path_single_cache_dir"}) 1 18 19
88 # Make the directory
89 1 ▶ mkdir( $s->{path_single_cache_dir} ) || die("Can't make a directory: ".$s->{path_single_cache_dir});
Branch: 1 branch
BranchTrueFalseTotal
mkdir $s->{"path_single_cache_dir"} or die q[Can't make a directory: ] . $s->{"path_single_cache_dir"} 1 0 1
90 }
91 }
92  
93 # Load content from the file and process
94 25 ▶sub _load_file {
Subroutine _load_file: called CC=10 SCAR=23.6
95 25 my $s = shift;
96 25 ▶ if(! $s->{is_use_cache}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->{"is_use_cache"}) 6 19 25
97 6 my $asset;
98 6 eval {
99 6 $asset = $s->{mojo_static}->file($s->{path_file});
100 6 my $updated_at = (stat($asset->path()))[9];
101 6 ▶ if(!defined $s->{updated_at} || $s->{updated_at} < $updated_at){ # Is Updated
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at) 6 0 6
Condition33%1/3
not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at
l!l&&r!l&&!r
600
MC/DC0%0/2
not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at
not defined $s->{"updated_at"} $s->{"updated_at"} < $updated_at
Truth table33%1/3
not(A) or B
A= not defined $s->{"updated_at"}B= $s->{"updated_at"} < $updated_at
ABresult
1X1
011
000
102 6 $s->{content} = Encode::decode_utf8($asset->slurp());
103 6 $s->{updated_at} = $updated_at;
104 }
105 0 ▶ }; if($@){ die ("Can't read static file: ". $s->{path_file} ."\n$@"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
106 6 return;
107 }
108  
109 19 $s->_check_cache_dir();
110  
111 # Generate cache path
112 19 $s->{path_cached_file} = $s->{path_single_cache_dir}.$s->get_key();
113  
114 # Load the file from cache
115 19 ▶ if(defined $s->{path_cached_file}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->{"path_cached_file"}) 19 0 19
116 19 eval{
117 19 my $cache = Mojo::Asset::File->new( path => $s->{path_cached_file} );
118 19 $s->{updated_at} = (stat($s->{path_cached_file}))[9];
119 19 $s->{content} = $cache->slurp();
120 };
121 }
122  
123 # Load the file and check for update
124 19 my ($asset, $updated_at, $raw_content);
125 19 eval {
126 19 $asset = $s->{mojo_static}->file($s->{path_file});
127 19 $updated_at = (stat($asset->path()))[9];
128 19 $raw_content = Encode::decode_utf8($asset->slurp());
129 0 ▶ }; if($@){ die ("Can't read static file: ".$asset->path()."\n$@"); }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 19 19
130  
131 # Process and cache
132 19 ▶ if(!defined $s->{updated_at} || $s->{updated_at} < $updated_at){ # Is Updated
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at) 3 16 19
Condition66%2/3
not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at
l!l&&r!l&&!r
3016
MC/DC50%1/2
not defined $s->{"updated_at"} or $s->{"updated_at"} < $updated_at
not defined $s->{"updated_at"} $s->{"updated_at"} < $updated_at
Truth table66%2/3
not(A) or B
A= not defined $s->{"updated_at"}B= $s->{"updated_at"} < $updated_at
ABresult
1X1
011
000
133 3 $s->{content} = $raw_content;
134 3 $s->{updated_at} = $updated_at;
135
136 3 ▶ if($s->{is_minify}){
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"is_minify"}) 3 0 3
137 # Process the file
138 3 $s->_minify();
139 # Cache to the file
140 3 my $cache = Mojo::Asset::File->new();
141 3 $cache->add_chunk( Encode::encode_utf8($s->{content}) );
142 3 $cache->move_to( $s->{path_cached_file} );
143 }
144 }
145 }
146  
147 # Minify the content
148 3 ▶sub _minify {
Subroutine _minify: called CC=3 SCAR=11.3
149 3 my $s = shift;
150  
151 3 ▶ if($s->{extension} eq 'js'){
Branch: 2 branches
BranchTrueFalseTotal
if ($s->{'extension'} eq 'js') { } 2 1 3
elsif ($s->{'extension'} eq 'css') { } 1 0 1
152 2 $s->{content} = JavaScript::Minifier::minify(input => $s->{content});
153 } elsif($s->{extension} eq 'css'){
154 1 $s->{content} = CSS::Minifier::minify(input => $s->{content});
155 } else {
156 0 die('Not supported file type');
157 }
158 }
159  
160 4 1;
161 __END__
« StaticCompressor.pm ↑ Summary StaticCompressor/Container.pm »