lib/Mail/DKIM2/MessageStore.pm

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