blib/lib/Log/Log4perl/Appender/AmazonSES.pm

filter: Statementstmt 46.1% 36 / 78 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 71.4% 10 / 14 Podpod 33.3% 1 / 3 totaltotal 29.1% 47 / 161 CC 20 SCAR 39.1
CC
20
Cov
29%
new
110.0
init_auth
56.0
log
30.0
CRAP 50.1
↑ Summary
1 package Log::Log4perl::Appender::AmazonSES;
2  
3 1 ▶use parent qw(Log::Log4perl::Appender);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use List::Util qw(any pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::Domain 'hostfqdn';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Net::SMTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 Readonly::Scalar our $TRUE => 1;
18 1 Readonly::Scalar our $FALSE => 0;
19  
20 1 Readonly::Scalar our $DEFAULT_HOST => 'email-smtp.us-east-1.amazonaws.com';
21 1 Readonly::Scalar our $DEFAULT_PORT => '465';
22  
23 1 our $VERSION = '1.0.1';
24  
25 ########################################################################
26 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=47.0
Pod documented
27 ########################################################################
28 0 my ( $class, %options ) = @_;
29  
30 0 my $host = delete $options{Host};
31 0 ▶ $host //= delete $options{host};
Condition0%0/3
$host //= delete $options{"host"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$host //= delete $options{"host"}
$host delete $options{"host"}
Truth table0%0/3
A //= B
A= $hostB= delete $options{"host"}
ABresult
1X1
011
000
32  
33 0 ▶ $host //= $DEFAULT_HOST;
Condition0%0/3
$host //= $Log::Log4perl::Appender::AmazonSES::DEFAULT_HOST
l!l&&r!l&&!r
000
MC/DC0%0/2
$host //= $Log::Log4perl::Appender::AmazonSES::DEFAULT_HOST
$host $Log::Log4perl::Appender::AmazonSES::DEFAULT_HOST
Truth table0%0/3
A //= B
A= $hostB= $Log::Log4perl::Appender::AmazonSES::DEFAULT_HOST
ABresult
1X1
011
000
34  
35 0 ▶ $options{Port} //= delete $options{port};
Condition0%0/3
$options{"Port"} //= delete $options{"port"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"Port"} //= delete $options{"port"}
$options{"Port"} delete $options{"port"}
Truth table0%0/3
A //= B
A= $options{"Port"}B= delete $options{"port"}
ABresult
1X1
011
000
36 0 ▶ $options{Port} //= $DEFAULT_PORT;
Condition0%0/3
$options{"Port"} //= $Log::Log4perl::Appender::AmazonSES::DEFAULT_PORT
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"Port"} //= $Log::Log4perl::Appender::AmazonSES::DEFAULT_PORT
$options{"Port"} $Log::Log4perl::Appender::AmazonSES::DEFAULT_PORT
Truth table0%0/3
A //= B
A= $options{"Port"}B= $Log::Log4perl::Appender::AmazonSES::DEFAULT_PORT
ABresult
1X1
011
000
37  
38 0 ▶ $options{Hello} //= delete $options{domain};
Condition0%0/3
$options{"Hello"} //= delete $options{"domain"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"Hello"} //= delete $options{"domain"}
$options{"Hello"} delete $options{"domain"}
Truth table0%0/3
A //= B
A= $options{"Hello"}B= delete $options{"domain"}
ABresult
1X1
011
000
39 0 ▶ $options{Hello} //= hostfqdn;
Condition0%0/3
$options{"Hello"} //= hostfqdn()
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"Hello"} //= hostfqdn()
$options{"Hello"} hostfqdn()
Truth table0%0/3
A //= B
A= $options{"Hello"}B= hostfqdn()
ABresult
1X1
011
000
40  
41 0 my $from = delete $options{from};
42 0 my $to = delete $options{to};
43 0 my $subject = delete $options{subject};
44  
45 0 foreach my $p ( pairs( from => $from, to => $to, subject => $subject ) ) {
46 0 ▶ croak $p->[0] . ' is a required parameter'
Branch: 1 branch
BranchTrueFalseTotal
unless $p->[1] 0 0 0
47 if !$p->[1];
48 }
49  
50 0 my $auth = eval { init_auth( delete $options{auth} ); };
51  
52 0 $options{SSL} = $TRUE;
53 0 ▶ $options{Debug} //= delete $options{debug};
Condition0%0/3
$options{"Debug"} //= delete $options{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"Debug"} //= delete $options{"debug"}
$options{"Debug"} delete $options{"debug"}
Truth table0%0/3
A //= B
A= $options{"Debug"}B= delete $options{"debug"}
ABresult
1X1
011
000
54  
55 0 my $self = bless {
56 host => $host,
57 auth => $auth,
58 to => $to,
59 from => $from,
60 subject => $subject,
61 options => \%options
62 }, $class;
63  
64 0 return $self;
65 }
66  
67 ########################################################################
68 0 ▶sub init_auth {
Subroutine init_auth: not called CC=7 SCAR=40.3
Pod undocumented
69 ########################################################################
70 0 my ($auth) = @_;
71  
72 0 ▶ if ( !$auth || !ref $auth ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth and ref $auth) 0 0 0
Condition0%0/3
$auth and ref $auth
!ll&&!rl&&r
000
MC/DC0%0/2
$auth and ref $auth
$auth ref $auth
Truth table0%0/3
A and B
A= $authB= ref $auth
ABresult
0X0
100
111
73 0 ▶ if ( $ENV{SES_SMTP_USER} && $ENV{SES_SMTP_PASS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"SES_SMTP_USER"} and $ENV{"SES_SMTP_PASS"}) 0 0 0
Condition0%0/3
$ENV{"SES_SMTP_USER"} and $ENV{"SES_SMTP_PASS"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"SES_SMTP_USER"} and $ENV{"SES_SMTP_PASS"}
$ENV{"SES_SMTP_USER"} $ENV{"SES_SMTP_PASS"}
Truth table0%0/3
A and B
A= $ENV{"SES_SMTP_USER"}B= $ENV{"SES_SMTP_PASS"}
ABresult
0X0
100
111
74 $auth = {
75 user => $ENV{SES_SMTP_USER},
76 password => $ENV{SES_SMTP_PASS},
77 0 };
78 }
79 }
80  
81 croak sprintf 'auth.user and auth.password are required parameters'
82 0 ▶ if !$auth->{user} || !$auth->{password};
Branch: 1 branch
BranchTrueFalseTotal
unless $auth->{"user"} and $auth->{"password"} 0 0 0
Condition0%0/3
$auth->{"user"} and $auth->{"password"}
!ll&&!rl&&r
000
MC/DC0%0/2
$auth->{"user"} and $auth->{"password"}
$auth->{"user"} $auth->{"password"}
Truth table0%0/3
A and B
A= $auth->{"user"}B= $auth->{"password"}
ABresult
0X0
100
111
83  
84 0 return $auth;
85 }
86  
87 ########################################################################
88 0 ▶sub log { ## no critic
Subroutine log: not called CC=5 SCAR=34.0
Pod undocumented
89 ########################################################################
90 0 my ( $self, %params ) = @_;
91  
92 0 ▶ my $smtp = Net::SMTP->new( $self->{host}, %{ $self->{options} }, )
Branch: 1 branch
BranchTrueFalseTotal
my $smtp = "Net::SMTP"->new($self->{"host"}, %{$$self{"options"};}) or croak("ERROR: unable to create a Net::SMTP instance") 0 0 0
93 or croak 'ERROR: unable to create a Net::SMTP instance';
94  
95 0 ▶ if ( any { $self->{options}->{Port} eq $_ } qw(25 587) ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $self->{"options"}{"Port"} eq $_; } , 25, 587)) 0 0 0
96 $smtp->starttls()
97 0 ▶ or croak sprintf 'TLS negotiation with %s failed', $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
$smtp->starttls or croak(sprintf('TLS negotiation with %s failed', $self->{"host"})) 0 0 0
98 }
99  
100 0 my $auth = $self->{auth};
101  
102 0 $smtp->auth( $auth->{user}, $auth->{password} );
103  
104 0 $smtp->mail( $self->{from} );
105 0 $smtp->to( split /\s*,\s*/xsm, $self->{to} );
106  
107 0 $smtp->data;
108  
109 0 $smtp->datasend( sprintf "From: %s\n", $self->{from} );
110 0 $smtp->datasend( sprintf "To: %s\n", $self->{to} );
111 0 $smtp->datasend( sprintf "Subject: %s\n", $self->{subject} );
112 0 $smtp->datasend( sprintf "\n%s\n", $params{message} );
113  
114 0 ▶ $smtp->dataend
Branch: 1 branch
BranchTrueFalseTotal
$smtp->dataend or carp("ERROR: could not send message") 0 0 0
115 or carp 'ERROR: could not send message';
116  
117 0 $smtp->quit;
118 }
119  
120 1 1;
121  
122 __END__
↑ Summary