GX/Session.pm

filter: Statementstmt 11.5% 33 / 285 Branchbran 1.6% 2 / 118 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 12.2% 7 / 57 Podpod 100% 22 / 22 totaltotal 12.0% 64 / 531 CC 75 SCAR 43.5
CC
75
Cov
8%
_setup_subcomponent
272.0
resume
240.0
_setup_config
110.0
CRAP 77.3
« GX/Router.pm ↑ Summary GX/Script/Build.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Session.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Session;
7  
8 1 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Constants
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 STATE_INITIALIZED => 0,
20 STATE_ACTIVE => 1 << 0,
21 STATE_NEW => 1 << 1,
22 STATE_RESUMED => 1 << 2,
23 STATE_STORED => 1 << 3,
24 STATE_UNKNOWN => 1 << 4,
25 STATE_EXPIRED => 1 << 5,
26 STATE_INVALID => 1 << 6
27 1 };
28  
29  
30 # ----------------------------------------------------------------------------------------------------------------------
31 # Class setup
32 # ----------------------------------------------------------------------------------------------------------------------
33  
34 1 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 extends 'GX::Component';
37  
38 has static 'id_generator' => (
39 isa => 'Object',
40 0 ▶ constraint => sub { $_->isa( 'GX::Session::ID::Generator' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 1 accessors => {
42 'id_generator' => { type => 'get' },
43 '_set_id_generator' => { type => 'set' }
44 }
45 );
46  
47 has static 'initial_lifetime' => (
48 isa => 'Scalar',
49 0 ▶ constraint => sub { defined && /^\d+$/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and /^\d+$/ 0 0 0
50 1 accessors => {
51 '_get_initial_lifetime' => { type => 'get' },
52 '_set_initial_lifetime' => { type => 'set' }
53 }
54 );
55  
56 has static 'lifetime' => (
57 isa => 'Scalar',
58 1 ▶ constraint => sub { defined && /^\d+$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and /^\d+$/ 1 0 1
59 1 default => 86400,
60 accessors => {
61 'lifetime' => { type => 'get' },
62 '_set_lifetime' => { type => 'set' }
63 }
64 );
65  
66 1 has static 'options' => (
67 isa => 'Hash',
68 initializer => '_initialize_options',
69 accessors => {
70 'options' => { type => 'get_list' },
71 '_get_options' => { type => 'get_reference' },
72 '_get_option' => { type => 'get_value' },
73 '_set_option' => { type => 'set_value' }
74 }
75 );
76  
77 has static 'store' => (
78 isa => 'Object',
79 0 ▶ constraint => sub { $_->isa( 'GX::Session::Store' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 1 accessors => {
81 'store' => { type => 'get' },
82 '_set_store' => { type => 'set' }
83 }
84 );
85  
86 has static 'timeout' => (
87 isa => 'Scalar',
88 1 ▶ constraint => sub { defined && /^\d+$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and /^\d+$/ 1 0 1
89 1 default => 3600,
90 accessors => {
91 'timeout' => { type => 'get' },
92 '_set_timeout' => { type => 'set' }
93 }
94 );
95  
96 has static 'tracker' => (
97 isa => 'Object',
98 0 ▶ constraint => sub { $_->isa( 'GX::Session::Tracker' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 1 accessors => {
100 'tracker' => { type => 'get' },
101 '_set_tracker' => { type => 'set' }
102 }
103 );
104  
105 has 'context' => (
106 isa => 'Object',
107 0 ▶ constraint => sub { $_->isa( 'GX::Context' ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 1 required => 1,
109 weaken => 1,
110 sticky => 1,
111 accessors => {
112 'context' => { type => 'get' }
113 }
114 );
115  
116 has 'data' => (
117 isa => 'Scalar',
118 0 ▶ initializer => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 1 accessors => {
120 '_get_data' => { type => 'get' },
121 '_set_data' => { type => 'set' },
122 '_clear_data' => { type => 'clear' }
123 }
124 );
125  
126 1 has 'id' => (
127 isa => 'Scalar',
128 accessors => {
129 'id' => { type => 'get' },
130 '_set_id' => { type => 'set' },
131 '_clear_id' => { type => 'clear' }
132 }
133 );
134  
135 has 'info' => (
136 isa => 'Scalar',
137 0 ▶ initializer => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 1 accessors => {
139 '_get_info' => { type => 'get' },
140 '_set_info' => { type => 'set' },
141 '_clear_info' => { type => 'clear' }
142 }
143 );
144  
145 1 has 'state' => (
146 isa => 'Scalar',
147 default => STATE_INITIALIZED,
148 accessors => {
149 '_get_state' => { type => 'get' },
150 '_set_state' => { type => 'set' },
151 '_clear_state' => { type => 'clear' }
152 }
153 );
154  
155 1 build;
156  
157  
158 # ----------------------------------------------------------------------------------------------------------------------
159 # Export mechanism
160 # ----------------------------------------------------------------------------------------------------------------------
161  
162 1 __PACKAGE__->_install_import_method;
163  
164  
165 # ----------------------------------------------------------------------------------------------------------------------
166 # Public methods - Session state API
167 # ----------------------------------------------------------------------------------------------------------------------
168  
169 0 ▶sub expires_at {
Subroutine expires_at: not called CC=1 SCAR=6.9
Pod documented
170  
171 0 return $_[0]->_get_info->{'expires_at'};
172  
173 }
174  
175 0 ▶sub is_active {
Subroutine is_active: not called CC=1 SCAR=6.9
Pod documented
176  
177 0 return $_[0]->_get_state & STATE_ACTIVE;
178  
179 }
180  
181 0 ▶sub is_expired {
Subroutine is_expired: not called CC=1 SCAR=6.9
Pod documented
182  
183 0 return $_[0]->_get_state & STATE_EXPIRED;
184  
185 }
186  
187 0 ▶sub is_invalid {
Subroutine is_invalid: not called CC=1 SCAR=6.9
Pod documented
188  
189 0 return $_[0]->_get_state & STATE_INVALID;
190  
191 }
192  
193 0 ▶sub is_new {
Subroutine is_new: not called CC=1 SCAR=6.9
Pod documented
194  
195 0 return $_[0]->_get_state & STATE_NEW;
196  
197 }
198  
199 0 ▶sub is_resumed {
Subroutine is_resumed: not called CC=1 SCAR=6.9
Pod documented
200  
201 0 return $_[0]->_get_state & STATE_RESUMED;
202  
203 }
204  
205 0 ▶sub is_stored {
Subroutine is_stored: not called CC=1 SCAR=6.9
Pod documented
206  
207 0 return $_[0]->_get_state & STATE_STORED;
208  
209 }
210  
211 0 ▶sub is_unknown {
Subroutine is_unknown: not called CC=1 SCAR=6.9
Pod documented
212  
213 0 return $_[0]->_get_state & STATE_UNKNOWN;
214  
215 }
216  
217 0 ▶sub remote_address {
Subroutine remote_address: not called CC=1 SCAR=6.9
Pod documented
218  
219 0 return $_[0]->_get_info->{'remote_address'};
220  
221 }
222  
223 0 ▶sub started_at {
Subroutine started_at: not called CC=1 SCAR=6.9
Pod documented
224  
225 0 return $_[0]->_get_info->{'started_at'};
226  
227 }
228  
229 0 ▶sub updated_at {
Subroutine updated_at: not called CC=1 SCAR=6.9
Pod documented
230  
231 0 return $_[0]->_get_info->{'updated_at'};
232  
233 }
234  
235  
236 # ----------------------------------------------------------------------------------------------------------------------
237 # Public methods - Session data API
238 # ----------------------------------------------------------------------------------------------------------------------
239  
240 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
241  
242 0 my $self = shift;
243  
244 0 $self->_clear_data;
245  
246 0 return;
247  
248 }
249  
250 0 ▶sub data {
Subroutine data: not called CC=5 SCAR=34.0
Pod documented
251  
252 0 my $self = shift;
253  
254 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
255  
256 0 ▶ if ( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (not @_ % 2) { } 0 0 0
257  
258 0 ▶ if ( ref $_[0] ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] ne "HASH") 0 0 0
259 0 complain "Invalid argument";
260 }
261  
262 0 $self->_set_data( $_[0] );
263  
264 }
265 elsif ( ! ( @_ % 2 ) ) {
266 0 %{$self->_get_data} = @_;
267 }
268 else {
269 0 complain "Invalid number of arguments";
270 }
271  
272 }
273  
274 0 return $self->_get_data;
275  
276 }
277  
278 0 ▶sub delete_data {
Subroutine delete_data: not called CC=1 SCAR=6.9
Pod documented
279  
280 0 return delete $_[0]->_get_data->{$_[1]};
281  
282 }
283  
284 0 ▶sub get_data {
Subroutine get_data: not called CC=1 SCAR=6.9
Pod documented
285  
286 0 return $_[0]->_get_data->{$_[1]};
287  
288 }
289  
290 0 ▶sub set_data {
Subroutine set_data: not called CC=1 SCAR=6.9
Pod documented
291  
292 0 $_[0]->_get_data->{$_[1]} = $_[2];
293  
294 0 return;
295  
296 }
297  
298 0 ▶sub variables {
Subroutine variables: not called CC=1 SCAR=6.9
Pod documented
299  
300 0 return keys %{$_[0]->_get_data};
301  
302 }
303  
304  
305 # ----------------------------------------------------------------------------------------------------------------------
306 # Public methods - Session control API
307 # ----------------------------------------------------------------------------------------------------------------------
308  
309 0 ▶sub end {
Subroutine end: not called CC=4 SCAR=30.0
Pod documented
310  
311 0 my $self = shift;
312  
313 0 ▶ if ( ! $self->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_active) 0 0 0
314 0 return;
315 }
316  
317 0 ▶ if ( $self->is_stored ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_stored) 0 0 0
318  
319 0 eval {
320 0 $self->store->delete( $self->id );
321 };
322  
323 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
324 0 GX::Exception->complain(
325 message => "Cannot end session " . ref( $self ),
326 subexception => $@
327 );
328 }
329  
330 }
331  
332 0 $self->tracker->unset_id( $self->context );
333  
334 0 $self->reset;
335  
336 0 return 1;
337  
338 }
339  
340 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
341  
342 0 my $self = shift;
343  
344 0 $self->_clear_state;
345 0 $self->_clear_id;
346 0 $self->_clear_info;
347 0 $self->_clear_data;
348  
349 0 return 1;
350  
351 }
352  
353 0 ▶sub resume {
Subroutine resume: not called CC=15 SCAR=54.8
Pod documented
354  
355 0 my $self = shift;
356 0 my $id = shift;
357  
358 0 ▶ if ( $self->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_active) 0 0 0
359 0 complain "Session " . ref( $self ) . " is already active";
360 }
361  
362 0 $self->reset;
363  
364 0 ▶ if ( ! defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
365  
366 0 $id = $self->tracker->get_id( $self->context );
367  
368 0 ▶ if ( ! defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
369 0 return;
370 }
371  
372 }
373  
374 0 ▶ if ( ! $self->id_generator->validate_id( $id ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->id_generator->validate_id($id)) 0 0 0
375 0 $self->_set_state_flag( STATE_INVALID );
376 0 return;
377 }
378  
379 0 my ( $info, $data ) = eval {
380 0 $self->store->load( $id );
381 };
382  
383 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
384 0 GX::Exception->complain(
385 message => "Cannot resume session " . ref( $self ),
386 subexception => $@
387 );
388 }
389  
390 0 ▶ if ( ! $info ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info) 0 0 0
391 0 $self->_set_state_flag( STATE_UNKNOWN );
392 0 return;
393 }
394  
395 0 ▶ if ( defined $info->{'remote_address'} && length $info->{'remote_address'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"remote_address"} and length $info->{"remote_address"}) 0 0 0
Condition0%0/3
defined $info->{"remote_address"} and length $info->{"remote_address"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $info->{"remote_address"} and length $info->{"remote_address"}
defined $info->{"remote_address"} length $info->{"remote_address"}
Truth table0%0/3
A and B
A= defined $info->{"remote_address"}B= length $info->{"remote_address"}
ABresult
0X0
100
111
396  
397 0 my $remote_address = $self->context->request->remote_address;
398  
399 0 ▶ if ( ! defined $remote_address || $info->{'remote_address'} ne $remote_address ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $remote_address or $info->{"remote_address"} ne $remote_address) 0 0 0
Condition0%0/3
not defined $remote_address or $info->{"remote_address"} ne $remote_address
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $remote_address or $info->{"remote_address"} ne $remote_address
not defined $remote_address $info->{"remote_address"} ne $remote_address
Truth table0%0/3
not(A) or B
A= not defined $remote_addressB= $info->{"remote_address"} ne $remote_address
ABresult
1X1
011
000
400 0 $self->_set_state_flag( STATE_INVALID );
401 0 return;
402 }
403  
404 }
405  
406 0 ▶ if ( defined $info->{'expires_at'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"expires_at"}) 0 0 0
407  
408 # Special values:
409 # 0 => session never expires
410 # -1 => session is expired
411  
412 0 ▶ if ( $info->{'expires_at'} != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"expires_at"} != 0) 0 0 0
413  
414 0 ▶ if ( ! ( $info->{'expires_at'} > $self->_get_current_time ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"expires_at"} > $self->_get_current_time) 0 0 0
415 0 $self->_set_state_flag( STATE_EXPIRED );
416 0 return;
417 }
418  
419 }
420  
421 }
422  
423 0 $self->_set_state_flag( STATE_ACTIVE | STATE_RESUMED | STATE_STORED );
424 0 $self->_set_id( $id );
425 0 $self->_set_info( $info );
426 0 ▶ $self->_set_data( $data // {} );
Condition0%0/2
$data // {}
l!l
00
MC/DC0%0/1
$data // {}
$data
Truth table0%0/2
A
A= $data
Aresult
11
00
427
428 0 return 1;
429  
430 }
431  
432 0 ▶sub save {
Subroutine save: not called CC=6 SCAR=37.4
Pod documented
433  
434 0 my $self = shift;
435  
436 0 ▶ if ( ! $self->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_active) 0 0 0
437 0 complain "Session " . ref( $self ) . " is not active";
438 }
439  
440 0 my $info = { %{$self->_get_info}, 'updated_at' => $self->_get_current_time };
441  
442 0 ▶ if ( $self->timeout > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->timeout > 0) 0 0 0
443  
444 0 my $expires_at = $self->_get_current_time + $self->timeout;
445  
446 0 ▶ if ( $self->lifetime > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lifetime > 0) 0 0 0
447 0 $expires_at = List::Util::min( $expires_at, $self->started_at + $self->lifetime );
448 }
449  
450 0 $info->{'expires_at'} = $expires_at;
451  
452 }
453  
454 0 my $success = eval {
455  
456 0 ▶ if ( $self->is_stored ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_stored) { } 0 0 0
457 0 $self->store->update( $self->id, $info, $self->_get_data );
458 }
459 else {
460 0 $self->store->save( $self->id, $info, $self->_get_data );
461 }
462  
463 };
464  
465 0 ▶ if ( ! $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
466 0 GX::Exception->complain(
467 message => "Cannot save session " . ref( $self ),
468 subexception => $@
469 );
470 }
471  
472 0 $self->_set_state_flag( STATE_STORED );
473 0 $self->_set_info( $info );
474  
475 0 return 1;
476  
477 }
478  
479 0 ▶sub start {
Subroutine start: not called CC=5 SCAR=34.0
Pod documented
480  
481 0 my $self = shift;
482  
483 0 ▶ if ( $self->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_active) 0 0 0
484 0 complain "Session " . ref( $self ) . " is already active";
485 }
486  
487 0 $self->_clear_state;
488 0 $self->_clear_id;
489 0 $self->_clear_info;
490  
491 0 my $id = $self->id_generator->generate_id;
492 0 my $info = {};
493  
494 0 $info->{'started_at'} = $self->_get_current_time;
495  
496 0 ▶ if ( $self->_get_initial_lifetime > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_initial_lifetime > 0) { } 0 0 0
497 0 $info->{'expires_at'} = $info->{'started_at'} + $self->_get_initial_lifetime;
498 }
499 else {
500 0 $info->{'expires_at'} = 0;
501 }
502  
503 0 ▶ if ( $self->_get_option( 'bind_to_remote_address' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_get_option("bind_to_remote_address")) 0 0 0
504 0 $info->{'remote_address'} = $self->context->request->remote_address;
505 }
506  
507 0 my $success = eval {
508 0 $self->store->save( $id, $info, $self->_get_data );
509 };
510  
511 0 ▶ if ( ! $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
512 0 GX::Exception->complain(
513 message => "Cannot start session " . ref( $self ),
514 subexception => $@
515 );
516 }
517  
518 0 $self->tracker->set_id( $self->context, $id );
519  
520 0 $self->_set_state_flag( STATE_ACTIVE | STATE_NEW | STATE_STORED );
521 0 $self->_set_id( $id );
522 0 $self->_set_info( $info );
523  
524 0 return 1;
525  
526 }
527  
528  
529 # ----------------------------------------------------------------------------------------------------------------------
530 # Private methods
531 # ----------------------------------------------------------------------------------------------------------------------
532  
533 0 ▶sub _auto_resume_handler {
Subroutine _auto_resume_handler: not called CC=3 SCAR=24.8
534  
535 0 my $class = shift;
536 0 my $context = shift;
537  
538 0 ▶ if ( my $session = $context->session( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $context->session($class)) 0 0 0
539  
540 0 ▶ if ( ! $session->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->is_active) 0 0 0
541  
542 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
543 0 $session->resume;
544 }
545 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
546 0 $class->application->handle_error( $context, $_ );
547 0 };
548  
549 }
550  
551 }
552  
553 0 return;
554  
555 }
556  
557 0 ▶sub _auto_save_handler {
Subroutine _auto_save_handler: not called CC=3 SCAR=24.8
558  
559 0 my $class = shift;
560 0 my $context = shift;
561  
562 0 ▶ if ( my $session = $context->session( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $context->session($class)) 0 0 0
563  
564 0 ▶ if ( $session->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->is_active) 0 0 0
565  
566 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
567 0 $session->save;
568 }
569 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
570 0 $class->application->handle_error( $context, $_ );
571 0 };
572  
573 }
574  
575 }
576  
577 0 return;
578  
579 }
580  
581 0 ▶sub _auto_start_handler {
Subroutine _auto_start_handler: not called CC=3 SCAR=24.8
582  
583 0 my $class = shift;
584 0 my $context = shift;
585  
586 0 ▶ if ( my $session = $context->session( $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $session = $context->session($class)) 0 0 0
587  
588 0 ▶ if ( ! $session->is_active ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->is_active) 0 0 0
589  
590 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
591 0 ▶ $session->resume or $session->start;
Branch: 1 branch
BranchTrueFalseTotal
$session->resume or $session->start 0 0 0
592 }
593 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
594 0 $class->application->handle_error( $context, $_ );
595 0 };
596  
597 }
598  
599 }
600  
601 0 return;
602  
603 }
604  
605 0 ▶sub _get_current_time {
Subroutine _get_current_time: not called CC=1 SCAR=6.9
606  
607 0 return $_[0]->context->time;
608  
609 }
610  
611 0 ▶sub _initialize_config {
Subroutine _initialize_config: not called CC=1 SCAR=6.9
612  
613 return {
614 0 'id_generator' => undef,
615 'store' => undef,
616 'tracker' => undef
617 };
618  
619 }
620  
621 0 ▶sub _initialize_options {
Subroutine _initialize_options: not called CC=1 SCAR=6.9
622  
623 return {
624 0 'auto_resume' => 1,
625 'auto_save' => 1,
626 'auto_start' => 0,
627 'bind_to_remote_address' => 1
628 };
629  
630 }
631  
632 0 ▶sub _set_state_flag {
Subroutine _set_state_flag: not called CC=1 SCAR=6.9
633  
634 0 my $self = shift;
635 0 my $flag = shift;
636  
637 0 ( my $new_state = $self->_get_state ) |= $flag;
638  
639 0 $self->_set_state( $new_state );
640  
641 0 return;
642  
643 }
644  
645 0 ▶sub _setup {
Subroutine _setup: not called CC=1 SCAR=6.9
646  
647 0 my $class = shift;
648  
649 0 $class->SUPER::_setup( @_ );
650  
651 0 $class->_setup_id_generator;
652 0 $class->_setup_tracker;
653 0 $class->_setup_store;
654  
655 0 return;
656  
657 }
658  
659 0 ▶sub _setup_config {
Subroutine _setup_config: not called CC=10 SCAR=47.0
660  
661 0 my $class = shift;
662 0 my $args = shift;
663  
664 0 my $config = $class->_get_config;
665  
666 0 for ( qw(
667 id_generator
668 store
669 tracker
670 ) ) {
671 0 $config->{$_} = delete $args->{$_};
672 }
673  
674 0 for ( qw(
675 auto_resume
676 auto_save
677 auto_start
678 bind_to_remote_address
679 ) ) {
680 0 ▶ $class->_set_option( $_ => delete $args->{$_} ? 1 : 0 ) if exists $args->{$_};
Branch: 2 branches
BranchTrueFalseTotal
if exists $args->{$_} 0 0 0
delete $args->{$_} ? : 0 0 0
681 }
682  
683 0 for ( qw(
684 lifetime
685 timeout
686 ) ) {
687  
688 0 ▶ next unless exists $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$_} 0 0 0
689  
690 0 ▶ if ( ! defined $args->{$_} || $args->{$_} !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{$_} and $args->{$_} =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $args->{$_} and $args->{$_} =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{$_} and $args->{$_} =~ /^\d+$/
defined $args->{$_} $args->{$_} =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $args->{$_}B= $args->{$_} =~ /^\d+$/
ABresult
0X0
100
111
691 0 throw "Invalid option (\"$_\" must be a positive integer)";
692 }
693  
694 0 my $accessor = "_set_$_";
695 0 $class->$accessor( delete $args->{$_} );
696  
697 }
698  
699 $class->_set_initial_lifetime(
700 0 ▶ List::Util::min( grep { $_ > 0 } $class->lifetime, $class->timeout ) // 0
Condition0%0/2
&List::Util::min(grep({$_ > 0;} $class->lifetime, $class->timeout)) // 0
l!l
00
MC/DC0%0/1
&List::Util::min(grep({$_ > 0;} $class->lifetime, $class->timeout)) // 0
&List::Util::min(grep({$_ > 0;} $class->lifetime, $class->timeout))
Truth table0%0/2
A
A= &List::Util::min(grep({$_ > 0;} $class->lifetime, $class->timeout))
Aresult
11
00
701 );
702  
703 0 $class->SUPER::_setup_config( $args );
704  
705 0 return;
706  
707 }
708  
709 0 ▶sub _setup_handlers {
Subroutine _setup_handlers: not called CC=4 SCAR=30.0
710  
711 0 my $class = shift;
712  
713 0 $class->SUPER::_setup_handlers;
714  
715 0 ▶ if ( $class->_get_option( 'auto_start' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($class->_get_option('auto_start')) { } 0 0 0
elsif ($class->_get_option('auto_resume')) { } 0 0 0
716 0 $class->_add_handler( ProcessSessions => '_auto_start_handler' );
717 }
718 elsif ( $class->_get_option( 'auto_resume' ) ) {
719 0 $class->_add_handler( ProcessSessions => '_auto_resume_handler' );
720 }
721  
722 0 ▶ if ( $class->_get_option( 'auto_save' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_get_option("auto_save")) 0 0 0
723 0 $class->_add_handler( FinalizeSessions => '_auto_save_handler' );
724 }
725  
726 0 return;
727  
728 }
729  
730 0 ▶sub _setup_id_generator {
Subroutine _setup_id_generator: not called CC=3 SCAR=24.8
731  
732 0 my $class = shift;
733  
734 0 ▶ my $config = delete $class->_get_config->{'id_generator'} // 'GX::Session::ID::Generator::MD5';
Condition0%0/2
delete $class->_get_config->{"id_generator"} // "GX::Session::ID::Generator::MD5"
l!l
00
MC/DC0%0/1
delete $class->_get_config->{"id_generator"} // "GX::Session::ID::Generator::MD5"
delete $class->_get_config->{"id_generator"}
Truth table0%0/2
A
A= delete $class->_get_config->{"id_generator"}
Aresult
11
00
735  
736 0 my $id_generator = eval {
737 0 $class->_setup_subcomponent( 'GX::Session::ID::Generator', 'id_generator', $config );
738 };
739  
740 0 ▶ if ( ! $id_generator ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id_generator) 0 0 0
741 0 GX::Exception->throw(
742 message => "Cannot setup session ID generator",
743 subexception => $@
744 );
745 }
746  
747 0 $class->_set_id_generator( $id_generator );
748  
749 0 return;
750  
751 }
752  
753 0 ▶sub _setup_store {
Subroutine _setup_store: not called CC=3 SCAR=24.8
754  
755 0 my $class = shift;
756  
757 0 my $config = delete $class->_get_config->{'store'};
758  
759 0 ▶ if ( ! defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 0 0 0
760 0 throw "Unspecified session store";
761 }
762  
763 0 my $store = eval {
764 0 $class->_setup_subcomponent( 'GX::Session::Store', 'store', $config );
765 };
766  
767 0 ▶ if ( ! $store ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store) 0 0 0
768 0 GX::Exception->throw(
769 message => "Cannot setup session store",
770 subexception => $@
771 );
772 }
773  
774 0 $class->_set_store( $store );
775  
776 0 return;
777  
778 }
779  
780 0 ▶sub _setup_tracker {
Subroutine _setup_tracker: not called CC=4 SCAR=30.0
781  
782 0 my $class = shift;
783  
784 0 ▶ my $config = delete $class->_get_config->{'tracker'} // [
Branch: 1 branch
BranchTrueFalseTotal
$class->lifetime ? : 0 0 0
Condition0%0/2
delete $class->_get_config->{"tracker"} // ["GX::Session::Tracker::Cookie", {"cookie_attributes", {"name", uc join("_", split(/::/, ($class =~ /^.+?::Session::(.+)$/)[0], 0), "SESSION_ID"), $class->lifetime ? ("max_age", $class->lifetime) : ()}}]
l!l
00
MC/DC0%0/1
delete $class->_get_config->{"tracker"} // ["GX::Session::Tracker::Cookie", {"cookie_attributes", {"name", uc join("_", split(/::/, ($class =~ /^.+?::Session::(.+)$/)[0], 0), "SESSION_ID"), $class->lifetime ? ("max_age", $class->lifetime) : ()}}]
delete $class->_get_config->{"tracker"}
Truth table0%0/2
A
A= delete $class->_get_config->{"tracker"}
Aresult
11
00
785 'GX::Session::Tracker::Cookie' => {
786 cookie_attributes => {
787 name => uc( join( '_', split( /::/, ( $class =~ /^.+?::Session::(.+)$/ )[0] ), 'SESSION_ID' ) ),
788 ( $class->lifetime ? ( max_age => $class->lifetime ) : () )
789 }
790 }
791 ];
792  
793 0 my $tracker = eval {
794 0 $class->_setup_subcomponent( 'GX::Session::Tracker', 'tracker', $config );
795 };
796  
797 0 ▶ if ( ! $tracker ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tracker) 0 0 0
798 0 GX::Exception->throw(
799 message => "Cannot setup session tracker",
800 subexception => $@
801 );
802 }
803  
804 0 $class->_set_tracker( $tracker );
805  
806 0 return;
807  
808 }
809  
810 0 ▶sub _setup_subcomponent {
Subroutine _setup_subcomponent: not called CC=16 SCAR=56.1
811  
812 0 my $class = shift;
813 0 my $subcomponent_base = shift;
814 0 my $config_key = shift;
815 0 my $config = shift;
816  
817 0 my $subcomponent;
818  
819 0 ▶ if ( blessed $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($config)) { } 0 0 0
820  
821 0 ▶ if ( $config->isa( $subcomponent_base ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->isa($subcomponent_base)) { } 0 0 0
822 0 $subcomponent = $config;
823 }
824 else {
825 0 throw "Invalid option (\"$config_key\")";
826 }
827  
828 }
829 else {
830  
831 0 my $subcomponent_class;
832 my $subcomponent_options;
833  
834 0 ▶ if ( ref $config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config) { } 0 0 0
835  
836 0 ▶ if ( ref $config eq 'HASH' && keys %$config == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq "HASH" and keys %$config == 1) 0 0 0
Condition0%0/3
ref $config eq "HASH" and keys %$config == 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $config eq "HASH" and keys %$config == 1
ref $config eq "HASH" keys %$config == 1
Truth table0%0/3
A and B
A= ref $config eq "HASH"B= keys %$config == 1
ABresult
0X0
100
111
837 0 $config = [ %$config ];
838 }
839  
840 0 ▶ if ( ref $config eq 'ARRAY' && ( @$config == 1 || @$config == 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config eq 'ARRAY' and @$config == 1 || @$config == 2) { } 0 0 0
Condition0%0/6
ref $config eq "ARRAY" and @$config == 1 || @$config == 2
!ll&&!rl&&r
000
@$config == 1 or @$config == 2
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $config eq "ARRAY" and @$config == 1 || @$config == 2
ref $config eq "ARRAY" @$config == 1 @$config == 2
Truth table0%0/4
A and B or C
A= ref $config eq "ARRAY"B= @$config == 1C= @$config == 2
ABCresult
0XX0
1000
11X1
1011
841 0 $subcomponent_class = $config->[0];
842 0 ▶ $subcomponent_options = $config->[1] // {};
Condition0%0/2
$config->[1] // {}
l!l
00
MC/DC0%0/1
$config->[1] // {}
$config->[1]
Truth table0%0/2
A
A= $config->[1]
Aresult
11
00
843 }
844 else {
845 0 throw "Invalid option (\"$config_key\")";
846 }
847  
848 }
849 else {
850 0 $subcomponent_class = "$config";
851 0 $subcomponent_options = {};
852 }
853  
854 0 ▶ if ( ! defined $subcomponent_class || $subcomponent_class !~ GX::Meta::Constants::REGEX_CLASS_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subcomponent_class and $subcomponent_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/) 0 0 0
Condition0%0/3
defined $subcomponent_class and $subcomponent_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subcomponent_class and $subcomponent_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
defined $subcomponent_class $subcomponent_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
Truth table0%0/3
A and B
A= defined $subcomponent_classB= $subcomponent_class =~ /^[a-zA-Z_]\w*(?:::[a-zA-Z_]\w*)*$/
ABresult
0X0
100
111
855 0 throw "Invalid option (\"$config_key\")";
856 }
857  
858 0 ▶ if ( ref $subcomponent_options ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subcomponent_options ne "HASH") 0 0 0
859 0 throw "Invalid option (\"$config_key\")";
860 }
861  
862 0 ▶ if ( ! GX::Meta::Util::load_module( $subcomponent_class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (GX::Meta::Util::load_module($subcomponent_class)) 0 0 0
863 0 throw "Cannot load $subcomponent_class";
864 }
865  
866 0 ▶ if ( ! $subcomponent_class->isa( $subcomponent_base ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcomponent_class->isa($subcomponent_base)) 0 0 0
867 0 throw "Subcomponent must inherit from $subcomponent_base";
868 }
869  
870 0 $subcomponent = eval { $subcomponent_class->new( %$subcomponent_options ) };
871  
872 0 ▶ if ( ! $subcomponent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subcomponent) 0 0 0
873 0 GX::Exception->throw(
874 message => "Cannot instantiate $subcomponent_class",
875 subexception => $@
876 );
877 }
878  
879 }
880  
881 0 return $subcomponent;
882  
883 }
884  
885 0 ▶sub _validate_class_name {
Subroutine _validate_class_name: not called CC=1 SCAR=6.9
886  
887 0 return $_[1] =~ /^(?:[_a-zA-Z]\w*::)+?Session(?:::[_a-zA-Z]\w*)+$/;
888  
889 }
890  
891  
892 1 1;
893  
894 __END__
« GX/Router.pm ↑ Summary GX/Script/Build.pm »