blib/lib/Dancer2/Session/PSGI.pm

filter: Statementstmt 96.0% 24 / 25 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 3 totaltotal 85.3% 35 / 41 CC 3 SCAR 4.9
CC
3
Cov
97%
set_cookie_header
3.0
_sessions
2.0
BEGIN
1.0
CRAP 1.6
↑ Summary
1 package Dancer2::Session::PSGI;
2  
3 # ABSTRACT: Dancer2 session storage via Plack::Middleware::Session
4  
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 with 'Dancer2::Core::Role::SessionFactory';
7  
8 3 our $VERSION = '0.010'; # VERSION
9  
10 #-----------------------------------------#
11 # Alter SessionFactory attribute defaults
12 #-----------------------------------------#
13  
14 3 has '+cookie_name' => ( default => 'plack_session' );
15  
16 #-----------------------------------------#
17 # SessionFactory implementation methods
18 #-----------------------------------------#
19  
20 # Get middleware session hash
21 7 ▶sub _retrieve {
Subroutine _retrieve: called CC=1 SCAR=0.0
22 7 shift->request->env->{'psgix.session'};
23 }
24  
25 # Put data back/into middleware session hash
26 8 ▶sub _flush {
Subroutine _flush: called CC=1 SCAR=0.0
27 8 my ( $self, $id, $data ) = @_;
28 8 $self->request->env->{'psgix.session'} = $data;
29 }
30  
31 # Do nothing - we override change_id below but this method must exist to
32 # force Dance2::Core::App::change_session_id to call our change_id method
33 0 ▶sub _change_id {}
Subroutine _change_id: not called CC=1 SCAR=0.0
34  
35 # Middleware handles cookie expiry
36 1 ▶sub _destroy { return }
Subroutine _destroy: called CC=1 SCAR=0.0
37  
38 # Its the responsibility of Plack::Middleware::Session for
39 # tracking other sessions (we know nothing about them).
40 # So return an empty list.
41 0 ▶sub _sessions { return [] }
Subroutine _sessions: not called CC=1 SCAR=6.9
42  
43 #-----------------------------------------#
44 # Overridden methods from SessionFactory
45 #-----------------------------------------#
46  
47 # Plack::Middleware::Session is responsible for fetch and id generation
48 # any ID passed to this engine is considered valid
49 7 ▶sub validate_id { 1 }
Subroutine validate_id: called CC=1 SCAR=0.0
Pod undocumented
50  
51 1 ▶sub change_id {
Subroutine change_id: called CC=1 SCAR=0.0
Pod undocumented
52 1 my ( $self, %params ) = @_;
53 1 my $session = $params{session};
54  
55 1 $self->execute_hook( 'engine.session.before_change_id', $session->id );
56 1 $self->request->env->{'psgix.session.options'}->{'change_id'} = 1;
57 # we don't know what the new session ID will be yet so make it undef
58 1 $self->execute_hook( 'engine.session.before_change_id', undef );
59 }
60  
61 # Middleware sets the cookie.
62 10 ▶sub set_cookie_header {
Subroutine set_cookie_header: called CC=3 SCAR=11.0
Pod undocumented
63 10 my ( $self, %params ) = @_;
64  
65 10 my $session = $params{session};
66 10 my $options = $self->request->env->{'psgix.session.options'};
67  
68 10 ▶ if ( my $expires = $session->expires ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expires = $session->expires) 2 8 10
69 2 ▶ if ( $session->expires < time ) { # Cookie has expired.
Branch: 1 branch
BranchTrueFalseTotal
if ($session->expires < time) { } 1 1 2
70 1 $options->{expire} = 1;
71 }
72 else { # Pass upstream the cookie expire time
73 1 $options->{expires} = $expires;
74 }
75 }
76 }
77  
78 3 1;
79  
80 __END__
↑ Summary