TLSRPT/Report.pm untested

filter: Statementstmt 0.0% 0 / 44 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 65 CC 1 SCAR 6.9
CC
1
Cov
0%
new_from_json
2.0
new_from_data
2.0
as_string
2.0
CRAP 2.0
1 package Mail::TLSRPT::Report;
2 # ABSTRACT: TLSRPT report object
3 our $VERSION = '1.20200303.1'; # VERSION
4 use 5.20.0;
5 use Moo;
6 use Carp;
7 use Types::Standard qw{Str HashRef ArrayRef};
8 use Type::Utils qw{class_type};
9 use Mail::TLSRPT::Pragmas;
10 use Mail::TLSRPT::Policy;
11 use DateTime;
12 use Date::Parse qw{ str2time };
13 has organization_name => (is => 'rw', isa => Str);
14 has start_datetime => (is => 'rw', isa => class_type('DateTime'));
15 has end_datetime => (is => 'rw', isa => class_type('DateTime'));
16 has contact_info => (is => 'rw', isa => Str);
17 has report_id => (is => 'rw', isa => Str);
18 has policies => (is => 'rw', isa => ArrayRef);
19  
20 sub new_from_json($class,$json) {
21 my $j = JSON->new;
22 my $data = $j->decode($json);
23 return $class->new_from_data($data);
24 }
25  
26 sub new_from_data($class,$data) {
27 my @policies;
28 foreach my $policy ( $data->{policies}->@* ) {
29 push @policies, Mail::TLSRPT::Policy->new_from_data($policy);
30 }
31 my $self = $class->new(
32 organization_name => $data->{'organization-name'} // '',
33 start_datetime => DateTime->from_epoch(epoch => str2time($data->{'date-range'}->{'start-datetime'})),
34 end_datetime => DateTime->from_epoch(epoch => str2time($data->{'date-range'}->{'end-datetime'})),
35 contact_info => $data->{'contact-info'} // '',
36 report_id => $data->{'report-id'} // '',
37 policies => \@policies,
38 );
39 return $self;
40 }
41  
42 sub as_string($self) {
43 return join( "\n",
44 'Report-ID: <'.$self->report_id.'>',
45 'From: "'.$self->organization_name.'" <'.$self->contact_info.'>',
46 'Dates: '.$self->start_datetime->datetime.' to '.$self->end_datetime->datetime,
47 map { $_->as_string } $self->policies->@*,
48 );
49 }
50  
51 1;
52