File Coverage

blib/lib/Dancer2/Session/CGISession.pm
Criterion Covered Total %
statement 26 49 53.0
branch 2 10 20.0
condition 0 3 0.0
subroutine 9 13 69.2
pod 0 2 0.0
total 37 77 48.0


line stmt bran cond sub pod time code
1             package Dancer2::Session::CGISession;
2              
3 2     2   46217 use strict;
  2         3  
  2         41  
4 2     2   41 use 5.008_005;
  2         6  
5             our $VERSION = '0.04';
6              
7 2     2   518 use Moo;
  2         8475  
  2         10  
8 2     2   1255 use Carp;
  2         2  
  2         91  
9 2     2   457 use Dancer2::Core::Types;
  2         6261  
  2         532  
10 2     2   1095 use CGI::Session;
  2         7531  
  2         9  
11              
12             with 'Dancer2::Core::Role::SessionFactory';
13              
14             #------------------------------------#
15             # Attributes
16             #------------------------------------#
17              
18             has driver_params => (
19             is => 'ro',
20             default => sub { {}; }
21             );
22              
23             has driver => (
24             is => 'ro',
25             default => sub { 'driver:File'; }
26             );
27              
28             has name => (
29             is => 'ro',
30             default => sub { 'CGISESSID'; }
31             );
32              
33             #------------------------------------#
34             # Role composition
35             #------------------------------------#
36              
37             #might be possible to do something with CGI::Session find method here
38             sub _sessions {
39 0     0   0 my ($self) = @_;
40 0         0 return [];
41             }
42              
43             sub generate_id {
44 3     3 0 119299 my ( $class ) = @_;
45              
46             #creation of the cgi session when generating the id, as it using dancer2 id to create CGI session seems not to be working
47 3 50       29 my $cgi_session = CGI::Session->new(
48             $class->driver,
49             undef,
50             $class->driver_params
51             ) or die CGI::Session->errstr();;
52 3         50880 $cgi_session->expire( $class->session_duration );
53 3         50 $cgi_session->name( $class->cookie_name );
54              
55             #Return the newly created CGI::Session id
56 3         24 return $cgi_session->id();
57             }
58              
59             sub _change_id {
60 0     0   0 my ( $self, $old_id, $new_id ) = @_;
61 0         0 my $data = $self->_retrieve($old_id);
62 0         0 $self->_destroy($old_id);
63 0         0 $self->_flush($new_id, $data);
64             }
65              
66             sub _retrieve {
67 0     0   0 my ( $class, $id ) = @_;
68              
69 0         0 my $cgi_session = $class->get_cgi_session( $id );
70 0 0 0     0 if( $cgi_session->is_empty or $cgi_session->is_expired ) {
71             # CGI Session has been removed from the server, die here, Dancer2::Core::Role::Session
72             # knows how to deal with that, warn Caller by dying
73 0         0 die "CGI Session has disappeared";
74             }
75              
76 0         0 return $cgi_session->dataref();
77             }
78              
79             sub _destroy {
80 0     0   0 my ( $class, $id ) = @_;
81              
82 0         0 my $cgi_session = $class->get_cgi_session( $id );
83 0 0       0 if( defined $cgi_session->id ) {
84 0         0 $cgi_session->delete;
85 0         0 $cgi_session->flush;
86             }
87             }
88              
89             sub _flush {
90 3     3   4238 my ( $class, $id, $data ) = @_;
91              
92 3         8 my $cgi_session = $class->get_cgi_session( $id );
93 0         0 foreach my $key (keys %{$data} ){
  0         0  
94 0 0       0 delete $$data{$key} if ($key =~ m/^_SESSION_/);
95             }
96 0         0 $cgi_session->param( %{$data} );
  0         0  
97 0         0 $cgi_session->flush;
98             }
99              
100             sub get_cgi_session {
101 3     3 0 5 my ( $class, $id ) = @_;
102              
103 3 50       22 my $cgi_session = CGI::Session->load( $class->driver, $id, $class->driver_params )
104             or die CGI::Session->errstr();
105              
106 0           return $cgi_session;
107             }
108              
109             1;
110             __END__