Builder/Attachment.pm

filter: Statementstmt 92.9% 66 / 71 Branchbran 70.8% 17 / 24 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 85.7% 108 / 126 CC 17 SCAR 14.2
CC
17
Cov
86%
_build_mimetype
6.0
serialize
5.3
__ANON__
5.0
CRAP 4.1
« Builder/Image.pm ↑ Summary Builder/Utils.pm »
1 # ============================================================================
2 package Mail::Builder::Attachment;
3 # ============================================================================
4  
5 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 with qw(Mail::Builder::Role::File);
8 8 ▶use Mail::Builder::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use MIME::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 our $VERSION = $Mail::Builder::VERSION;
15  
16 has 'name' => (
17 is => 'rw',
18 isa => 'Str',
19 lazy_build => 1,
20 4 ▶ trigger => sub { shift->clear_cache },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 8 );
22  
23 has 'mimetype' => (
24 is => 'rw',
25 isa => 'Mail::Builder::Type::Mimetype',
26 lazy_build => 1,
27 1 ▶ trigger => sub { shift->clear_cache },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 );
29  
30  
31 6 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
32 6 my $orig = shift;
33 6 my $class = shift;
34  
35 6 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 2 4 6
Condition100%3/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
222
MC/DC100%2/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
36 2 return $class->$orig($_[0]);
37 }
38 else {
39 4 my $params = {
40 file => $_[0],
41 };
42 4 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 2 2 4
43 2 $params->{name} = $_[1];
44 }
45 4 ▶ if (defined $_[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 0 4 4
46 0 $params->{mimetype} = $_[2];
47 }
48 4 return $class->$orig($params);
49 }
50 8 };
51  
52 6 ▶sub _build_mimetype {
Subroutine _build_mimetype: called CC=6 SCAR=18.0
53 6 my ($self) = @_;
54  
55 6 my $filename = $self->filename;
56 6 my $filetype;
57  
58 6 ▶ if (defined $filename
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/) 4 2 6
Condition66%2/3
defined $filename and lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/
!ll&&!rl&&r
204
MC/DC50%1/2
defined $filename and lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/
defined $filename lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/
Truth table66%2/3
A and B
A= defined $filenameB= lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/
ABresult
0X0
100
111
59 && lc($filename->basename) =~ /\.([0-9a-z]{1,4})$/) {
60 4 my $mimetype = MIME::Types->new->mimeTypeOf($1);
61 4 ▶ $filetype = $mimetype->type
Branch: 1 branch
BranchTrueFalseTotal
if defined $mimetype 4 0 4
62 if defined $mimetype;
63 }
64  
65 6 ▶ unless (defined $filetype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filetype) 2 4 6
66 2 my $filecontent = $self->filecontent;
67 2 $filetype = $self->_check_magic_string($filecontent);
68 }
69  
70 6 ▶ $filetype ||= 'application/octet-stream';
Condition100%2/2
$filetype ||= "application/octet-stream"
l!l
42
MC/DC100%1/1
$filetype ||= "application/octet-stream"
$filetype
Truth table100%2/2
A
A= $filetype
Aresult
11
00
71  
72 6 return $filetype;
73 }
74  
75 1 ▶sub _build_name {
Subroutine _build_name: called CC=4 SCAR=16.1
76 1 my ($self) = @_;
77  
78 1 my $filename = $self->filename;
79 1 my $name;
80  
81 1 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 0 1 1
82 0 $name = $filename->basename;
83 }
84  
85 1 ▶ unless (defined $name
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and not $name =~ /^\s*$/) 1 0 1
Condition33%1/3
defined $name and not $name =~ /^\s*$/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $name and not $name =~ /^\s*$/
defined $name not $name =~ /^\s*$/
Truth table33%1/3
A and not(B)
A= defined $nameB= not $name =~ /^\s*$/
ABresult
0X0
100
111
86 && $name !~ m/^\s*$/) {
87 1 croak('Could not determine the attachment name automatically');
88 }
89  
90 0 return $name;
91 }
92  
93 3 ▶sub serialize {
Subroutine serialize: called CC=5 SCAR=16.6
Pod documented
94 3 my ($self) = @_;
95  
96 3 ▶ return $self->cache
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cache 0 3 3
97 if ($self->has_cache);
98  
99 3 my $file = $self->file;
100 3 my $accessor;
101 my $value;
102  
103 3 ▶ if (blessed $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($file)) { } 3 0 3
104 3 ▶ if ($file->isa('IO::File')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($file->isa('IO::File')) { } 2 1 3
elsif ($file->isa('Path::Class::File')) { } 1 0 1
105 2 $accessor = 'Data';
106 2 $value = $self->filecontent;
107 } elsif ($file->isa('Path::Class::File')) {
108 1 $accessor = 'Path';
109 1 $value = $file->stringify;
110 }
111 } else {
112 0 $accessor = 'Data';
113 0 $value = $$file;
114 }
115  
116 3 my $entity = MIME::Entity->build(
117 Disposition => 'attachment',
118 Type => $self->mimetype,
119 Top => 0,
120 Filename => Mail::Builder::Utils::encode_mime($self->name),
121 Encoding => 'base64',
122 $accessor => $value,
123 );
124  
125 2 $self->cache($entity);
126  
127 2 return $entity;
128 }
129  
130 8 __PACKAGE__->meta->make_immutable;
131  
132 8 1;
133  
134 =encoding utf8
135  
136 =head1 NAME
137  
138 Mail::Builder::Attachment - Class for handling e-mail attachments
139  
140 =head1 SYNOPSIS
141  
142 use Mail::Builder::Attachment;
143
144 my $attachment1 = Mail::Builder::Attachment->new({
145 file => 'path/to/attachment.pdf',
146 name => 'LoveLetter.txt.vbs',
147 });
148
149 my $attachment2 = Mail::Builder::Attachment->new($fh);
150
151 my $attachment_entity = $attachment1->serialize;
152  
153 =head1 DESCRIPTION
154  
155 This class handles e-mail attachments for Mail::Builder.
156  
157 =head1 METHODS
158  
159 =head2 Constructor
160  
161 =head3 new
162  
163 The constructor can be called in multiple ways
164  
165 Mail::Builder::Attachment->new({
166 file => Path | Path::Class::File | IO::File | FH | ScalarRef,
167 [ name => Attachment filename, ]
168 [ mimetype => MIME type, ]
169 })
170 OR
171 Mail::Builder::Image->new(
172 Path | Path::Class::File | IO::File | FH | ScalarRef
173 [, Attachment filename [, MIME type ]]
174 )
175  
176 See L<Accessors> for more details.
177  
178 =head2 Public Methods
179  
180 =head3 serialize
181  
182 Returns the attachment as a L<MIME::Entity> object.
183  
184 =head3 filename
185  
186 If possible, returns the filename of the attachment file as a
187 L<Path::Class::File> object.
188  
189 =head3 filecontent
190  
191 Returns the content of the attachment file.
192  
193 =head3 filehandle
194  
195 If possible, returns a filehandle for the attachment file as a
196 L<IO::File> object.
197  
198 =head2 Accessors
199  
200 =head3 name
201  
202 Name of the attachment as used in the e-mail message. If no name is provided
203 the current filename will be used.
204  
205 =head3 mimetype
206  
207 Mime type of the attachment.
208  
209 If not provided the mime type is determined by analyzing the filename
210 extension.
211  
212 =head3 file
213  
214 Attachment file. Can be a
215  
216 =over
217  
218 =item * Path (or a Path::Class::File object)
219  
220 =item * Filehandle (or an IO::File object)
221  
222 =item * ScalarRef containing the attachment data
223  
224 =back
225  
226 =head1 AUTHOR
227  
228 Maroš Kollár
229 CPAN ID: MAROS
230 maros [at] k-1.com
231 http://www.k-1.com
232  
233 =cut
« Builder/Image.pm ↑ Summary Builder/Utils.pm »