Silki/Role/Schema/File.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 3 SCAR 10.4
CC
3
Cov
0%
_build_resized_image
6.0
_build_file_on_disk
6.0
_build_small_image_file
2.0
CRAP 2.8
1 package Silki::Role::Schema::File;
2 {
3 $Silki::Role::Schema::File::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8 use namespace::autoclean;
9 use autodie;
10  
11 use Digest::SHA qw( sha256_hex );
12 use File::stat;
13 use Image::Magick;
14 use Image::Thumbnail;
15 use Silki::Config;
16 use Silki::Types qw( File Maybe Str );
17  
18 use Moose::Role;
19  
20 requires 'filename';
21  
22 has file_on_disk => (
23 is => 'ro',
24 isa => File,
25 init_arg => undef,
26 lazy => 1,
27 builder => '_build_file_on_disk',
28 clearer => '_clear_file_on_disk', # for testing
29 );
30  
31 has _filename_with_hash => (
32 is => 'ro',
33 isa => Str,
34 init_arg => undef,
35 lazy => 1,
36 builder => '_build_filename_with_hash',
37 );
38  
39 has small_image_file => (
40 is => 'ro',
41 isa => Maybe [File],
42 init_arg => undef,
43 lazy => 1,
44 builder => '_build_small_image_file',
45 );
46  
47 has thumbnail_file => (
48 is => 'ro',
49 isa => Maybe [File],
50 init_arg => undef,
51 lazy => 1,
52 builder => '_build_thumbnail_file',
53 );
54  
55 has mini_image_file => (
56 is => 'ro',
57 isa => Maybe [File],
58 init_arg => undef,
59 lazy => 1,
60 builder => '_build_mini_image_file',
61 );
62  
63 sub _build_small_image_file {
64 my $self = shift;
65  
66 $self->_build_resized_image(
67 Silki::Config->instance()->small_image_dir(),
68 '150x400',
69 );
70 }
71  
72 sub _build_thumbnail_file {
73 my $self = shift;
74  
75 $self->_build_resized_image(
76 Silki::Config->instance()->thumbnails_dir(),
77 '75x200',
78 );
79 }
80  
81 sub _build_mini_image_file {
82 my $self = shift;
83  
84 $self->_build_resized_image(
85 Silki::Config->instance()->mini_image_dir(),
86 '40x40',
87 );
88 }
89  
90 sub _build_resized_image {
91 my $self = shift;
92 my $dir = shift;
93 my $dimensions = shift;
94  
95 my $file = $dir->file( $self->_filename_with_hash() );
96  
97 return $file
98 if -f $file
99 && ( File::stat::populate( CORE::stat(_) ) )->mtime()
100 >= $self->creation_datetime()->epoch();
101  
102 Image::Thumbnail->new(
103 module => 'Image::Magick',
104 size => $dimensions,
105 create => 1,
106 inputpath => $self->file_on_disk()->stringify(),
107 outputpath => $file->stringify(),
108 );
109  
110 return $file;
111 }
112  
113 sub _build_file_on_disk {
114 my $self = shift;
115  
116 my $dir = Silki::Config->instance()->files_dir();
117  
118 my $file = $dir->file( $self->_filename_with_hash() );
119  
120 return $file
121 if -f $file
122 && ( File::stat::populate( CORE::stat(_) ) )->mtime()
123 >= $self->creation_datetime()->epoch();
124  
125 open my $fh, '>', $file;
126 print {$fh} $self->contents();
127 close $fh;
128  
129 return $file;
130 }
131  
132 sub _build_filename_with_hash {
133 my $self = shift;
134  
135 return join q{-},
136 sha256_hex( $self->pk_values_hash(), Silki::Config->instance()->secret() ),
137 $self->filename();
138 }
139  
140 1;