Neaf/X/Session/Base.pm

filter: Statementstmt 83.1% 59 / 71 Branchbran 57.1% 16 / 28 Conditioncond 37.9% 11 / 29 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 85.7% 12 / 14 Podpod 100% 9 / 9 totaltotal 64.5% 109 / 169 CC 26 SCAR 26.4
CC
26
Cov
67%
load_session
35.6
save_session
6.3
new
4.4
CRAP 14.0
« Neaf/CLI.pm ↑ Summary Neaf/Route.pm »
1 package MVC::Neaf::X::Session::Base;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 our $VERSION = '0.2901';
6  
7 =head1 DESCRIPTION
8  
9 MVC::Neaf::X::Session::Base - session engine base class & tooling for
10 Not Even A Framework.
11  
12 =head1 SINOPSYS
13  
14 package My::Session::Engine;
15 use parent qw(MVC::Neaf::X::Session::Base);
16  
17 sub store { ... };
18 sub fetch { ... };
19  
20 1;
21  
22 =head1 METHODS
23  
24 =cut
25  
26 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 ▶use MVC::Neaf::Util qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use parent qw(MVC::Neaf::X::Session);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 =head2 new( %options )
32  
33 %options may include:
34  
35 =over
36  
37 =item * session_ttl - how long until session expires (not given = don't expire).
38  
39 =item * session_renewal_ttl - how long until session is forcibly re-saved
40 and updated.
41 Defaults to session_ttl * some_fraction.
42 0 means don't do this at all.
43  
44 =back
45  
46 =cut
47  
48 4 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
49 4 my $class = shift;
50  
51 4 my $self = $class->SUPER::new( @_ );
52  
53 4 ▶ if (!defined $self->{session_ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"session_ttl"}) 0 4 4
54 0 $self->{session_ttl} = 7*24*60*60; # default expiration to a week
55 };
56  
57 4 ▶ if (!defined $self->{session_renewal_ttl}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"session_renewal_ttl"}) 4 0 4
58 4 my $ttl = $self->session_ttl;
59 4 ▶ $self->{session_renewal_ttl} = ($ttl || 0) * 0.875; # 7/8 of expiration
Condition50%1/2
$ttl || 0
l!l
40
MC/DC0%0/1
$ttl || 0
$ttl
Truth table50%1/2
A
A= $ttl
Aresult
11
00
60 };
61  
62 4 return $self;
63 };
64  
65 =head2 save_session( $id, $hash )
66  
67 Save session data. Returns hash with keys id and expire.
68 Returned id MAY differ from the given one, and must be honored in such case.
69  
70 =cut
71  
72 5 ▶sub save_session {
Subroutine save_session: called CC=5 SCAR=18.3
Pod documented
73 5 my ($self, $id, $obj) = @_;
74  
75 5 my $str = $self->encode( $obj );
76 5 ▶ $id ||= $self->get_session_id;
Condition33%1/3
$id ||= $self->get_session_id
l!l&&r!l&&!r
500
MC/DC0%0/2
$id ||= $self->get_session_id
$id $self->get_session_id
Truth table33%1/3
A ||= B
A= $idB= $self->get_session_id
ABresult
1X1
011
000
77  
78 5 my $hash = $self->store( $id, $str, $obj );
79  
80 5 ▶ $self->my_croak("Failed to save session (unknown reason)")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 5 5
81 unless (ref $hash eq 'HASH');
82  
83 5 ▶ $hash->{id} ||= $id;
Condition33%1/3
$hash->{"id"} ||= $id
l!l&&r!l&&!r
500
MC/DC0%0/2
$hash->{"id"} ||= $id
$hash->{"id"} $id
Truth table33%1/3
A ||= B
A= $hash->{"id"}B= $id
ABresult
1X1
011
000
84 5 ▶ $hash->{expire} ||= $self->get_expire;
Condition33%1/3
$hash->{"expire"} ||= $self->get_expire
l!l&&r!l&&!r
050
MC/DC0%0/2
$hash->{"expire"} ||= $self->get_expire
$hash->{"expire"} $self->get_expire
Truth table33%1/3
A ||= B
A= $hash->{"expire"}B= $self->get_expire
ABresult
1X1
011
000
85  
86 5 return $hash;
87 };
88  
89 =head2 load_session( $id )
90  
91 Load session by id. A hash containing session data, id, and expiration
92 time is returned.
93  
94 Session is re-saved if time has come to update it.
95  
96 =cut
97  
98 7 ▶sub load_session {
Subroutine load_session: called CC=13 SCAR=35.7
Pod documented
99 7 my ($self, $id) = @_;
100  
101 7 my $hash = $self->fetch( $id );
102 7 ▶ return unless ref $hash eq 'HASH' and ($hash->{strfy} or $hash->{override});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" and $hash->{"strfy"} || $hash->{"override"} 2 5 7
Condition50%3/6
ref $hash eq "HASH" and $hash->{"strfy"} || $hash->{"override"}
!ll&&!rl&&r
205
$hash->{"strfy"} or $hash->{"override"}
l!l&&r!l&&!r
500
MC/DC33%1/3
ref $hash eq "HASH" and $hash->{"strfy"} || $hash->{"override"}
ref $hash eq "HASH" $hash->{"strfy"} $hash->{"override"}
Truth table50%2/4
A and B or C
A= ref $hash eq "HASH"B= $hash->{"strfy"}C= $hash->{"override"}
ABCresult
0XX0
1000
11X1
1011
103  
104 # extract real data and apply overrides if any
105 5 ▶ $hash->{data} = $hash->{strfy} ? $self->decode( $hash->{strfy} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$hash->{'strfy'} ? : 5 0 5
106 5 ▶ if ($hash->{override}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"override"}) 0 5 5
107 $hash->{data}{$_} = $hash->{override}{$_}
108 0 for keys %{ $hash->{override} };
109 };
110  
111 # data would be nonepty if strfy is decoded OR at least one override present
112 5 ▶ return unless $hash->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash->{"data"} 0 5 5
113  
114 # expired = return empty & cleanup
115 5 ▶ if ($hash->{expire} and $hash->{expire} < time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{"expire"} and $hash->{"expire"} < time) 0 5 5
Condition66%2/3
$hash->{"expire"} and $hash->{"expire"} < time
!ll&&!rl&&r
230
MC/DC0%0/2
$hash->{"expire"} and $hash->{"expire"} < time
$hash->{"expire"} $hash->{"expire"} < time
Truth table66%2/3
A and B
A= $hash->{"expire"}B= $hash->{"expire"} < time
ABresult
0X0
100
111
116 0 $self->delete_session( $id );
117 0 return;
118 };
119  
120 5 ▶ if ($self->need_renewal( $hash->{expire} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->need_renewal($hash->{"expire"})) 0 5 5
121 0 my $update = $self->save_session( $id, $hash->{data} );
122 0 ▶ $hash->{id} = $update->{id} || $id;
Condition0%0/3
$update->{'id'} || $id
l!l&&r!l&&!r
000
MC/DC0%0/2
$update->{'id'} || $id
$update->{'id'} $id
Truth table0%0/3
A || B
A= $update->{'id'}B= $id
ABresult
1X1
011
000
123 0 ▶ $hash->{expire} = $update->{expire} || $self->get_expire;
Condition0%0/3
$update->{'expire'} || $self->get_expire
l!l&&r!l&&!r
000
MC/DC0%0/2
$update->{'expire'} || $self->get_expire
$update->{'expire'} $self->get_expire
Truth table0%0/3
A || B
A= $update->{'expire'}B= $self->get_expire
ABresult
1X1
011
000
124 };
125  
126 # just return fetched data
127 5 return $hash;
128 };
129  
130 =head2 get_expire ( $time || time )
131  
132 Caclulate expiration time, if applicable.
133  
134 =cut
135  
136 8 ▶sub get_expire {
Subroutine get_expire: called CC=3 SCAR=11.3
Pod documented
137 8 my ($self, $time) = @_;
138  
139 8 my $ttl = $self->session_ttl;
140 8 ▶ return unless $ttl;
Branch: 1 branch
BranchTrueFalseTotal
unless $ttl 0 8 8
141  
142 8 ▶ $time = time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 8 0 8
143 8 return $time + $ttl;
144 };
145  
146 =head2 need_renewal( $time )
147  
148 Tell if session expiring by $time needs to be renewed.
149  
150 =cut
151  
152 5 ▶sub need_renewal {
Subroutine need_renewal: called CC=3 SCAR=11.0
Pod documented
153 5 my ($self, $time) = @_;
154  
155 5 my $ttl = $self->{session_renewal_ttl};
156  
157 5 ▶ return ($time && $ttl) ? ($time < time + $ttl) : ('');
Branch: 1 branch
BranchTrueFalseTotal
$time && $ttl ? : 3 2 5
Condition66%2/3
$time and $ttl
!ll&&!rl&&r
203
MC/DC50%1/2
$time and $ttl
$time $ttl
Truth table66%2/3
A and B
A= $timeB= $ttl
ABresult
0X0
100
111
158 };
159  
160 =head2 encode
161  
162 =cut
163  
164 5 ▶sub encode {
Subroutine encode: called CC=2 SCAR=7.0
Pod documented
165 5 my ($self, $data) = @_;
166 5 my $str = eval { encode_json( $data ) };
167 5 ▶ carp "Failed to encode session data: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
168 if $@;
169 5 return $str;
170 };
171  
172 =head2 decode
173  
174 =cut
175  
176 5 ▶sub decode {
Subroutine decode: called CC=2 SCAR=7.0
Pod documented
177 5 my ($self, $data) = @_;
178 5 my $obj = eval { decode_json( $data ) };
179 5 ▶ carp "Failed to encode session data: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
180 if $@;
181 5 return $obj;
182 };
183  
184 =head2 fetch ($id)
185  
186 Stub, to be redefined by real storage access method.
187 Return is expected as { data => stringified_session }.
188  
189 =cut
190  
191 0 ▶sub fetch {
Subroutine fetch: not called CC=1 SCAR=6.9
Pod documented
192 0 my ($self, $id) = @_;
193  
194 0 $self->my_croak("unimplemented");
195 };
196  
197 =head2 store( $id, $stringified_data, $data_as_is)
198  
199 Stub, to be redefined by real storage access method.
200  
201 Must return false value or a hash with following fields (all optional):
202  
203 =over
204  
205 =item * id - indicates that id has changed and/or client session needs update;
206  
207 =item * expire - indicates that expiration date has changed and/or needs update;
208  
209 =item * strfy - stringified session data;
210  
211 =item * override - hash with individual fields that would override
212 decoded content.
213  
214 =back
215  
216 =cut
217  
218 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod documented
219 0 my ($self, $id, $data_str, $data_real) = @_;
220  
221 0 $self->my_croak("unimplemented");
222 };
223  
224 =head1 LICENSE AND COPYRIGHT
225  
226 This module is part of L<MVC::Neaf> suite.
227  
228 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
229  
230 This program is free software; you can redistribute it and/or modify it
231 under the terms of either: the GNU General Public License as published
232 by the Free Software Foundation; or the Artistic License.
233  
234 See L<http://dev.perl.org/licenses/> for more information.
235  
236 =cut
237  
238 6 1;
« Neaf/CLI.pm ↑ Summary Neaf/Route.pm »