lib/Mail/DKIM2/MessageStore.pm

filter: Statementstmt 89.8% 62 / 69 Branchbran 40.9% 9 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 100% 5 / 5 totaltotal 80.9% 89 / 110 CC 12 SCAR 16.9
CC
12
Cov
78%
remove
20.0
store
5.2
fetch
4.0
CRAP 5.4
« lib/Mail/DKIM2/Digest.pm ↑ Summary lib/Mail/DKIM2/Util.pm »
1 package Mail::DKIM2::MessageStore;
2 81 ▶use 5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 81 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 81 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 81 ▶use Mail::DKIM2::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 81 our $VERSION = '0.36';
8  
9 81 ▶use Crypt::Digest::SHA256 qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 81 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 81 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 93 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
14 93 my ($class, %args) = @_;
15 93 Mail::DKIM2::Util::check_options("$class->new", \%args, qw(directory));
16 92 ▶ croak "directory required" unless $args{directory};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"directory"} 0 92 92
17 92 my $self = bless \%args, $class;
18 92 return $self;
19 }
20  
21 # Derive a filesystem-safe key from an MI header value
22 73 ▶sub _key_for_mi {
Subroutine _key_for_mi: called CC=1 SCAR=0.0
23 73 my ($self, $mi_value) = @_;
24 # Canonicalize: unfold continuation lines and collapse whitespace
25 # so that folded and unfolded forms hash identically.
26 73 $mi_value =~ s/\r?\n[ \t]/ /g;
27 73 $mi_value =~ s/\s+/ /g;
28 73 $mi_value =~ s/^\s+//;
29 73 $mi_value =~ s/\s+$//;
30 73 return sha256_hex($mi_value);
31 }
32  
33 # Path with 2-char prefix subdirectory to avoid crowding
34 49 ▶sub _path_for_key {
Subroutine _path_for_key: called CC=1 SCAR=0.0
35 49 my ($self, $key) = @_;
36 49 my $prefix = substr($key, 0, 2);
37 49 return "$self->{directory}/$prefix/$key";
38 }
39  
40 31 ▶sub store {
Subroutine store: called CC=5 SCAR=16.5
Pod documented
41 31 my ($self, $mi_value, $message_data) = @_;
42 31 ▶ croak "mi_value required" unless defined $mi_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mi_value 0 31 31
43 31 ▶ croak "message_data required" unless defined $message_data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message_data 0 31 31
44  
45 31 my $key = $self->_key_for_mi($mi_value);
46 31 my $path = $self->_path_for_key($key);
47  
48 31 my $dir = $path;
49 31 $dir =~ s{/[^/]+$}{};
50 31 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 31 0 31
51  
52 31 ▶ open my $fh, '>:raw', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $path or croak("Cannot write ${path}: $!") 31 0 31
53 or croak "Cannot write $path: $!";
54 31 print $fh $message_data;
55 31 close $fh;
56  
57 31 return $key;
58 }
59  
60 18 ▶sub fetch {
Subroutine fetch: called CC=4 SCAR=13.9
Pod documented
61 18 my ($self, $mi_value) = @_;
62 18 ▶ croak "mi_value required" unless defined $mi_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mi_value 0 18 18
63  
64 18 my $key = $self->_key_for_mi($mi_value);
65 18 my $path = $self->_path_for_key($key);
66  
67 18 ▶ return unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 2 16 18
68  
69 16 ▶ open my $fh, '<:raw', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or croak("Cannot read ${path}: $!") 16 0 16
70 or croak "Cannot read $path: $!";
71 16 local $/;
72 16 my $data = <$fh>;
73 16 close $fh;
74  
75 16 return $data;
76 }
77  
78 24 ▶sub rel_path_for_mi {
Subroutine rel_path_for_mi: called CC=1 SCAR=0.0
Pod documented
79 24 my ($self, $mi_value) = @_;
80 24 my $key = $self->_key_for_mi($mi_value);
81 24 my $prefix = substr($key, 0, 2);
82 24 return "$prefix/$key";
83 }
84  
85 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
86 0 my ($self, $mi_value) = @_;
87 0 ▶ croak "mi_value required" unless defined $mi_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mi_value 0 0 0
88  
89 0 my $key = $self->_key_for_mi($mi_value);
90 0 my $path = $self->_path_for_key($key);
91  
92 0 ▶ return unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 0 0 0
93 0 ▶ unlink $path or croak "Cannot remove $path: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or croak("Cannot remove ${path}: $!") 0 0 0
94 0 return 1;
95 }
96  
97 81 1;
98  
99 __END__
« lib/Mail/DKIM2/Digest.pm ↑ Summary lib/Mail/DKIM2/Util.pm »