lib/Mail/Toaster/Base.pm

filter: Statementstmt 80.4% 111 / 138 Branchbran 62.5% 45 / 72 Conditioncond 62.5% 5 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 78.2% 18 / 23 Podpod 0.0% 0 / 19 totaltotal 68.6% 182 / 265 SCAR 43.0
CC
42
Cov
74%
log
30.0
error
8.4
dump_audit
7.4
CRAP 73.5
« blib/lib/Mail/Toaster/Darwin.pm ↑ Summary lib/Mail/Toaster/DNS.pm »
1 package Mail::Toaster::Base;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '5.50';
6  
7 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 our $verbose = our $last_audit = our $last_error = 0; # package variables
11 14 our (@audit, @errors); # package wide message stacks
12 14 our ($conf, $log);
13 14 our ($darwin, $dns, $freebsd, $qmail, $logs, $mysql, $setup, $toaster, $util);
14  
15 14 our %std_opts = (
16 test_ok => { type => BOOLEAN, optional => 1 },
17 verbose => { type => BOOLEAN, optional => 1, default => $verbose },
18 fatal => { type => BOOLEAN, optional => 1, default => 1 },
19 );
20  
21 sub new {
22 32 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 32 my %p = validate( @_, { %std_opts } );
24 32 my @caller = caller;
25 # warn sprintf( "Base.pm loaded by %s, %s, %s\n", @caller ) if $caller[0] ne 'main';
26 32 return bless {}, $class;
27 }
28  
29 sub darwin {
30 0 ▶ my $self = shift;
Subroutine darwin: not called CC=2 SCAR=17.9
Pod undocumented
31 0 ▶ return $darwin if ref $darwin;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::darwin 0 0 0
32 0 require Mail::Toaster::Darwin;
33 0 return $darwin = Mail::Toaster::Darwin->new();
34 }
35  
36 sub dns {
37 3 ▶ my $self = shift;
Subroutine dns: called CC=2 SCAR=6.9
Pod undocumented
38 3 ▶ return $dns if ref $dns;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::dns 2 1 3
39 1 require Mail::Toaster::DNS;
40 1 return $dns = Mail::Toaster::DNS->new();
41 }
42  
43 sub freebsd {
44 0 ▶ my $self = shift;
Subroutine freebsd: not called CC=2 SCAR=17.9
Pod undocumented
45 0 ▶ return $freebsd if ref $freebsd;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::freebsd 0 0 0
46 0 require Mail::Toaster::FreeBSD;
47 0 return $freebsd = Mail::Toaster::FreeBSD->new();
48 }
49  
50 sub logs {
51 0 ▶ my $self = shift;
Subroutine logs: not called CC=2 SCAR=17.9
Pod undocumented
52 0 ▶ return $logs if ref $logs;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::logs 0 0 0
53 0 require Mail::Toaster::Logs;
54 0 return $logs = Mail::Toaster::Logs->new();
55 }
56  
57 sub mysql {
58 0 ▶ my $self = shift;
Subroutine mysql: not called CC=2 SCAR=17.9
Pod undocumented
59 0 ▶ return $mysql if ref $mysql;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::mysql 0 0 0
60 0 require Mail::Toaster::Mysql;
61 0 return $mysql = Mail::Toaster::Mysql->new();
62 }
63  
64 sub qmail {
65 2 ▶ my $self = shift;
Subroutine qmail: called CC=2 SCAR=7.0
Pod undocumented
66 2 ▶ return $qmail if ref $qmail;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::qmail 0 2 2
67 2 require Mail::Toaster::Qmail;
68 2 return $qmail = Mail::Toaster::Qmail->new();
69 }
70  
71 sub setup {
72 3 ▶ my $self = shift;
Subroutine setup: called CC=2 SCAR=6.9
Pod undocumented
73 3 ▶ return $setup if ref $setup;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::setup 1 2 3
74 2 require Mail::Toaster::Setup;
75 2 return $setup = Mail::Toaster::Setup->new();
76 }
77  
78 sub toaster {
79 13 ▶ my $self = shift;
Subroutine toaster: called CC=2 SCAR=6.9
Pod undocumented
80 13 ▶ return $toaster if ref $toaster;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::toaster 12 1 13
81 1 require Mail::Toaster;
82 1 return $toaster = Mail::Toaster->new();
83 }
84  
85 sub util {
86 32 ▶ my $self = shift;
Subroutine util: called CC=2 SCAR=6.9
Pod undocumented
87 32 ▶ return $util if ref $util;
Branch: 1 branch
BranchTrueFalseTotal
if ref $Mail::Toaster::Base::util 23 9 32
88 9 require Mail::Toaster::Utility;
89 9 return $util = Mail::Toaster::Utility->new();
90 }
91  
92 sub verbose {
93 60 ▶ return $verbose if 1 == scalar @_;
Subroutine verbose: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if 1 == scalar @_ 58 2 60
94 2 return $verbose = $std_opts{verbose}{default} = $_[1];
95 };
96  
97 sub conf {
98 193 ▶ $conf = $_[1] if $_[1];
Subroutine conf: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 9 184 193
99 193 ▶ return $conf if $conf;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Toaster::Base::conf 188 5 193
100 5 $conf = $_[0]->util->parse_config( "toaster-watcher.conf" );
101 };
102  
103 sub audit {
104 362 ▶ my $self = shift;
Subroutine audit: called CC=4 SCAR=13.9
Pod undocumented
105 362 my $mess = shift;
106  
107 362 my %p = validate( @_, { %std_opts } );
108  
109 362 ▶ if ($mess) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mess) 360 2 362
110 360 push @audit, $mess;
111 360 ▶ print "$mess\n" if $verbose || $p{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Toaster::Base::verbose or $p{"verbose"} 2 358 360
Condition100%3/3
$Mail::Toaster::Base::verbose or $p{"verbose"}
l!l&&r!l&&!r
11358
MC/DC100%2/2
$Mail::Toaster::Base::verbose or $p{"verbose"}
$Mail::Toaster::Base::verbose $p{"verbose"}
Truth table100%3/3
A or B
A= $Mail::Toaster::Base::verboseB= $p{"verbose"}
ABresult
1X1
011
000
112 }
113  
114 362 return \@audit;
115 }
116  
117 sub dump_audit {
118 10 ▶ my $self = shift;
Subroutine dump_audit: called CC=7 SCAR=20.1
Pod undocumented
119 10 my %p = validate( @_, {
120 quiet => { type => BOOLEAN, optional => 1, default => 0 },
121 %std_opts,
122 }
123 );
124  
125 10 ▶ if ( 0 == scalar @audit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == scalar @Mail::Toaster::Base::audit) 0 10 10
126 0 ▶ print "dump_audit: no audit messages\n" if $p{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"verbose"} 0 0 0
127 0 return 1;
128 };
129  
130 10 ▶ if ( $last_audit == scalar @audit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Toaster::Base::last_audit == scalar @Mail::Toaster::Base::audit) 1 9 10
131 1 ▶ print "dump_audit: all messages dumped\n" if $p{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"verbose"} 1 0 1
132 1 return 1;
133 };
134  
135 9 ▶ if ( $p{quiet} ) { # hide/mask unreported messages
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"quiet"}) 7 2 9
136 7 $last_audit = scalar @audit;
137 7 $last_error = scalar @errors;
138 7 return 1;
139 };
140  
141 2 print "\n\t\t\tAudit History Report \n\n";
142 2 ▶ for( my $i = $last_audit; $i < scalar @audit; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @Mail::Toaster::Base::audit) 2 2 4
143 2 print " $audit[$i]\n";
144 2 $last_audit++;
145 };
146 2 return 1;
147 };
148  
149 sub error {
150 56 ▶ my $self = shift;
Subroutine error: called CC=8 SCAR=21.2
Pod undocumented
151 56 ▶ my $message = shift or carp "why call error w/o message?";
Branch: 1 branch
BranchTrueFalseTotal
my $message = shift() or carp("why call error w/o message?") 0 56 56
152 56 my %p = validate( @_,
153 { location => { type => SCALAR, optional => 1 },
154 frames => { type => SCALAR, optional => 1, default => 0 },
155 %std_opts,
156 },
157 );
158  
159 56 ▶ if ( $message ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message) { } 56 0 56
160 # append message and location to the error stack
161 56 my @call = caller $p{frames};
162 56 my $location = $p{location};
163 56 ▶ if ( ! $location && scalar @call ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $location and scalar @call) 46 10 56
Condition100%2/2
not $location and scalar @call
!ll
1046
MC/DC100%1/1
not $location and scalar @call
not $location
Truth table100%2/2
not(A)
A= not $location
Aresult
00
11
164 46 $location = join( ', ', $call[0], $call[2] );
165 };
166 56 push @errors, { errmsg => $message, errloc => $location };
167 }
168 else {
169 0 $message = $errors[-1];
170 }
171  
172 56 ▶ $self->dump_audit if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 1 55 56
173 56 ▶ $self->dump_errors if $p{fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"fatal"} 0 56 56
174  
175 56 ▶ exit 1 if $p{fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"fatal"} 0 56 56
176 56 return;
177 }
178  
179 sub dump_errors {
180 3 ▶ my $self = shift;
Subroutine dump_errors: called CC=7 SCAR=19.5
Pod undocumented
181  
182 3 ▶ if ( $last_error == scalar @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Mail::Toaster::Base::last_error == scalar @Mail::Toaster::Base::errors) 2 1 3
183 2 ▶ print "all error messages dumped!\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Toaster::Base::verbose 0 2 2
184 2 return 1;
185 };
186  
187 1 print "\n\t\t\t Error History Report \n\n";
188 1 my $i = 0;
189 1 foreach ( @errors ) {
190 1 $i++;
191 1 ▶ next if $i < $last_error;
Branch: 1 branch
BranchTrueFalseTotal
if $i < $Mail::Toaster::Base::last_error 0 1 1
192 1 my $msg = $_->{errmsg};
193 1 ▶ my $loc = $_->{errloc} ? " at $_->{errloc}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$_->{'errloc'} ? : 1 0 1
194 1 print $msg;
195 1 ▶ for (my $j=length($msg); $j < 90-length($loc); $j++) { print '.'; };
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 90 - length($loc)) 66 1 67
196 1 print " $loc\n";
197 };
198 1 print "\n";
199 1 $last_error = $i;
200 1 return 1;
201 };
202  
203 sub get_std_args {
204 183 ▶ my $self = shift;
Subroutine get_std_args: called CC=4 SCAR=14.0
Pod undocumented
205 183 my %p = @_;
206 183 my %args;
207 183 foreach ( qw/ verbose fatal test_ok / ) {
208 549 ▶ if ( defined $p{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p{$_}) 374 175 549
209 374 $args{$_} = $p{$_};
210 374 next;
211 };
212 175 ▶ if ( $self->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$_}) 0 175 175
213 0 $args{$_} = $self->{$_};
214 };
215 };
216 183 return %args;
217 };
218  
219 317 ▶sub get_std_opts { return %std_opts };
Subroutine get_std_opts: called CC=1 SCAR=0.0
Pod undocumented
220  
221 sub log {
222 0 ▶ my $self = shift;
Subroutine log: not called CC=5 SCAR=34.0
Pod undocumented
223 0 ▶ my $mess = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $mess = shift() or return 0 0 0
224  
225 0 ▶ my $logfile = $conf->{'toaster_watcher_log'} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $logfile = $Mail::Toaster::Base::conf->{"toaster_watcher_log"} or do { warn "ERROR: no log file defined!\n"; return } 0 0 0
226 0 warn "ERROR: no log file defined!\n";
227 0 return;
228 };
229 0 ▶ return if ( -e $logfile && ! -w $logfile );
Branch: 1 branch
BranchTrueFalseTotal
if -e $logfile and not -w $logfile 0 0 0
Condition0%0/3
-e $logfile and not -w $logfile
!ll&&!rl&&r
000
MC/DC0%0/2
-e $logfile and not -w $logfile
-e $logfile not -w $logfile
Truth table0%0/3
A and not(B)
A= -e $logfileB= not -w $logfile
ABresult
0X0
100
111
230  
231 0 $self->util->logfile_append( $logfile, lines => [$mess], fatal => 0 );
232 };
233  
234 14 1;
« blib/lib/Mail/Toaster/Darwin.pm ↑ Summary lib/Mail/Toaster/DNS.pm »