Redirect/Recipients.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 87.5% 21 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 95.7% 89 / 93 SCAR 28.4
CC
17
Cov
95%
BUILDARGS
7.0
_build_excludes_regexps
6.1
replace
6.0
CRAP 17.0
↑ Summary Redirect.pm »
1 package Email::Sender::Transport::Redirect::Recipients;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Email::Valid;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Types::Standard qw/ArrayRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Email::Sender::Transport::Redirect::Recipients - handle email address redirect replacements
12  
13 =head1 SYNOPSIS
14  
15 This is a class used internally by
16 L<Email::Sender::Transport::Redirect> and shouldn't be used directly.
17  
18 my $rec = Email::Sender::Transport::Redirect::Recipients->new($string_or_hashref);
19  
20 print $rec->to;
21 print Dumper($rec->exclude);
22 print $rec->replace('myemail@example');
23  
24 =head1 CONSTRUCTOR
25  
26 =head2 BUILDARGS
27  
28 =head2 new($string_or_hashref)
29  
30 Either a single email as string, or an hashref which are used to
31 initialize the accessors (see above). If a string is provided, then
32 just C<to> will be set and no exclusions are set.
33  
34 =head1 ACCESSORS
35  
36 =head2 to
37  
38 The main, required email address to use as a redirect.
39  
40 =head2 exclude
41  
42 An arrayref of emails or wildcard expressions. E.g.
43  
44 [ 'mail@example.org', '*@example.org', 'racke@*' ]
45  
46 These emails will not get redirected.
47  
48 =head1 METHODS
49  
50 =head2 replace($string)
51  
52 Main method. When a string is passed, it's checked against the
53 exclusion list. If there is a match, the address passed as argument
54 will be returned, otherwise the C<to> address set in the object will
55 be returned.
56  
57 =cut
58  
59  
60 4 has to => (is => 'ro', isa => Str, required => 1);
61 2 ▶has exclude => (is => 'ro', isa => ArrayRef[Str], default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
62  
63 sub BUILDARGS {
64 9 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=7 SCAR=19.5
Pod documented
65 9 ▶ die "Only one argument is supported!" unless @args == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 1 8 9
66 8 my $arg = shift @args;
67 8 ▶ if (my $kind = ref($arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $kind = ref $arg) { } 6 2 8
68 6 ▶ if ($kind eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "HASH") 5 1 6
69 5 my %hash = %$arg;
70 5 foreach my $k (keys %hash) {
71 11 ▶ die "Extra argument $k" unless $k eq 'to' || $k eq 'exclude';
Branch: 1 branch
BranchTrueFalseTotal
unless $k eq "to" or $k eq "exclude" 1 10 11
Condition100%3/3
$k eq "to" or $k eq "exclude"
l!l&&r!l&&!r
551
MC/DC100%2/2
$k eq "to" or $k eq "exclude"
$k eq "to" $k eq "exclude"
Truth table100%3/3
A or B
A= $k eq "to"B= $k eq "exclude"
ABresult
1X1
011
000
72 }
73 4 return \%hash;
74 }
75 1 die "Argument must be an hashref with to and exclude keys, you passed a $kind";
76 }
77 else {
78 2 return { to => $arg };
79 }
80 }
81  
82 4 has excludes_regexps => (is => 'lazy', isa => ArrayRef);
83  
84 sub _build_excludes_regexps {
85 5 ▶ my $self = shift;
Subroutine _build_excludes_regexps: called CC=6 SCAR=18.1
86 5 my @out;
87 5 foreach my $exclusion (@{$self->exclude}) {
88 6 ▶ if ($exclusion =~ m/\*/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exclusion =~ /\*/) { } 3 3 6
elsif (my $address = 'Email::Valid'->address($exclusion)) { } 3 0 3
89 3 my $re = $exclusion;
90 # http://blogs.perl.org/users/mauke/2015/08/converting-glob-patterns-to-regular-expressions.html
91 3 $re =~ s{(\W)}{
92 9 ▶ $1 eq '?' ? '.' :
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '?' ? : 0 9 9
$1 eq '*' ? : 3 6 9
93 $1 eq '*' ? '.*' :
94 '\\' . $1
95 }eg;
96 3 push @out, qr{$re};
97 }
98 elsif (my $address = Email::Valid->address($exclusion)) {
99 3 push @out, qr{\Q$address\E};
100 }
101 else {
102 0 die "Exclusion contains an invalid string: $exclusion, nor a wildcard, nor a valid address: $exclusion";
103 }
104 }
105 5 return \@out;
106 }
107  
108  
109  
110 sub replace {
111 21 ▶ my ($self, $mail) = @_;
Subroutine replace: called CC=6 SCAR=17.9
Pod documented
112 21 ▶ if ($mail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mail) 20 1 21
113 20 ▶ if (my @exclusions = @{$self->excludes_regexps}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@exclusions) = @{$self->excludes_regexps;}) 18 2 20
114 # an alternate approach could be Email::Address to allow multiple addresses
115 18 ▶ if (my $address = Email::Valid->address($mail)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $address = "Email::Valid"->address($mail)) 18 0 18
116 18 my $real = $address . ''; # stringify
117 18 foreach my $re (@exclusions) {
118 # print "Checking $real against $re\n";
119 24 ▶ if ($real =~ m/\A$re\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($real =~ /\A$re\z/) 12 12 24
120 # print "Found, returning $real\n";
121 12 return $real;
122 }
123 }
124 }
125 }
126 }
127 # fall back
128 # print "Falling back\n";
129 9 return $self->to;
130 }
131  
132 4 1;
↑ Summary Redirect.pm »