Middleware/Session.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 77.2% 17 / 22 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 94.7% 18 / 19 Podpod 30.0% 3 / 10 totaltotal 84.2% 118 / 140 CC 14 SCAR 8.5
CC
14
Cov
88%
inflate_backend
4.0
commit
3.6
finalize
3.0
CRAP 2.3
« Session/Store/File.pm ↑ Summary Session/State.pm »
1 package Plack::Middleware::Session;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 our $VERSION = '0.36';
6 17 our $AUTHORITY = 'cpan:STEVAN';
7  
8 17 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Plack::Session::Cleanup;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 17 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 state
16 store
17 17 );
18  
19 93 ▶sub prepare_app {
Subroutine prepare_app: called CC=2 SCAR=6.9
Pod documented
20 93 my $self = shift;
21  
22 93 ▶ $self->state( 'Cookie' ) unless $self->state;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->state 3 90 93
23 93 $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
24 93 $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
25 }
26  
27 186 ▶sub inflate_backend {
Subroutine inflate_backend: called CC=4 SCAR=13.9
Pod undocumented
28 186 my($self, $prefix, $backend) = @_;
29  
30 186 ▶ return $backend if defined $backend && Scalar::Util::blessed $backend;
Branch: 1 branch
BranchTrueFalseTotal
if defined $backend and builtin::blessed($backend) 181 5 186
Condition100%3/3
defined $backend and builtin::blessed($backend)
!ll&&!rl&&r
23181
MC/DC100%2/2
defined $backend and builtin::blessed($backend)
defined $backend builtin::blessed($backend)
Truth table100%3/3
A and B
A= defined $backendB= builtin::blessed($backend)
ABresult
0X0
100
111
31  
32 5 my @class;
33 5 ▶ push @class, $backend if defined $backend; # undef means the root class
Branch: 1 branch
BranchTrueFalseTotal
if defined $backend 3 2 5
34 5 push @class, $prefix;
35  
36 5 Plack::Util::load_class(@class)->new();
37 }
38  
39 100 ▶sub call {
Subroutine call: called CC=3 SCAR=11.0
Pod documented
40 100 my $self = shift;
41 100 my $env = shift;
42  
43 100 my($id, $session) = $self->get_session($env);
44 100 ▶ if ($id && $session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id and $session) { } 52 48 100
Condition66%2/3
$id and $session
!ll&&!rl&&r
48052
MC/DC50%1/2
$id and $session
$id $session
Truth table66%2/3
A and B
A= $idB= $session
ABresult
0X0
100
111
45 52 $env->{'psgix.session'} = $session;
46 } else {
47 48 $id = $self->generate_id($env);
48 48 $env->{'psgix.session'} = {};
49 }
50  
51 100 $env->{'psgix.session.options'} = { id => $id };
52  
53 100 my $res = $self->app->($env);
54 100 ▶ $self->response_cb($res, sub { $self->finalize($env, $_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
55 }
56  
57 94 ▶sub get_session {
Subroutine get_session: called CC=3 SCAR=11.0
Pod undocumented
58 94 my($self, $env) = @_;
59  
60 94 ▶ my $id = $self->state->extract($env) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->state->extract($env) or return 62 32 94
61 62 ▶ my $session = $self->store->fetch($id) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $session = $self->store->fetch($id) or return 52 10 62
62  
63 52 return ($id, $session);
64 }
65  
66 42 ▶sub generate_id {
Subroutine generate_id: called CC=1 SCAR=0.0
Pod undocumented
67 42 my($self, $env) = @_;
68 42 $self->state->generate($env);
69 }
70  
71 84 ▶sub commit {
Subroutine commit: called CC=3 SCAR=12.9
Pod undocumented
72 84 my($self, $env) = @_;
73  
74 84 my $session = $env->{'psgix.session'};
75 84 my $options = $env->{'psgix.session.options'};
76  
77 84 ▶ my $end = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
78 84 ▶ return if $options->{no_store};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"no_store"} 0 84 84
79 84 $self->store->store($options->{id}, $session);
80 84 };
81  
82 84 ▶ if (not $options->{late_store}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $options->{'late_store'}) { } 84 0 84
elsif ($env->{'psgix.cleanup'}) { } 0 0 0
83 84 $end->();
84 } elsif ($env->{'psgix.cleanup'}) {
85 0 push @{$env->{'psgix.cleanup.handlers'}}, $end;
86 } else {
87 0 $env->{'psgix.session.cleanup'}
88 = Plack::Session::Cleanup->new($end);
89 }
90 }
91  
92 100 ▶sub finalize {
Subroutine finalize: called CC=3 SCAR=11.0
Pod undocumented
93 100 my($self, $env, $res) = @_;
94  
95 100 my $session = $env->{'psgix.session'};
96 100 my $options = $env->{'psgix.session.options'};
97  
98 100 ▶ if ($options->{expire}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'expire'}) { } 10 90 100
99 10 $self->expire_session($options->{id}, $res, $env);
100 } else {
101 90 ▶ $self->change_id($env) if $options->{change_id};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"change_id"} 0 90 90
102 90 $self->commit($env);
103 90 $self->save_state($options->{id}, $res, $env);
104 }
105 }
106  
107 0 ▶sub change_id {
Subroutine change_id: not called CC=1 SCAR=6.9
Pod documented
108 0 my($self, $env) = @_;
109  
110 0 my $options = $env->{'psgix.session.options'};
111  
112 0 $self->store->remove($options->{id});
113 0 $options->{id} = $self->generate_id($env);
114 }
115  
116 10 ▶sub expire_session {
Subroutine expire_session: called CC=1 SCAR=0.0
Pod undocumented
117 10 my($self, $id, $res, $env) = @_;
118 10 $self->store->remove($id);
119 10 $self->state->expire_session_id($id, $res, $env->{'psgix.session.options'});
120 }
121  
122 84 ▶sub save_state {
Subroutine save_state: called CC=1 SCAR=0.0
Pod undocumented
123 84 my($self, $id, $res, $env) = @_;
124 84 $self->state->finalize($id, $res, $env->{'psgix.session.options'});
125 }
126  
127 17 1;
128  
129 __END__
« Session/Store/File.pm ↑ Summary Session/State.pm »