MojoX/Session.pm

filter: Statementstmt 84.4% 179 / 212 Branchbran 57.0% 57 / 100 Conditioncond 53.8% 7 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 90.9% 30 / 33 Podpod 100% 13 / 13 totaltotal 76.2% 289 / 379 CC 56 SCAR 22.9
CC
56
Cov
75%
flush
25.4
__ANON__
20.0
__ANON__
20.0
CRAP 9.9
« MojoX/Session/Store/Dbi.pm ↑ Summary MojoX/Session/Store/Dbic.pm »
1 package MojoX::Session;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 our $VERSION = '0.33';
7  
8 15 ▶use base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use Mojo::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Mojo::Transaction::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use MojoX::Session::Transport::Cookie;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 my $PRIVATE_IP_FIELD = 'mojox.session.ip_address';
17  
18 12 ▶__PACKAGE__->attr(loader => sub { my $l; eval { $l = Mojo::Loader->new }; $l });
Subroutine __ANON__: called CC=1 SCAR=0.0
19 8 ▶__PACKAGE__->attr(tx => sub { Mojo::Transaction::HTTP->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
20 15 __PACKAGE__->attr([qw/sid _store/]);
21 8 ▶__PACKAGE__->attr(_transport => sub { MojoX::Session::Transport::Cookie->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
22 15 );
23  
24 15 __PACKAGE__->attr(ip_match => 0);
25 15 __PACKAGE__->attr(expires_delta => 3600);
26  
27 15 __PACKAGE__->attr(_is_new => 0);
28 15 __PACKAGE__->attr(_is_stored => 0);
29 15 __PACKAGE__->attr(_is_flushed => 1);
30  
31 15 __PACKAGE__->attr(_expires => 0);
32 18 ▶__PACKAGE__->attr(_data => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 15 __PACKAGE__->attr('error');
35  
36 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
37 20 my $class = shift;
38 20 my %args = @_;
39  
40 20 my $store = delete $args{store};
41 20 my $transport = delete $args{transport};
42  
43 20 my $self = $class->SUPER::new(%args);
44  
45 20 $self->_store($self->_instance(Store => $store));
46 20 $self->_transport($self->_instance(Transport => $transport));
47  
48 20 return $self;
49 }
50  
51 100 ▶sub store {
Subroutine store: called CC=2 SCAR=8.1
Pod documented
52 100 my $self = shift;
53  
54 100 ▶ return $self->_store if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 100 0 100
55  
56 0 $self->_store($self->_instance(Store => shift));
57 }
58  
59 136 ▶sub transport {
Subroutine transport: called CC=2 SCAR=8.1
Pod documented
60 136 my $self = shift;
61  
62 136 ▶ return $self->_transport if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 136 0 136
63  
64 0 $self->_transport($self->_instance(Transport => shift));
65 }
66  
67 17 ▶sub _load_and_build {
Subroutine _load_and_build: called CC=5 SCAR=19.1
68 17 my $self = shift;
69 17 my ($namespace, $name, $args) = @_;
70  
71 17 my $class = join('::',
72 __PACKAGE__, $namespace, Mojo::ByteStream->new($name)->camelize);
73  
74 17 ▶ my $rv = $self->loader ? $self->loader->load($class) : Mojo::Loader::load_class($class);
Branch: 1 branch
BranchTrueFalseTotal
$self->loader ? : 0 17 17
75  
76 17 ▶ if (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 0 17 17
77 0 ▶ die qq/Store "$class" can not be loaded : $rv/ if ref $rv;
Branch: 1 branch
BranchTrueFalseTotal
if ref $rv 0 0 0
78  
79 0 die qq/Store "$class" not found/;
80 }
81  
82 17 ▶ return $class->new(%{$args || {}});
Condition50%1/2
$args || {}
l!l
017
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
83 }
84  
85 40 ▶sub _instance {
Subroutine _instance: called CC=5 SCAR=16.8
86 40 my $self = shift;
87 40 my ($namespace, $instance) = @_;
88  
89 40 ▶ return unless $instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance 9 31 40
90  
91 31 ▶ if (ref $instance eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $instance eq 'HASH') { } 0 31 31
elsif (ref $instance eq 'ARRAY') { } 0 31 31
elsif (not ref $instance) { } 17 14 31
92 0 die 'HASH';
93  
94 #$store
95 }
96 elsif (ref $instance eq 'ARRAY') {
97 0 $instance =
98 $self->_load_and_build($namespace, $instance->[0], $instance->[1]);
99 }
100 elsif (!ref $instance) {
101 17 $instance = $self->_load_and_build($namespace, $instance);
102 }
103  
104 31 return $instance;
105 }
106  
107 20 ▶sub create {
Subroutine create: called CC=4 SCAR=13.9
Pod documented
108 20 my $self = shift;
109 20 my ($cb) = @_;
110  
111 20 $self->_expires(time + $self->expires_delta);
112  
113 20 $self->_is_new(1);
114  
115 20 ▶ if ($self->ip_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ip_match) 1 19 20
116 1 $self->data($PRIVATE_IP_FIELD, $self->_remote_addr);
117 }
118  
119 20 $self->_generate_sid;
120  
121 20 ▶ if ($self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport) 20 0 20
122 20 $self->transport->tx($self->tx);
123 20 $self->transport->set($self->sid, $self->expires);
124 }
125  
126 20 $self->_is_flushed(0);
127  
128 20 ▶ return $cb->($self, $self->sid) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 20 20
129  
130 20 return $self->sid;
131 }
132  
133 19 ▶sub load {
Subroutine load: called CC=10 SCAR=28.8
Pod documented
134 19 my $self = shift;
135 19 my ($sid, $cb) = @_;
136  
137 19 ▶ ($cb, $sid) = ($sid, undef) if ref $sid eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $sid eq "CODE" 0 19 19
138  
139 19 $self->sid(undef);
140 19 $self->_expires(0);
141 19 $self->_data({});
142  
143 19 ▶ if ($self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport) 19 0 19
144 19 $self->transport->tx($self->tx);
145 }
146  
147 19 ▶ unless ($sid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sid) 7 12 19
148 7 $sid = $self->transport->get;
149 7 ▶ return $cb ? $cb->($self) : undef unless $sid;
Branch: 2 branches
BranchTrueFalseTotal
unless $sid 2 5 7
$cb ? : 0 2 2
150 }
151  
152 17 ▶ if ($self->store->is_async) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->store->is_async) { } 0 17 17
153 $self->store->load(
154 0 ▶ $sid => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
155 0 my ($store, $expires, $data) = @_;
156  
157 0 ▶ if ($store->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->error) 0 0 0
158 0 $self->error($store->error);
159 0 ▶ return $cb ? $cb->($self) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
160 }
161  
162 0 my $sid = $self->_on_load($sid, $expires, $data);
163  
164 0 ▶ return $cb->($self, $sid) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
165  
166 0 return $sid;
167 }
168 0 );
169 }
170 else {
171 17 my ($expires, $data) = $self->store->load($sid);
172  
173 17 ▶ return $self->error($self->store->error) && undef
Branch: 1 branch
BranchTrueFalseTotal
if $self->store->error 0 17 17
Condition0%0/2
$self->error($self->store->error) && undef
!ll
00
MC/DC0%0/1
$self->error($self->store->error) && undef
$self->error($self->store->error)
Truth table0%0/2
A
A= $self->error($self->store->error)
Aresult
00
11
174 if $self->store->error;
175  
176 17 my $sid = $self->_on_load($sid, $expires, $data);
177  
178 17 ▶ return unless $sid;
Branch: 1 branch
BranchTrueFalseTotal
unless $sid 6 11 17
179  
180 11 return $sid;
181 }
182 }
183  
184 17 ▶sub _on_load {
Subroutine _on_load: called CC=8 SCAR=21.0
185 17 my $self = shift;
186 17 my ($sid, $expires, $data) = @_;
187  
188 17 ▶ unless (defined $expires && defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $expires and defined $data) 5 12 17
Condition66%2/3
defined $expires and defined $data
!ll&&!rl&&r
5012
MC/DC50%1/2
defined $expires and defined $data
defined $expires defined $data
Truth table66%2/3
A and B
A= defined $expiresB= defined $data
ABresult
0X0
100
111
189 5 ▶ $self->transport->set($sid, time - 30 * 24 * 3600)
Branch: 1 branch
BranchTrueFalseTotal
if $self->transport 5 0 5
190 if $self->transport;
191 5 return;
192 }
193  
194 12 $self->_expires($expires);
195 12 $self->_data($data);
196  
197 12 ▶ if ($self->ip_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ip_match) 2 10 12
198 2 ▶ return unless $self->_remote_addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_remote_addr 0 2 2
199  
200 2 ▶ return unless $self->data($PRIVATE_IP_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->data($PRIVATE_IP_FIELD) 0 2 2
201  
202 2 ▶ return unless $self->_remote_addr eq $self->data($PRIVATE_IP_FIELD);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_remote_addr eq $self->data($PRIVATE_IP_FIELD) 1 1 2
203 }
204  
205 11 $self->sid($sid);
206  
207 11 $self->_is_stored(1);
208  
209 11 return $self->sid;
210 }
211  
212 25 ▶sub flush {
Subroutine flush: called CC=10 SCAR=32.4
Pod documented
213 25 my $self = shift;
214 25 my ($cb) = @_;
215  
216 25 ▶ return $cb ? $cb->($self) : 1 unless $self->sid && !$self->_is_flushed;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->sid and not $self->_is_flushed 1 24 25
$cb ? : 0 1 1
Condition66%2/3
$self->sid and not $self->_is_flushed
!ll&&!rl&&r
1024
MC/DC50%1/2
$self->sid and not $self->_is_flushed
$self->sid not $self->_is_flushed
Truth table66%2/3
A and not(B)
A= $self->sidB= not $self->_is_flushed
ABresult
0X0
100
111
217  
218 24 ▶ if ($self->is_expired && $self->_is_stored) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_expired and $self->_is_stored) { } 2 22 24
Condition66%2/3
$self->is_expired and $self->_is_stored
!ll&&!rl&&r
2202
MC/DC50%1/2
$self->is_expired and $self->_is_stored
$self->is_expired $self->_is_stored
Truth table66%2/3
A and B
A= $self->is_expiredB= $self->_is_stored
ABresult
0X0
100
111
219 2 ▶ if ($self->store->is_async) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->store->is_async) { } 0 2 2
220  
221 $self->store->delete(
222 0 ▶ $self->sid => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
223 0 my ($store) = @_;
224  
225 0 ▶ if (my $error = $store->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $store->error) 0 0 0
226 0 $self->error($error);
227 0 ▶ return $cb ? $cb->($self) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
228 }
229  
230 0 $self->_is_stored(0);
231 0 $self->_is_flushed(1);
232  
233 0 ▶ return $cb->($self) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
234  
235 0 return 1;
236 }
237 0 );
238 }
239 else {
240 2 my $ok = $self->store->delete($self->sid);
241 2 $self->_is_stored(0);
242 2 $self->_is_flushed(1);
243 2 return $ok;
244 }
245 }
246 else {
247 22 my $ok = 1;
248  
249 22 ▶ my $action = $self->_is_new ? 'create' : 'update';
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_new ? : 19 3 22
250  
251 22 $self->_is_new(0);
252  
253 22 ▶ if ($self->store->is_async) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->store->is_async) { } 0 22 22
254 $self->store->$action(
255 $self->sid,
256 $self->expires,
257 0 ▶ $self->data => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
258 0 my ($store) = @_;
259  
260 0 ▶ if ($store->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->error) 0 0 0
261 0 $self->error($store->error);
262 0 ▶ return $cb ? $cb->($self) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
263 }
264  
265 0 $self->_is_stored(1);
266 0 $self->_is_flushed(1);
267  
268 0 ▶ return $cb ? $cb->($self) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
269 }
270 0 );
271 }
272 else {
273 22 $ok =
274 $self->store->$action($self->sid, $self->expires, $self->data);
275  
276 22 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 22 22
277 0 $self->error($self->store->error);
278 0 return;
279 }
280  
281 22 $self->_is_stored(1);
282 22 $self->_is_flushed(1);
283  
284 22 return $ok;
285 }
286 }
287 }
288  
289 60 ▶sub data {
Subroutine data: called CC=3 SCAR=11.0
Pod documented
290 60 my $self = shift;
291  
292 60 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 39 21 60
293 39 return $self->_data;
294 }
295  
296 21 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 11 10 21
297 11 return $self->_data->{$_[0]};
298 }
299  
300 10 my %params = @_;
301  
302 10 $self->_data({%{$self->_data}, %params});
303 10 $self->_is_flushed(0);
304 }
305  
306 2 ▶sub flash {
Subroutine flash: called CC=2 SCAR=7.0
Pod documented
307 2 my $self = shift;
308 2 my ($key) = @_;
309  
310 2 ▶ return unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 2 2
311  
312 2 $self->_is_flushed(0);
313  
314 2 return delete $self->data->{$key};
315 }
316  
317 3 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
318 3 my $self = shift;
319 3 my ($key) = @_;
320  
321 3 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 1 2 3
322 1 delete $self->_data->{$key};
323 }
324 else {
325 2 $self->_data({});
326 }
327  
328 3 $self->_is_flushed(0);
329 }
330  
331 2 ▶sub expire {
Subroutine expire: called CC=2 SCAR=7.0
Pod documented
332 2 my $self = shift;
333  
334 2 $self->expires(0);
335  
336 2 ▶ if ($self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport) 2 0 2
337 2 $self->transport->tx($self->tx);
338 2 $self->transport->set($self->sid, $self->expires);
339 }
340  
341 2 return $self;
342 }
343  
344 92 ▶sub expires {
Subroutine expires: called CC=2 SCAR=6.9
Pod documented
345 92 my $self = shift;
346 92 my ($val) = @_;
347  
348 92 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 4 88 92
349 4 $self->_expires($val);
350 4 $self->_is_flushed(0);
351 }
352  
353 92 return $self->_expires;
354 }
355  
356 3 ▶sub extend_expires {
Subroutine extend_expires: called CC=2 SCAR=7.0
Pod documented
357 3 my $self = shift;
358  
359 3 $self->_expires(time + $self->expires_delta);
360  
361 3 ▶ if ($self->transport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->transport) 3 0 3
362 3 $self->transport->tx($self->tx);
363 3 $self->transport->set($self->sid, $self->expires);
364 }
365  
366 3 $self->_is_flushed(0);
367 }
368  
369 31 ▶sub is_expired {
Subroutine is_expired: called CC=2 SCAR=6.9
Pod documented
370 31 my ($self) = shift;
371  
372 31 ▶ return time > $self->expires ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
time > $self->expires ? : 7 24 31
373 }
374  
375 5 ▶sub _remote_addr {
Subroutine _remote_addr: called CC=1 SCAR=0.0
376 5 my $self = shift;
377  
378 5 return $self->tx->remote_address;
379 }
380  
381 20 ▶sub _generate_sid {
Subroutine _generate_sid: called CC=1 SCAR=0.0
382 20 my $self = shift;
383  
384 # based on CGI::Session::ID
385 20 my $sha1 = Digest::SHA->new(1);
386 20 $sha1->add($$, time, rand(time));
387 20 $self->sid($sha1->hexdigest);
388 }
389  
390 15 1;
391 __END__
« MojoX/Session/Store/Dbi.pm ↑ Summary MojoX/Session/Store/Dbic.pm »