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