File Coverage

blib/lib/App/SpamcupNG/Error/Factory.pm
Criterion Covered Total %
statement 34 34 100.0
branch 10 12 83.3
condition 2 5 40.0
subroutine 8 8 100.0
pod 1 1 100.0
total 55 60 91.6


line stmt bran cond sub pod time code
1             package App::SpamcupNG::Error::Factory;
2 12     12   96 use strict;
  12         28  
  12         401  
3 12     12   80 use warnings;
  12         29  
  12         333  
4 12     12   62 use Exporter 'import';
  12         42  
  12         317  
5              
6 12     12   4928 use App::SpamcupNG::Error;
  12         113  
  12         380  
7 12     12   6308 use App::SpamcupNG::Error::Mailhost;
  12         38  
  12         368  
8 12     12   5006 use App::SpamcupNG::Error::Bounce;
  12         33  
  12         370  
9 12     12   5010 use App::SpamcupNG::Error::LoginFailed;
  12         31  
  12         4087  
10              
11             our $VERSION = '0.017'; # VERSION
12              
13             =head1 NAME
14              
15             App::SpamcupNG::Error::Factory - factory design pattern to create new instances
16             of errors parsed from Spamcop website HTML.
17              
18             =head1 SYNOPSIS
19              
20             use App::SpamcupNG::Error::Factory qw(create_error);
21              
22             =head1 DESCRIPTION
23              
24             =head1 EXPORTS
25              
26             The function C<create_error> is the only things exported by this module.
27              
28             =cut
29              
30             our @EXPORT_OK = qw(create_error);
31              
32             my $mailhost_regex = qr/Mailhost\sconfiguration\sproblem/;
33             my $bounce_regex = qr/bounce/;
34             my $login_failed_regex = qr/^Login\sfailed/;
35             my @fatal_errors = ( qr/email\sis\stoo\sold/, qr/^Nothing/ );
36              
37             =head1 FUNCTIONS
38              
39             =head2 create_error
40              
41             Creates new error from a given message string.
42              
43             The type of error is identified from this message.
44              
45             Expects as parameters:
46              
47             - an array reference where each index is a string line from the original error
48             message.
49              
50             - a integer, being 0 if the error message is not fatal, 1 otherwise.
51              
52             Returns an instance of App::SpamcupNG::Error or one of it's subclasses.
53              
54             =cut
55              
56             sub create_error {
57 6     6 1 13 my ( $message_ref, $is_fatal ) = @_;
58 6   50     30 $is_fatal //= 0;
59              
60             die 'message must be an no empty array reference'
61             unless ( ( ref($message_ref) eq 'ARRAY' )
62 6 50 33     18 and ( scalar( @{$message_ref} ) > 0 ) );
  6         19  
63              
64 6 100       45 return App::SpamcupNG::Error::Mailhost->new($message_ref)
65             if ( $message_ref->[0] =~ $mailhost_regex );
66              
67 5 100       44 return App::SpamcupNG::Error::Bounce->new( $message_ref, 1 )
68             if ( $message_ref->[0] =~ $bounce_regex );
69              
70 4 100       45 return App::SpamcupNG::Error::LoginFailed->new($message_ref)
71             if ( $message_ref->[0] =~ $login_failed_regex );
72              
73 3 50       21 return App::SpamcupNG::Error->new( $message_ref, $is_fatal )
74             if ($is_fatal);
75              
76 3         8 foreach my $regex (@fatal_errors) {
77 6 100       21 if ( $message_ref->[0] =~ $regex ) {
78 1         3 $is_fatal = 1;
79 1         2 last;
80             }
81             }
82              
83 3         15 return App::SpamcupNG::Error->new( $message_ref, $is_fatal );
84             }
85              
86             =head1 AUTHOR
87              
88             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
89              
90             =head1 COPYRIGHT AND LICENSE
91              
92             This software is copyright (c) 2018 of Alceu Rodrigues de Freitas Junior,
93             E<lt>arfreitas@cpan.orgE<gt>
94              
95             This file is part of App-SpamcupNG distribution.
96              
97             App-SpamcupNG is free software: you can redistribute it and/or modify it under
98             the terms of the GNU General Public License as published by the Free Software
99             Foundation, either version 3 of the License, or (at your option) any later
100             version.
101              
102             App-SpamcupNG is distributed in the hope that it will be useful, but WITHOUT
103             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
104             FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
105              
106             You should have received a copy of the GNU General Public License along with
107             App-SpamcupNG. If not, see <http://www.gnu.org/licenses/>.
108              
109             =cut
110              
111             1;
112