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