Sietima/Role/Headers.pm

filter: Statementstmt 100% 44 / 44 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 96.7% 60 / 62 CC 7 SCAR 9.2
CC
7
Cov
96%
_add_headers_to
5.0
_normalise_address
2.0
__ANON__
2.0
CRAP 2.5
« Sietima/MailStore/FS.pm ↑ Summary Sietima/Role/SubscriberOnly/Moderate.pm »
1 package Sietima::Role::Headers;
2 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Sietima::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Sietima::HeaderURI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Types::Standard qw(Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Sietima::Types qw(HeaderUriFromThings);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '1.1.5'; # VERSION
10 # ABSTRACT: adds standard list-related headers to messages
11  
12  
13 2 has name => (
14 isa => Str,
15 is => 'ro',
16 required => 0,
17 );
18  
19 10 ▶sub _normalise_address($self,$address) {
Subroutine _normalise_address: called CC=2 SCAR=6.9
20 10 ▶ my @items = ref($address) eq 'ARRAY' ? $address->@* : $address;
Branch: 1 branch
BranchTrueFalseTotal
ref $address eq 'ARRAY' ? : 1 9 10
21  
22 return map {
23 10 HeaderUriFromThings->coerce($_)
24 } @items;
25 }
26  
27 10 ▶sub _set_header($self,$mail,$name,$value) {
Subroutine _set_header: called CC=1 SCAR=0.0
28 10 my $header_name = 'List-' . ucfirst($name =~ s{[^[:alnum:]]+}{-}gr);
29 10 my @items = $self->_normalise_address($value);
30  
31 $mail->header_raw_set(
32 10 $header_name => join ', ', map { $_->as_header_raw } @items,
33 );
34 }
35  
36 3 ▶sub _add_headers_to($self,$message) {
Subroutine _add_headers_to: called CC=5 SCAR=16.2
37 3 my $addresses = $self->list_addresses;
38 3 my $mail = $message->mail;
39  
40 # see RFC 2919 "List-Id: A Structured Field and Namespace for the
41 # Identification of Mailing Lists"
42 3 my $return_path = delete $addresses->{return_path};
43 3 ▶ if (my $name = $self->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $self->name) 3 0 3
44 3 $mail->header_raw_set(
45 'List-Id',
46 sprintf '%s <%s>', $name,$return_path->address =~ s{\@}{.}r,
47 );
48 }
49  
50 # if nobody declared a "post" address, let's guess it's the same
51 # as the address we send from
52 3 ▶ if (not exists $addresses->{post}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $addresses->{'post'}) { } 2 1 3
elsif (not $addresses->{'post'}) { } 1 0 1
53 2 $self->_set_header( $mail, post => $return_path );
54 }
55 # but if they explicitly set a false value, this list does not
56 # allow posting, so we need to set the special value 'NO'
57 elsif (not $addresses->{post}) {
58 1 delete $addresses->{post};
59 1 $mail->header_raw_set('List-Post','NO');
60 }
61 # otherwise we can treat 'post' as normal
62  
63 3 for my $name (sort keys $addresses->%*) {
64 8 $self->_set_header( $mail, $name => $addresses->{$name} );
65 }
66 3 return;
67 }
68  
69  
70 3 ▶around munge_mail => sub ($orig,$self,$mail) {
Subroutine __ANON__: called CC=2 SCAR=6.9
71 3 my @messages = $self->$orig($mail);
72 3 $self->_add_headers_to($_) for @messages;
73 3 return @messages;
74 2 };
75  
76 2 1;
77  
78 __END__
« Sietima/MailStore/FS.pm ↑ Summary Sietima/Role/SubscriberOnly/Moderate.pm »