Lazy/Session.pm

filter: Statementstmt 15.8% 19 / 120 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 26.0% 6 / 23 Podpod 100% 17 / 17 totaltotal 21.3% 42 / 197 CC 17 SCAR 33.8
CC
17
Cov
12%
parseExpiry
90.0
open
42.0
expired
12.0
CRAP 29.3
« Lazy/Widget/Composite.pm ↑ Summary Lazy/Utility/Debug.pm »
1 package CGI::Lazy::Session;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use CGI::Lazy::ID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use CGI::Lazy::CookieMonster;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CGI::Lazy::Session::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CGI::Lazy::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #--------------------------------------------------------------------------------------
12 0 ▶sub cookiemonster {
Subroutine cookiemonster: not called CC=1 SCAR=6.9
Pod documented
13 0 my $self = shift;
14
15 0 return $self->{_cookiemonster};
16 }
17  
18 #--------------------------------------------------------------------------------------
19 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
20 0 my $self = shift;
21
22 0 my $q = $self->q;
23 0 return $q->config;
24 }
25  
26 #----------------------------------------------------------------------------------------------
27 0 ▶sub data {
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
28 0 my $self = shift;
29 0 return $self->{_data};
30 }
31  
32 #--------------------------------------------------------------------------------------
33 0 ▶sub db {
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
34 0 my $self = shift;
35
36 0 return $self->q->db;
37  
38 }
39  
40 #--------------------------------------------------------------------------------------
41 0 ▶sub expired {
Subroutine expired: not called CC=3 SCAR=24.8
Pod documented
42 0 my $self = shift;
43
44 0 my $table = $self->sessionTable;
45  
46 0 my $now = time();
47 0 my $expiry = $self->data->expires;
48  
49 0 ▶ if ($self->data->terminated) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->data->terminated) { } 0 0 0
elsif ($expiry > $now) { } 0 0 0
50 0 return 1;
51 } elsif ($expiry > $now) {
52 0 return;
53 } else {
54 0 return 1;
55 }
56 }
57  
58 #----------------------------------------------------------------------------------------------
59 0 ▶sub expires {
Subroutine expires: not called CC=1 SCAR=6.9
Pod documented
60 0 my $self = shift;
61 0 return $self->{_expires};
62 }
63  
64 #---------------------------------------------------------------------------------------
65 0 ▶sub getData {
Subroutine getData: not called CC=2 SCAR=17.9
Pod documented
66 0 my $self = shift;
67  
68 0 my $sessionTable = $self->config->plugins->{session}->{sessionTable};
69 0 my $results = $self->db->getarray("select data from $sessionTable where sessionID = ?", $self->sessionID);
70 0 my $data = $results->[0]->[0];
71  
72 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 0 0 0
73 0 return from_json($data);
74 } else {
75 0 return;
76 }
77 }
78  
79 #--------------------------------------------------------------------------------------
80 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82  
83 0 return $self->{_id};
84 }
85  
86 #---------------------------------------------------------------------------------------
87 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
88 0 my $self = shift;
89 0 my $sessionID = shift;
90  
91 0 my $sessionTable = $self->sessionTable;
92  
93 0 my $query = "insert into $sessionTable (sessionID, data) values (?, ?)";
94 0 my $now = time();
95 0 my $expires = $self->parseExpiry($now);
96
97 #set creation time, expiry time, last accessed time
98 0 my $var = {
99 created => $now,
100 updated => $now,
101 expires => $expires,
102 };
103  
104 0 my $data = to_json($var);
105  
106 0 $self->db->do($query, $sessionID, $data);
107  
108 0 return $sessionID;
109 }
110
111 #--------------------------------------------------------------------------------------
112 0 ▶sub open {
Subroutine open: not called CC=6 SCAR=37.4
Pod documented
113 0 my $class = shift;
114 0 my $q = shift;
115 0 my $sessionID = shift;
116  
117 0 my $self = {};
118 0 $self->{_q} = $q;
119 0 $self->{_sessionTable} = $q->plugin->session->{sessionTable};
120 0 $self->{_sessionCookie} = $q->plugin->session->{sessionCookie};
121 0 $self->{_expires} = $q->plugin->session->{expires};
122  
123 0 bless $self, $class;
124  
125 0 $self->{_cookiemonster} = CGI::Lazy::CookieMonster->new($q);
126 0 $self->{_id} = CGI::Lazy::ID->new($self);
127  
128 0 ▶ $sessionID = $self->cookiemonster->getCookie($self->sessionCookie) unless $sessionID;
Branch: 1 branch
BranchTrueFalseTotal
unless $sessionID 0 0 0
129  
130 0 ▶ if ($sessionID) { #check sessionID against db, compare expiry time to last accessed time, reopen if valid
Branch: 1 branch
BranchTrueFalseTotal
if ($sessionID) { } 0 0 0
131 #$q->util->debug->edump("have sessionID");
132 0 $self->{_sessionID} = $sessionID;
133 0 $self->{_data} = CGI::Lazy::Session::Data->new($self);
134
135 0 my $now = time();
136 0 my $expiry = $self->data->expires;
137  
138 0 ▶ $sessionID = $self->new($self->id->generate) if $self->expired;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expired 0 0 0
139  
140 0 ▶ if ($expiry > $now) { #valid sesion, update expiration
Branch: 1 branch
BranchTrueFalseTotal
if ($expiry > $now) { } 0 0 0
141 #$q->util->debug->edump("not expired", "now: ".$now, "expires: ".$expiry, "difference: ".($expiry - $now));
142 0 $self->data->expires($self->parseExpiry($now)); #reset expiry time
143  
144 } else {
145 #$q->util->debug->edump("expired");
146 0 $self->terminate();
147  
148 0 $sessionID = $self->new($self->id->generate); #session expired. create a new one
149 }
150  
151 } else { # create new session
152 0 $sessionID = $self->new($self->id->generate); #if we don't have a valid sessionID, we'll generate one
153 }
154  
155 0 ▶ $q->errorHandler->badSession($sessionID) unless $self->id->valid($sessionID); #error out if we still don't have something we can work with.
Branch: 1 branch
BranchTrueFalseTotal
unless $self->id->valid($sessionID) 0 0 0
156  
157 0 $self->{_sessionID} = $sessionID;
158 0 $self->{_data} = CGI::Lazy::Session::Data->new($self);
159  
160 0 return $self;
161  
162 }
163  
164 #--------------------------------------------------------------------------------------
165 0 ▶sub parseExpiry {
Subroutine parseExpiry: not called CC=9 SCAR=45.0
Pod documented
166 0 my $self = shift;
167 0 my $time = shift;
168  
169 0 my $expirestring = $self->expires;
170  
171 0 $expirestring =~ /([+-])(\d+)(\w)/;
172 0 my ($sign, $num, $unit) = ($1, $2, $3);
173  
174 0 ▶ unless ($sign && $num && $unit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sign and $num and $unit) 0 0 0
Condition0%0/6
$sign and $num and $unit
!ll&&!rl&&r
000
$sign and $num
!ll&&!rl&&r
000
MC/DC0%0/3
$sign and $num and $unit
$sign $num $unit
Truth table0%0/4
A and B and C
A= $signB= $numC= $unit
ABCresult
0XX0
10X0
1100
1111
175 0 $self->q->errorHandler->badSessionExpiry;
176 }
177  
178 0 my $minute = 60; #seconds in a minute
179 0 my $hour = 3600; #seconds in an hour
180 0 my $day = 43200; #seconds in a day
181 0 my $factor;
182 my $expiry;
183  
184 0 ▶ if ($unit eq 'm') {
Branch: 3 branches
BranchTrueFalseTotal
if ($unit eq 'm') { } 0 0 0
elsif ($unit eq 'h') { } 0 0 0
elsif ($unit eq 'd') { } 0 0 0
185 0 $factor = $minute * $num;
186 } elsif ($unit eq 'h') {
187 0 $factor = $hour * $num;
188 } elsif ($unit eq 'd') {
189 0 $factor = $day * $num;
190 } else { #we'll assume it's seconds then (why would someone do this?)
191 0 $factor = $num;
192 }
193  
194 0 ▶ if ($sign eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($sign eq '+') { } 0 0 0
elsif ($sign eq '-') { } 0 0 0
195 0 $expiry = $time + $factor;
196 } elsif ($sign eq '-') {
197 0 $expiry = $time - $factor;
198 }
199
200 0 return $expiry;
201 }
202  
203 #--------------------------------------------------------------------------------------
204 0 ▶sub q {
Subroutine q: not called CC=1 SCAR=6.9
Pod documented
205 0 my $self = shift;
206  
207 0 return $self->{_q};
208 }
209  
210 #--------------------------------------------------------------------------------------
211 0 ▶sub sessionCookie {
Subroutine sessionCookie: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213  
214 0 return $self->{_sessionCookie};
215 }
216  
217 #--------------------------------------------------------------------------------------
218 0 ▶sub sessionID {
Subroutine sessionID: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220
221 0 return $self->{_sessionID};
222 }
223  
224 #--------------------------------------------------------------------------------------
225 0 ▶sub sessionTable {
Subroutine sessionTable: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift;
227  
228 0 return $self->{_sessionTable};
229 }
230  
231 #--------------------------------------------------------------------------------------
232 0 ▶sub save {
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
233 0 my $self = shift;
234  
235 0 my $sessionID = $self->sessionID;
236 0 my $datastring = to_json($self->data->{_data});
237 0 my $sessionTable = $self->q->plugin->session->{sessionTable};
238  
239 0 $self->db->do("update $sessionTable set data = ? where sessionID = ?", $datastring, $sessionID);
240 }
241  
242 #--------------------------------------------------------------------------------------
243 0 ▶sub terminate {
Subroutine terminate: not called CC=1 SCAR=6.9
Pod documented
244 0 my $self = shift;
245  
246 # my $table = $self->sessionTable;
247  
248 # $self->db->do("update $table set expired = 1 where sessionID = ?", $self->sessionID);
249  
250 0 $self->data->terminated(1);
251 0 return;
252 }
253 1 1
254  
255 __END__
« Lazy/Widget/Composite.pm ↑ Summary Lazy/Utility/Debug.pm »