Tasks/SMTPGateway.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 144 CC 14 SCAR 29.8
CC
14
Cov
0%
_deliver_message
42.0
_tick
30.0
message
20.0
CRAP 19.7
1 package App::MultiModule::Tasks::SMTPGateway;
2 $App::MultiModule::Tasks::SMTPGateway::VERSION = '1.162230';
3 use 5.006;
4 use strict;
5 use warnings FATAL => 'all';
6 use Data::Dumper;
7 use Message::Transform qw(mtransform);
8 use Storable;
9 use Email::Stuffer;
10  
11 use parent 'App::MultiModule::Task';
12  
13 =head1 NAME
14  
15 App::MultiModule::Tasks::SMTPGateway - Task to allow message driven e-mail
16  
17 =cut
18  
19 =head2 message
20  
21 =cut
22  
23 sub message {
24 my $self = shift;
25 my $message = shift;
26 my $config = $self->{config};
27 my %args = @_;
28 $self->debug('message', message => $message)
29 if $self->{debug} > 5;
30 my $state = $self->{state};
31 for ('smtp_to','smtp_from','smtp_subject','smtp_body') {
32 if(not $message->{$_}) {
33 $self->error("App::MultiModule::Tasks::SMTPGateway::message: submitted message requires field '$_'");
34 return undef;
35 }
36 }
37 my $deliver_ttl = $config->{deliver_ttl} || 5;
38 $message->{'_smtp_gateway_ttl'} = $deliver_ttl
39 unless $message->{'_smtp_gateway_ttl'};
40 push @{$state->{messages}}, $message;
41 }
42  
43 sub _deliver_message {
44 my $self = shift;
45 my $config = $self->{config};
46 my $message = shift;
47 my @to;
48 if(ref $message->{smtp_to} and ref $message->{smtp_to} eq 'ARRAY') {
49 @to = @{$message->{smtp_to}};
50 } elsif(not ref $message->{smtp_to}) {
51 $to[0] = $message->{smtp_to};
52 }
53 my @errs;
54 foreach my $to (@to) {
55 eval {
56 my $send = Email::Stuffer->from($message->{smtp_from})
57 ->to($to)
58 ->subject($message->{smtp_subject})
59 ->text_body($message->{smtp_body});
60 $send->transport(@{$message->{smtp_transport}})
61 if $message->{smtp_transport};
62 $send->send_or_die;
63 };
64 push @errs, $@ if $@;
65 }
66 if(scalar @errs) {
67 my $errs = join "\n", @errs;
68 die $errs;
69 }
70 }
71  
72 sub _tick {
73 my $self = shift;
74 my $config = $self->{config};
75 my $tick_timeout = $config->{tick_timeout} || 5;
76 my $state = $self->{state};
77 my $message;
78 eval {
79 local $SIG{ALRM} = sub { die "timed out\n"; };
80 alarm $tick_timeout;
81 while($message = shift(@{$state->{messages}})) {
82 $message->{'_smtp_gateway_ttl'}--;
83 if(not $message->{'_smtp_gateway_ttl'}) {
84 $self->error('App::MultiModule::Tasks::SMTPGateway::_tick: message dropped because after multiple delivery attempts', message => $message);
85 next;
86 }
87 die 'configured to test exceptions'
88 if $config->{test_exceptions};
89 $self->_deliver_message($message);
90 }
91 undef $message;
92 };
93 alarm 0;
94 if($@) {
95 $self->error("App::MultiModule::Tasks::SMTPGateway::_tick failed: $@",
96 message => $message);
97 }
98 unshift @{$state->{messages}}, $message if $message;
99 }
100  
101 =head2 set_config
102  
103 =cut
104 sub set_config {
105 my $self = shift;
106 my $config = shift;
107 $self->{config} = $config;
108 my $state = $self->{state};
109 $state->{messages} = [] unless $state->{messages};
110 $self->named_recur(
111 recur_name => 'SMTPGateway_tick',
112 repeat_interval => 1,
113 work => sub {
114 $self->_tick,
115 },
116 );
117 }
118  
119 =head2 is_stateful
120  
121 =cut
122 sub is_stateful {
123 return 'yes';
124 }
125  
126 =head1 AUTHOR
127  
128 Dana M. Diederich, C<< <dana@realms.org> >>
129  
130 =head1 BUGS
131  
132 Please report any bugs or feature requests through L<https://github.com/dana/perl-App-MultiModule-Tasks-SMTPGateway/issues>. I will be notified, and then you'll
133 automatically be notified of progress on your bug as I make changes.
134  
135 =head1 SUPPORT
136  
137 You can find documentation for this module with the perldoc command.
138  
139 perldoc App::MultiModule::Tasks::SMTPGateway
140  
141  
142 You can also look for information at:
143  
144 =over 4
145  
146 =item * Report bugs here:
147  
148 L<https://github.com/dana/perl-App-MultiModule-Tasks-SMTPGateway/issues>
149  
150 =item * AnnoCPAN: Annotated CPAN documentation
151  
152 L<http://annocpan.org/dist/App-MultiModule-Tasks-SMTPGateway>
153  
154 =item * CPAN Ratings
155  
156 L<http://cpanratings.perl.org/d/App-MultiModule-Tasks-SMTPGateway>
157  
158 =item * Search CPAN
159  
160 L<https://metacpan.org/module/App::MultiModule::Tasks::SMTPGateway>
161  
162 =back
163  
164 =head1 ACKNOWLEDGEMENTS
165  
166 =head1 LICENSE AND COPYRIGHT
167  
168 Copyright 2016 Dana M. Diederich.
169  
170 This program is free software; you can redistribute it and/or modify it
171 under the terms of the the Artistic License (2.0). You may obtain a
172 copy of the full license at:
173  
174 L<http://www.perlfoundation.org/artistic_license_2_0>
175  
176 Any use, modification, and distribution of the Standard or Modified
177 Versions is governed by this Artistic License. By using, modifying or
178 distributing the Package, you accept this license. Do not use, modify,
179 or distribute the Package, if you do not accept this license.
180  
181 If your Modified Version has been derived from a Modified Version made
182 by someone other than you, you are nevertheless required to ensure that
183 your Modified Version complies with the requirements of this license.
184  
185 This license does not grant you the right to use any trademark, service
186 mark, tradename, or logo of the Copyright Holder.
187  
188 This license includes the non-exclusive, worldwide, free-of-charge
189 patent license to make, have made, use, offer to sell, sell, import and
190 otherwise transfer the Package with respect to any patent claims
191 licensable by the Copyright Holder that are necessarily infringed by the
192 Package. If you institute patent litigation (including a cross-claim or
193 counterclaim) against any party alleging that the Package constitutes
194 direct or contributory patent infringement, then this Artistic License
195 to you shall terminate on the date that such litigation is filed.
196  
197 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
198 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
199 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
200 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
201 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
202 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
203 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
204 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205  
206  
207 =cut
208  
209 1; # End of App::MultiModule::Tasks::SMTPGateway