File Coverage

blib/lib/Mail/TLSRPT/Policy.pm
Criterion Covered Total %
statement 43 43 100.0
branch 2 2 100.0
condition 6 8 75.0
subroutine 11 11 100.0
pod 0 3 0.0
total 62 67 92.5


line stmt bran cond sub pod time code
1             package Mail::TLSRPT::Policy;
2             # ABSTRACT: TLSRPT policy object
3             our $VERSION = '1.20200306.1'; # VERSION
4 7     7   1766 use 5.20.0;
  7         27  
5 7     7   56 use Moo;
  7         15  
  7         54  
6 7     7   2226 use Carp;
  7         26  
  7         381  
7 7     7   48 use Mail::TLSRPT::Pragmas;
  7         15  
  7         39  
8 7     7   5030 use Mail::TLSRPT::Failure;
  7         23  
  7         5279  
9             has policy_type => (is => 'rw', isa => Enum[qw( tlsa sts no-policy-found )], required => 1);
10             has policy_string => (is => 'rw', isa => ArrayRef, required => 1);
11             has policy_domain => (is => 'rw', isa => Str, required => 1);
12             has policy_mx_host => (is => 'rw', isa => Str, required => 1);
13             has total_successful_session_count => (is => 'rw', isa => Int, required => 1);
14             has total_failure_session_count => (is => 'rw', isa => Int, required => 1);
15 1     1   12332 has failures => (is => 'rw', isa => ArrayRef, required => 0, lazy => 1, builder => sub{return []} );
16              
17 6     6 0 5953 sub new_from_data($class,$data) {
  6         14  
  6         12  
  6         11  
18 6         12 my @failures;
19 6         22 foreach my $failure ( $data->{'failure-details'}->@* ) {
20 9         101 push @failures, Mail::TLSRPT::Failure->new_from_data($failure);
21             }
22             my $self = $class->new(
23             policy_type => $data->{policy}->{'policy-type'},
24             policy_string => $data->{policy}->{'policy-string'} // [],
25             policy_domain => $data->{policy}->{'policy-domain'},
26             policy_mx_host => $data->{policy}->{'mx-host'} // '',
27             total_successful_session_count => $data->{summary}->{'total-successful-session-count'} // 0,
28 6   100     171 total_failure_session_count => $data->{summary}->{'total-failure-session-count'} // 0,
      100        
      50        
      50        
29             failures => \@failures,
30             );
31 6         19893 return $self;
32             }
33              
34 4     4 0 806 sub as_struct($self) {
  4         8  
  4         8  
35 4         80 my @failures = map {$_->as_struct} $self->failures->@*;
  3         381  
36             return {
37 4 100       271 policy => {
38             'policy-type' => $self->policy_type,
39             'policy-string' => $self->policy_string,
40             'policy-domain' => $self->policy_domain,
41             'mx-host' => $self->policy_mx_host,
42             },
43             summary => {
44             'total-successful-session-count' => $self->total_successful_session_count,
45             'total-failure-session-count' => $self->total_failure_session_count,
46             },
47             scalar $self->failures->@* ? ( 'failure_details' => \@failures ) : (),
48             };
49             }
50              
51 3     3 0 5857 sub as_string($self) {
  3         8  
  3         6  
52             return join( "\n",
53             'Policy:',
54             ' Type: '.$self->policy_type,
55             ' String: '. join('; ',$self->policy_string->@*),
56             ' Domain: '.$self->policy_domain,
57             ' MX-Host: '.$self->policy_mx_host,
58             ' Successful-Session-Count: '.$self->total_successful_session_count,
59             ' Failure-Session-Count: '.$self->total_failure_session_count,
60 3         72 map { $_->as_string } $self->failures->@*,
  3         475  
61             );
62             }
63              
64 1     1   2 sub _csv_headers($self) {
  1         1  
  1         2  
65             return (
66 1         6 'policy type',
67             'policy string',
68             'policy domain',
69             'policy mx host',
70             'total successful session count',
71             'total failure session count',
72             );
73             }
74              
75 3     3   381 sub _csv_fragment($self) {
  3         5  
  3         3  
76             return (
77 3         47 $self->policy_type,
78             join('; ',$self->policy_string->@*),
79             $self->policy_domain,
80             $self->policy_mx_host,
81             $self->total_successful_session_count,
82             $self->total_failure_session_count,
83             );
84             }
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Mail::TLSRPT::Policy - TLSRPT policy object
97              
98             =head1 VERSION
99              
100             version 1.20200306.1
101              
102             =head1 AUTHOR
103              
104             Marc Bradshaw <marc@marcbradshaw.net>
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             This software is copyright (c) 2020 by Marc Bradshaw.
109              
110             This is free software; you can redistribute it and/or modify it under
111             the same terms as the Perl 5 programming language system itself.
112              
113             =cut