blib/lib/Email/Sender/Transport/SQLite.pm

filter: Statementstmt 97.9% 48 / 49 Branchbran 75.0% 3 / 4 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 88.8% 8 / 9 Podpod 33.3% 1 / 3 totaltotal 90.4% 66 / 73 CC 7 SCAR 8.8
CC
7
Cov
95%
dbh
4.0
retrieve_deliveries
3.0
__ANON__
2.0
CRAP 2.4
↑ Summary
1 package Email::Sender::Transport::SQLite 1.501;
2 # ABSTRACT: deliver mail to an sqlite db for testing
3  
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 with 'Email::Sender::Transport';
6  
7 #pod =head1 DESCRIPTION
8 #pod
9 #pod This transport makes deliveries to an SQLite database, creating it if needed.
10 #pod The SQLite transport is intended for testing programs that fork or that
11 #pod otherwise can't use the Test transport. It is not meant for robust, long-term
12 #pod storage of mail.
13 #pod
14 #pod The database will be created in the file named by the C<db_file> attribute,
15 #pod which defaults to F<email.db>.
16 #pod
17 #pod The database will have two tables:
18 #pod
19 #pod CREATE TABLE emails (
20 #pod id INTEGER PRIMARY KEY,
21 #pod body varchar NOT NULL,
22 #pod env_from varchar NOT NULL
23 #pod );
24 #pod
25 #pod CREATE TABLE recipients (
26 #pod id INTEGER PRIMARY KEY,
27 #pod email_id integer NOT NULL,
28 #pod env_to varchar NOT NULL
29 #pod );
30 #pod
31 #pod Each delivery will insert one row to the F<emails> table and one row per
32 #pod recipient to the F<recipients> table.
33 #pod
34 #pod Delivery to this transport should never fail.
35 #pod
36 #pod =cut
37  
38 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1 has _dbh => (
41 is => 'rw',
42 init_arg => undef,
43 );
44  
45 has _dbh_pid => (
46 is => 'rw',
47 init_arg => undef,
48 1 ▶ default => sub { $$ },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 1 );
50  
51 3 ▶sub dbh {
Subroutine dbh: called CC=4 SCAR=13.9
Pod undocumented
52 3 my ($self) = @_;
53  
54 ## no critic Punctuation
55 3 my $existing_dbh = $self->_dbh;
56  
57 3 ▶ return $existing_dbh if $existing_dbh and $self->_dbh_pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $existing_dbh and $self->_dbh_pid == $$ 2 1 3
Condition66%2/3
$existing_dbh and $self->_dbh_pid == $$
!ll&&!rl&&r
102
MC/DC50%1/2
$existing_dbh and $self->_dbh_pid == $$
$existing_dbh $self->_dbh_pid == $$
Truth table66%2/3
A and B
A= $existing_dbhB= $self->_dbh_pid == $$
ABresult
0X0
100
111
58  
59 1 my $must_setup = ! -e $self->db_file;
60 1 my $dbh = DBI->connect("dbi:SQLite:dbname=" . $self->db_file);
61  
62 1 $self->_dbh($dbh);
63 1 $self->_dbh_pid($$);
64 1 ▶ $self->_setup_dbh if $must_setup;
Branch: 1 branch
BranchTrueFalseTotal
if $must_setup 1 0 1
65  
66 1 return $dbh;
67 }
68  
69 has db_file => (
70 is => 'ro',
71 0 ▶ default => sub { 'email.db' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 1 );
73  
74 1 ▶sub _setup_dbh {
Subroutine _setup_dbh: called CC=1 SCAR=0.0
75 1 my ($self) = @_;
76 1 my $dbh = $self->_dbh;
77  
78 1 $dbh->do('
79 CREATE TABLE emails (
80 id INTEGER PRIMARY KEY,
81 body varchar NOT NULL,
82 env_from varchar NOT NULL
83 );
84 ');
85  
86 1 $dbh->do('
87 CREATE TABLE recipients (
88 id INTEGER PRIMARY KEY,
89 email_id integer NOT NULL,
90 env_to varchar NOT NULL
91 );
92 ');
93 }
94  
95 2 ▶sub send_email {
Subroutine send_email: called CC=2 SCAR=6.9
Pod undocumented
96 2 my ($self, $email, $env) = @_;
97  
98 2 my $message = $email->as_string;
99 2 my $to = $env->{to};
100 2 my $from = $env->{from};
101  
102 2 my $dbh = $self->dbh;
103  
104 2 $dbh->do(
105 "INSERT INTO emails (body, env_from) VALUES (?, ?)",
106 undef,
107 $message,
108 $from,
109 );
110  
111 2 my $id = $dbh->last_insert_id((undef) x 4);
112  
113 2 for my $addr (@$to) {
114 3 $dbh->do(
115 "INSERT INTO recipients (email_id, env_to) VALUES (?, ?)",
116 undef,
117 $id,
118 $addr,
119 );
120 }
121  
122 2 return $self->success;
123 }
124  
125 #pod =method retrieve_deliveries
126 #pod
127 #pod my @deliveries = $transport->retrieve_deliveries;
128 #pod
129 #pod This method returns a list of deliveries made so far to this transport's
130 #pod database. They're returned in order of insertion, and each delivery is a hash
131 #pod reference like this:
132 #pod
133 #pod id => $db_primary_key,
134 #pod env_from => $envelope_sender,
135 #pod env_to => \@all_env_recipients,
136 #pod message => $text_of_email_sent
137 #pod
138 #pod More fields may be added in the future.
139 #pod
140 #pod =cut
141  
142 1 ▶sub retrieve_deliveries {
Subroutine retrieve_deliveries: called CC=3 SCAR=11.0
Pod documented
143 1 my ($self) = @_;
144  
145 1 my $rows = $self->dbh->selectall_arrayref(
146 "SELECT e.id, env_from, env_to, body
147 FROM emails e
148 JOIN recipients r ON r.email_id = e.id
149 ORDER BY e.id"
150 );
151  
152 1 my %delivery;
153  
154 1 for my $d (@$rows) {
155 3 ▶ $delivery{$d->[0]} ||= {
Condition100%2/2
$delivery{$d->[0]} ||= {"id", $d->[0], "env_from", $d->[1], "env_to", [], "message", $d->[3]}
l!l
12
MC/DC100%1/1
$delivery{$d->[0]} ||= {"id", $d->[0], "env_from", $d->[1], "env_to", [], "message", $d->[3]}
$delivery{$d->[0]}
Truth table100%2/2
A
A= $delivery{$d->[0]}
Aresult
11
00
156 id => $d->[0],
157 env_from => $d->[1],
158 env_to => [ ],
159 message => $d->[3],
160 };
161  
162 3 push @{ $delivery{$d->[0]}{env_to} }, $d->[2];
163 }
164  
165 1 return @delivery{ sort { $a <=> $b } keys %delivery };
166 }
167  
168 1 __PACKAGE__->meta->make_immutable;
169 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 1 1;
171  
172 __END__
↑ Summary