Kit/Assembler/Standard.pm

filter: Statementstmt 95.2% 161 / 169 Branchbran 69.7% 53 / 76 Conditioncond 76.9% 20 / 26 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 26 / 26 Podpod 0.0% 0 / 4 totaltotal 84.3% 265 / 314 CC 56 SCAR 18.1
CC
56
Cov
86%
assemble
10.6
_contain_attachments
10.0
_setup_content_ids
7.8
CRAP 6.1
↑ Summary Kit/Role/ManifestDesugarer.pm »
1 package Email::MIME::Kit::Assembler::Standard 3.000007;
2 # ABSTRACT: the standard kit assembler
3  
4 4 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #pod =head1 WARNING
9 #pod
10 #pod Email::MIME::Kit::Assembler::Standard works well, but is poorly decomposed,
11 #pod internally. Its methods may change substantially in the future, so relying on
12 #pod it as a base class is a bad idea.
13 #pod
14 #pod Because I<being able to> rely on it would be so useful, its behaviors will in
15 #pod the future be more reliable or factored out into roles. Until then, be
16 #pod careful.
17 #pod
18 #pod =cut
19  
20 4 with 'Email::MIME::Kit::Role::Assembler';
21  
22 4 ▶use Email::MIME::Creator;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 sub BUILD {
27 18 ▶ my ($self) = @_;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
28 18 $self->_setup_content_ids;
29 18 $self->_pick_and_set_renderer;
30 18 $self->_build_subassemblies;
31 }
32  
33 4 has parent => (
34 is => 'ro',
35 isa => maybe_type(role_type('Email::MIME::Kit::Role::Assembler')),
36 weak_ref => 1,
37 );
38  
39 4 has renderer => (
40 reader => 'renderer',
41 writer => '_set_renderer',
42 clearer => '_unset_renderer',
43 isa => maybe_type(role_type('Email::MIME::Kit::Role::Renderer')),
44 init_arg => undef,
45 );
46  
47 sub assemble {
48 20 ▶ my ($self, $stash) = @_;
Subroutine assemble: called CC=10 SCAR=23.6
Pod undocumented
49  
50 20 my $manifest = $self->manifest;
51  
52 20 my $has_body = defined $manifest->{body};
53 20 my $has_path = defined $manifest->{path};
54 20 ▶ my $has_alts = @{ $manifest->{alternatives} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$manifest->{"alternatives"} or [] 0 20 20
55 20 ▶ my $has_att = @{ $manifest->{attachments} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$manifest->{"attachments"} or [] 0 20 20
56  
57 20 ▶ Carp::croak("neither body, path, nor alternatives provided")
Branch: 1 branch
BranchTrueFalseTotal
unless $has_body or $has_path or $has_alts 0 20 20
Condition83%5/6
$has_body or $has_path or $has_alts
l!l&&r!l&&!r
1730
$has_body or $has_path
l!l&&r!l&&!r
7103
MC/DC0%0/3
$has_body or $has_path or $has_alts
$has_body $has_path $has_alts
Truth table75%3/4
A or B or C
A= $has_bodyB= $has_pathC= $has_alts
ABCresult
1XX1
01X1
0011
0000
58 unless $has_body or $has_path or $has_alts;
59  
60 Carp::croak("you must provide only one of body, path, or alternatives")
61 20 ▶ unless (grep {$_} $has_body, $has_path, $has_alts) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless grep({$_;} $has_body, $has_path, $has_alts) == 1 0 20 20
62  
63 20 ▶ my $assembly_method = $has_body ? '_assemble_from_manifest_body'
Branch: 3 branches
BranchTrueFalseTotal
$has_body ? : 7 13 20
$has_path ? : 10 3 13
$has_alts ? : 3 0 3
64 : $has_path ? '_assemble_from_kit'
65 : $has_alts ? '_assemble_mp_alt'
66 : confess "unreachable code is a mistake";
67  
68 20 $self->$assembly_method($stash);
69 }
70  
71 sub _assemble_from_string {
72 17 ▶ my ($self, $body, $stash) = @_;
Subroutine _assemble_from_string: called CC=5 SCAR=16.1
73  
74 17 ▶ my %attr = %{ $self->manifest->{attributes} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$self->manifest->{"attributes"} or {} 3 14 17
75 17 ▶ $attr{content_type} ||= 'text/plain';
Condition100%2/2
$attr{"content_type"} ||= "text/plain"
l!l
125
MC/DC100%1/1
$attr{"content_type"} ||= "text/plain"
$attr{"content_type"}
Truth table100%2/2
A
A= $attr{"content_type"}
Aresult
11
00
76  
77 17 ▶ if ($attr{content_type} =~ m{^text/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr{"content_type"} =~ m[^text/]u) 13 4 17
78 # I really shouldn't have to do this, but I'm not going to go screw around
79 # with @#$@#$ Email::Simple/MIME just to deal with it right now. -- rjbs,
80 # 2009-01-19
81 13 ▶ $body .= "\x0d\x0a" unless $body =~ /[\x0d|\x0a]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $body =~ /[\x0d|\x0a]\z/u 8 5 13
82 }
83  
84 17 my $body_ref = $self->render(\$body, $stash);
85  
86 my $email = $self->_contain_attachments({
87 attributes => \%attr,
88 header => $self->manifest->{header},
89 stash => $stash,
90 body => $$body_ref,
91 container_type => $self->manifest->{container_type},
92 17 });
93 }
94  
95 sub _assemble_from_manifest_body {
96 7 ▶ my ($self, $stash) = @_;
Subroutine _assemble_from_manifest_body: called CC=1 SCAR=0.0
97  
98 $self->_assemble_from_string(
99 $self->manifest->{body},
100 7 $stash,
101 );
102 }
103  
104 sub _assemble_from_kit {
105 10 ▶ my ($self, $stash) = @_;
Subroutine _assemble_from_kit: called CC=3 SCAR=11.0
106  
107 10 ▶ my $type = $self->manifest->{attributes}{content_type} || 'text/plain';
Condition100%2/2
$self->manifest->{'attributes'}{'content_type'} || 'text/plain'
l!l
82
MC/DC100%1/1
$self->manifest->{'attributes'}{'content_type'} || 'text/plain'
$self->manifest->{'attributes'}{'content_type'}
Truth table100%2/2
A
A= $self->manifest->{'attributes'}{'content_type'}
Aresult
11
00
108 10 ▶ my $method = $type =~ m{^text/} ? 'get_decoded_kit_entry' : 'get_kit_entry';
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^text/]u ? : 6 4 10
109  
110 10 my $body_ref = $self->kit->$method($self->manifest->{path});
111  
112 10 $self->_assemble_from_string($$body_ref, $stash);
113 }
114  
115 sub _assemble_mp_alt {
116 3 ▶ my ($self, $stash) = @_;
Subroutine _assemble_mp_alt: called CC=4 SCAR=14.5
117  
118 3 ▶ my %attr = %{ $self->manifest->{attributes} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$self->manifest->{"attributes"} or {} 3 0 3
119 3 ▶ $attr{content_type} = $attr{content_type} || 'multipart/alternative';
Condition50%1/2
$attr{'content_type'} || 'multipart/alternative'
l!l
03
MC/DC0%0/1
$attr{'content_type'} || 'multipart/alternative'
$attr{'content_type'}
Truth table50%1/2
A
A= $attr{'content_type'}
Aresult
11
00
120  
121 3 ▶ if ($attr{content_type} !~ qr{\Amultipart/alternative\b}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr{"content_type"} =~ qr"\Amultipart/alternative\b"u) 0 3 3
122 0 confess "illegal content_type for mail with alts: $attr{content_type}";
123 }
124  
125 3 my $parts = [ map { $_->assemble($stash) } @{ $self->_alternatives } ];
126  
127 my $email = $self->_contain_attachments({
128 attributes => \%attr,
129 header => $self->manifest->{header},
130 3 stash => $stash,
131 parts => $parts,
132 });
133 }
134  
135 sub _renderer_from_override {
136 8 ▶ my ($self, $override) = @_;
Subroutine _renderer_from_override: called CC=2 SCAR=8.1
137  
138 # Allow an explicit undef to mean "no rendering is to be done." -- rjbs,
139 # 2009-01-19
140 8 ▶ return undef unless defined $override;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $override 8 0 8
141  
142 0 return $self->kit->_build_component(
143 'Email::MIME::Kit::Renderer',
144 $override,
145 );
146 }
147  
148 sub _pick_and_set_renderer {
149 18 ▶ my ($self) = @_;
Subroutine _pick_and_set_renderer: called CC=3 SCAR=11.0
150  
151 # "renderer" entry at top-level sets the kit default_renderer, so trying to
152 # look at the "renderer" entry at top-level for an override is nonsensical
153 # -- rjbs, 2009-01-22
154 18 ▶ unless ($self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parent) 6 12 18
155 6 $self->_set_renderer($self->kit->default_renderer);
156 6 return;
157 }
158  
159 # If there's no override, we just use the parent. We don't need to worry
160 # about the "there is no parent" case, because that was handled above. --
161 # rjbs, 2009-01-22
162 12 ▶ unless (exists $self->manifest->{renderer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->manifest->{"renderer"}) 10 2 12
163 10 $self->_set_renderer($self->parent->renderer);
164 10 return;
165 }
166  
167 2 my $renderer = $self->_renderer_from_override($self->manifest->{renderer});
168 2 $self->_set_renderer($renderer);
169 }
170  
171 4 has manifest => (
172 is => 'ro',
173 required => 1,
174 );
175  
176 has [ qw(_attachments _alternatives) ] => (
177 is => 'ro',
178 isa => 'ArrayRef',
179 init_arg => undef,
180 36 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
181 4 );
182  
183 4 has _body => (
184 reader => 'body',
185 writer => '_set_body',
186 );
187  
188 sub _build_subassemblies {
189 18 ▶ my ($self) = @_;
Subroutine _build_subassemblies: called CC=7 SCAR=19.7
190  
191 18 ▶ if (my $body = $self->manifest->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $body = $self->manifest->{"body"}) 6 12 18
192 6 $self->_set_body($body);
193 }
194  
195 18 ▶ for my $attach (@{ $self->manifest->{attachments} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
$self->manifest->{"attachments"} or [] 0 18 18
196 4 my $assembler = $self->kit->_assembler_from_manifest($attach, $self);
197 4 ▶ $assembler->_set_attachment_info($attach)
Branch: 1 branch
BranchTrueFalseTotal
if $assembler->can("_set_attachment_info") 4 0 4
198 if $assembler->can('_set_attachment_info');
199 4 push @{ $self->_attachments }, $assembler;
200 }
201  
202 18 ▶ for my $alt (@{ $self->manifest->{alternatives} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
$self->manifest->{"alternatives"} or [] 0 18 18
203 8 push @{ $self->_alternatives },
204 $self->kit->_assembler_from_manifest($alt, $self);
205 }
206 }
207  
208 sub _set_attachment_info {
209 4 ▶ my ($self, $manifest) = @_;
Subroutine _set_attachment_info: called CC=7 SCAR=20.4
210  
211 4 ▶ my $attr = $manifest->{attributes} ||= {};
Condition50%1/2
$manifest->{"attributes"} ||= {}
l!l
40
MC/DC0%0/1
$manifest->{"attributes"} ||= {}
$manifest->{"attributes"}
Truth table50%1/2
A
A= $manifest->{"attributes"}
Aresult
11
00
212  
213 4 ▶ $attr->{encoding} = 'base64' unless exists $attr->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"encoding"} 4 0 4
214 4 ▶ $attr->{disposition} = 'attachment' unless exists $attr->{disposition};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"disposition"} 4 0 4
215  
216 4 ▶ unless (exists $attr->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $attr->{"filename"}) 2 2 4
217 2 my $filename;
218 ($filename) = File::Basename::fileparse($manifest->{path})
219 2 ▶ if $manifest->{path};
Branch: 1 branch
BranchTrueFalseTotal
if $manifest->{"path"} 2 0 2
220  
221 # XXX: Steal the attachment-name-generator from Email::MIME::Modifier, or
222 # something. -- rjbs, 2009-01-20
223 2 ▶ $filename ||= "unknown-attachment";
Condition50%1/2
$filename ||= "unknown-attachment"
l!l
20
MC/DC0%0/1
$filename ||= "unknown-attachment"
$filename
Truth table50%1/2
A
A= $filename
Aresult
11
00
224  
225 2 $attr->{filename} = $filename;
226 }
227 }
228  
229 sub render {
230 17 ▶ my ($self, $input_ref, $stash) = @_;
Subroutine render: called CC=2 SCAR=6.9
Pod undocumented
231 17 ▶ local $stash->{cid_for} = sub { $self->cid_for_path($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
232 17 ▶ return $input_ref unless my $renderer = $self->renderer;
Branch: 1 branch
BranchTrueFalseTotal
unless my $renderer = $self->renderer 5 12 17
233 12 return $renderer->render($input_ref, $stash);
234 }
235  
236 sub _prep_header {
237 20 ▶ my ($self, $header, $stash) = @_;
Subroutine _prep_header: called CC=7 SCAR=20.3
238  
239 20 my @done_header;
240 20 for my $entry (@$header) {
241 confess "no field name candidates"
242 24 ▶ unless my (@hval) = grep { /^[^:]/ } keys %$entry;
Branch: 1 branch
BranchTrueFalseTotal
unless my(@hval) = grep({/^[^:]/u;} keys %$entry) 0 24 24
243 24 ▶ confess "multiple field name candidates: @hval" if @hval > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @hval > 1 0 24 24
244 24 my $value = $entry->{ $hval[ 0 ] };
245  
246 24 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 24 24
247 0 my ($v, $p) = @$value;
248 0 $value = join q{; }, $v, map { "$_=$p->{$_}" } keys %$p;
249 } else {
250 # I don't think I need to bother with $self->render, which will set up
251 # the cid_for callback. Honestly, who is going to be referencing a
252 # content-id from a header? Let's hope I never find out... -- rjbs,
253 # 2009-01-22
254 my $renderer = exists $entry->{':renderer'}
255 24 ▶ ? $self->_renderer_from_override($entry->{':renderer'})
Branch: 1 branch
BranchTrueFalseTotal
exists $entry->{':renderer'} ? : 6 18 24
256 : $self->renderer;
257  
258 24 ▶ $value = ${ $renderer->render(\$value, $stash) } if defined $renderer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $renderer 12 12 24
259 }
260  
261 24 push @done_header, $hval[0] => $value;
262 }
263  
264 20 return \@done_header;
265 }
266  
267 sub _contain_attachments {
268 20 ▶ my ($self, $arg) = @_;
Subroutine _contain_attachments: called CC=10 SCAR=23.1
269  
270 20 my @attachments = @{ $self->_attachments };
271 20 my $header = $self->_prep_header($arg->{header}, $arg->{stash});
272  
273 20 my $ct = $arg->{container_type};
274  
275 20 my %attr = %{ $arg->{attributes} };
276 20 my $body_type = 'body';
277  
278 20 ▶ if ($attr{content_type} =~ m{^text/}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attr{'content_type'} =~ m[^text/]u) { } 13 7 20
elsif (($arg->{'body'} || '') =~ /\P{ASCII}/u) { } 4 3 7
279 13 $body_type = 'body_str';
280  
281 13 ▶ $attr{encoding} ||= 'quoted-printable';
Condition50%1/2
$attr{"encoding"} ||= "quoted-printable"
l!l
013
MC/DC0%0/1
$attr{"encoding"} ||= "quoted-printable"
$attr{"encoding"}
Truth table50%1/2
A
A= $attr{"encoding"}
Aresult
11
00
282 13 ▶ $attr{charset} ||= 'UTF-8'
Condition100%2/2
$attr{"charset"} ||= "UTF-8"
l!l
49
MC/DC100%1/1
$attr{"charset"} ||= "UTF-8"
$attr{"charset"}
Truth table100%2/2
A
A= $attr{"charset"}
Aresult
11
00
283 ▶ } elsif (($arg->{body} || '') =~ /\P{ASCII}/) {
Condition100%2/2
$arg->{'body'} || ''
l!l
43
MC/DC100%1/1
$arg->{'body'} || ''
$arg->{'body'}
Truth table100%2/2
A
A= $arg->{'body'}
Aresult
11
00
284 4 ▶ $attr{encoding} ||= 'base64';
Condition50%1/2
$attr{"encoding"} ||= "base64"
l!l
40
MC/DC0%0/1
$attr{"encoding"} ||= "base64"
$attr{"encoding"}
Truth table50%1/2
A
A= $attr{"encoding"}
Aresult
11
00
285 }
286  
287 20 ▶ unless (@attachments) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@attachments) 16 4 20
288 16 ▶ confess "container_type given for single-part assembly" if $ct;
Branch: 1 branch
BranchTrueFalseTotal
if $ct 0 16 16
289  
290 return Email::MIME->create(
291 attributes => \%attr,
292 header_str => $header,
293 $body_type => $arg->{body},
294 parts => $arg->{parts},
295 16 );
296 }
297  
298 my $email = Email::MIME->create(
299 attributes => \%attr,
300 $body_type => $arg->{body},
301 parts => $arg->{parts},
302 4 );
303  
304 4 my @att_parts = map { $_->assemble($arg->{stash}) } @attachments;
305  
306 4 ▶ my $container = Email::MIME->create(
Condition100%2/2
$ct || 'multipart/mixed'
l!l
22
MC/DC100%1/1
$ct || 'multipart/mixed'
$ct
Truth table100%2/2
A
A= $ct
Aresult
11
00
307 attributes => { content_type => ($ct || 'multipart/mixed') },
308 header_str => $header,
309 parts => [ $email, @att_parts ],
310 );
311  
312 4 return $container;
313 }
314  
315 has _cid_registry => (
316 is => 'ro',
317 init_arg => undef,
318 18 ▶ default => sub { { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
319 4 );
320  
321 sub cid_for_path {
322 2 ▶ my ($self, $path) = @_;
Subroutine cid_for_path: called CC=2 SCAR=7.0
Pod undocumented
323 2 my $cid = $self->_cid_registry->{ $path };
324  
325 2 ▶ confess "no content-id for path $path" unless $cid;
Branch: 1 branch
BranchTrueFalseTotal
unless $cid 0 2 2
326  
327 2 return $cid;
328 }
329  
330 sub _setup_content_ids {
331 18 ▶ my ($self) = @_;
Subroutine _setup_content_ids: called CC=6 SCAR=20.5
332  
333 18 ▶ for my $att (@{ $self->manifest->{attachments} || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
$self->manifest->{"attachments"} or [] 0 18 18
334 4 ▶ next unless $att->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless $att->{"path"} 0 4 4
335  
336 4 for my $header (@{ $att->{header} }) {
337 0 my ($header) = grep { /^[^:]/ } keys %$header;
338 0 ▶ Carp::croak("attachments must not supply content-id")
Branch: 1 branch
BranchTrueFalseTotal
if lc $header eq "content-id" 0 0 0
339 if lc $header eq 'content-id';
340 }
341  
342 4 my $cid = $self->kit->_generate_content_id;
343 4 push @{ $att->{header} }, {
344 'Content-Id' => $cid->in_brackets,
345 ':renderer' => undef,
346 };
347  
348 4 $self->_cid_registry->{ $att->{path} } = $cid->as_string;
349 }
350 }
351  
352 4 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
353 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
354 4 __PACKAGE__->meta->make_immutable;
355 4 1;
356  
357 __END__
358  
359 =pod
360  
361 =encoding UTF-8
362  
363 =head1 NAME
364  
365 Email::MIME::Kit::Assembler::Standard - the standard kit assembler
366  
367 =head1 VERSION
368  
369 version 3.000007
370  
371 =head1 PERL VERSION
372  
373 This library should run on perls released even a long time ago. It should work
374 on any version of perl released in the last five years.
375  
376 Although it may work on older versions of perl, no guarantee is made that the
377 minimum required version will not be increased. The version may be increased
378 for any reason, and there is no promise that patches will be accepted to lower
379 the minimum required perl.
380  
381 =head1 WARNING
382  
383 Email::MIME::Kit::Assembler::Standard works well, but is poorly decomposed,
384 internally. Its methods may change substantially in the future, so relying on
385 it as a base class is a bad idea.
386  
387 Because I<being able to> rely on it would be so useful, its behaviors will in
388 the future be more reliable or factored out into roles. Until then, be
389 careful.
390  
391 =head1 AUTHOR
392  
393 Ricardo Signes <rjbs@cpan.org>
394  
395 =head1 COPYRIGHT AND LICENSE
396  
397 This software is copyright (c) 2023 by Ricardo Signes.
398  
399 This is free software; you can redistribute it and/or modify it under
400 the same terms as the Perl 5 programming language system itself.
401  
402 =cut
↑ Summary Kit/Role/ManifestDesugarer.pm »