CSE/File.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 137 CC 8 SCAR 18.3
CC
8
Cov
0%
requalify
20.0
_build_content
12.0
_build_raw_content
6.0
CRAP 6.2
1 package App::CSE::File;
2 $App::CSE::File::VERSION = '0.016';
3 use Moose;
4  
5 use App::CSE;
6 use Class::Load;
7 use Encode;
8 use File::Basename;
9 use File::Slurp;
10 use File::stat qw//;
11 use DateTime;
12 use String::CamelCase;
13  
14 use Log::Log4perl;
15 my $LOGGER = Log::Log4perl->get_logger();
16  
17 has 'cse' => ( is => 'ro' , isa => 'App::CSE' , required => 1 );
18  
19 has 'mime_type' => ( is => 'ro', isa => 'Str', required => 1);
20 has 'file_path' => ( is => 'ro', isa => 'Str', required => 1);
21 has 'dir' => ( is => 'ro' , isa => 'Str' , required => 1, lazy_build => 1);
22  
23 has 'encoding' => ( is => 'ro' , isa => 'Str', lazy_build => 1 );
24 has 'content' => ( is => 'ro' , isa => 'Maybe[Str]', required => 1, lazy_build => 1 );
25 has 'raw_content' => ( is => 'ro' , isa => 'Maybe[Str]', required => 1 , lazy_build => 1);
26 has 'decl' => ( is => 'ro' , isa => 'ArrayRef[Str]' , lazy_build => 1);
27 has 'call' => ( is => 'ro' , isa => 'ArrayRef[Str]' , lazy_build => 1);
28  
29 has 'stat' => ( is => 'ro' , isa => 'File::stat' , lazy_build => 1 );
30 has 'mtime' => ( is => 'ro' , isa => 'DateTime' , lazy_build => 1);
31  
32 sub _build_decl{
33 return [];
34 }
35  
36 sub _build_call{
37 return [];
38 }
39  
40 sub _build_dir{
41 my ($self) = @_;
42 return File::Basename::dirname($self->file_path());
43 }
44  
45 sub _build_stat{
46 my ($self) = @_;
47 return File::stat::stat($self->file_path());
48 }
49  
50 sub _build_mtime{
51 my ($self) = @_;
52 return DateTime->from_epoch( epoch => $self->stat->mtime() );
53 }
54  
55  
56 sub _build_encoding{
57 my ($self) = @_;
58 ## This is the default. Override that in specific file types
59 return 'UTF-8';
60 }
61  
62 sub _build_raw_content{
63 my ($self) = @_;
64 if( $self->stat()->size() > $self->cse()->max_size() ){
65 return undef;
66 }
67 return scalar( File::Slurp::read_file($self->file_path(), binmode => ':raw') );
68 }
69  
70 sub _build_content{
71 my ($self) = @_;
72 my $raw_content = $self->raw_content();
73 unless( defined($raw_content) ){ return undef; }
74  
75 my $decoded = eval{ Encode::decode($self->encoding(), $raw_content, Encode::FB_CROAK ); };
76 unless( $decoded ){
77 $LOGGER->debug("File ".$self->file_path()." failed to be decoded as ".$self->encoding().": ".$@);
78 return;
79 }
80 return $decoded;
81 }
82  
83 sub effective_object{
84 my ($self) = @_;
85 return $self;
86 }
87  
88 =head2 requalify
89  
90 Requalifies this object into the given mimetype.
91  
92 =cut
93  
94 sub requalify{
95 my ($self, $mimetype) = @_;
96  
97 my $class = __PACKAGE__->class_for_mime($mimetype, $self->file_path());
98 unless( $class ){
99 confess("Cannot requalify in $mimetype. No class found");
100 }
101  
102 return $class->new({ cse => $self->cse(),
103 file_path => $self->file_path(),
104 mime_type => $mimetype,
105 ( $self->has_dir() ? ( dir => $self->dir() ) : () ),
106 ( $self->has_content() ? ( content => $self->content() ) : () )
107 });
108 }
109  
110 =head2 class_for_mime
111  
112 Returns the File subclass that goes well with the given mimetype.
113 This is a Class method.
114  
115 You can also give a file name to make the diagnostic easier in case no
116 class is found.
117  
118 Usage:
119  
120 my $class = App::CSE::File->class_for_mime('application/x-perl');
121 my $class = App::CSE::File->class_for_mime('application/x-perl' , 'the/file/name.something');
122  
123 =cut
124  
125 sub class_for_mime{
126 my ($class, $mime_type , $file_name) = @_;
127  
128 $file_name ||= 'unknown_file_name';
129  
130 my $half_camel = $mime_type; $half_camel =~ s/\W/_/g;
131 my $file_class_name = 'App::CSE::File::'.String::CamelCase::camelize($half_camel);
132 # Protect against unsecure class name
133 ( $file_class_name ) = ( $file_class_name =~ /^([\w:]+)$/ );
134 my $file_class = eval{ Class::Load::load_class($file_class_name); };
135 unless( $file_class ){
136 # A bit dirty, but well..
137 $LOGGER->debug(App::CSE->instance()->colorizer->colored("No class '$file_class_name' for mimetype $mime_type ($file_name)",'red bold'));
138 return undef;
139 }
140 return $file_class;
141 }
142  
143 __PACKAGE__->meta->make_immutable();
144  
145 __END__