Eve/Session.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 100% 64 / 64 CC 4 SCAR 3.8
CC
4
Cov
100%
init
2.0
set_parameter
2.0
_flush
2.0
CRAP 1.5
« Eve/Uri.pm ↑ Summary Eve/HttpOutput.pm »
1 package Eve::Session;
2  
3 2 ▶use parent qw(Eve::Class);
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use CGI::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 B<Eve::Session> - a persistent session class.
13  
14 =head1 SYNOPSIS
15  
16 # Construct the session object
17 my $session = Eve::Session->new(
18 id => $md5_id,
19 storage_path => '/storage/path',
20 expiration_interval => 3600);
21  
22 # Get the session identifier
23 my $id = $session->get_id();
24  
25 # Set ang get the parameter
26 $session->set_parameter(name => 'foo', value => 'bar');
27 my $foo = $session->get_parameter(name => 'foo');
28  
29 # Set the parameter expired after 600 seconds of idling
30 $session->set_parameter(
31 name => 'foo', value => 'bar', expiration_interval => 600);
32  
33 # Clear the parameter
34 $session->clear_parameter(name => 'foo');
35  
36 =head1 DESCRIPTION
37  
38 B<Eve::Session> is a persistent session class allowing to share an
39 identified state between application calls. The class adapts the
40 package B<CGI::Session>.
41  
42 =head3 Constructor arguments
43  
44 =over 4
45  
46 =item C<id>
47  
48 a session ID string (it can differ from the actual ID, see the
49 C<get_id()> method documentation)
50  
51 =item C<storage_path>
52  
53 a path on the server where session files are stored
54  
55 =item C<expiration_interval>
56  
57 an interval of idling from the last access when the session is
58 considered actual (0 cancels expiration).
59  
60 =back
61  
62 =head3 Attributes
63  
64 =over 4
65  
66 =item C<expiration_interval>
67  
68 (read only) an expiration interval in seconds that has been initially
69 set for the session.
70  
71 =back
72  
73 =head3 Throws
74  
75 =over 4
76  
77 =item C<Eve::Error::Session>
78  
79 when the session creation is unsuccessful.
80  
81 =back
82  
83 =head1 METHODS
84  
85 =head2 B<init()>
86  
87 =cut
88  
89 16 ▶sub init {
Subroutine init: called CC=2 SCAR=6.9
Pod documented
90 16 my ($self, %arg_hash) = @_;
91 16 Eve::Support::arguments(
92 \%arg_hash,
93 my ($id, $storage_path, $expiration_interval));
94  
95 16 $self->{'session'} = CGI::Session->new(
96 undef, $id, { Directory => $storage_path });
97  
98 16 ▶ if (not defined $self->session) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->session) 1 15 16
99 1 Eve::Error::Session->throw(message => CGI::Session->errstr());
100 }
101  
102 15 $self->session->expire($expiration_interval);
103  
104 15 $self->{'expiration_interval'} = $self->session->expire();
105  
106 15 $self->_flush();
107  
108 14 return;
109 }
110  
111 =head2 B<get_id()>
112  
113 =head3 Returns
114  
115 An md5 string identified the session. The string can be different from
116 one that is specified in the constructor in case of not existing or
117 expired session.
118  
119 =cut
120  
121 3 ▶sub get_id {
Subroutine get_id: called CC=1 SCAR=0.0
Pod documented
122 3 my $self = shift;
123  
124 3 return $self->session->id();
125 }
126  
127 =head2 B<set_parameter()>
128  
129 Sets a named parameter in the session.
130  
131 =head3 Arguments
132  
133 =over 4
134  
135 =item C<name>
136  
137 =item C<value>
138  
139 =item C<expiration_interval>
140  
141 an interval of idling from the last access when the parameter is
142 considered actual (0 cancels expiration).
143  
144 =back
145  
146 =head3 Returns
147  
148 The value passed to the method.
149  
150 =cut
151  
152 7 ▶sub set_parameter {
Subroutine set_parameter: called CC=2 SCAR=6.9
Pod documented
153 7 my ($self, %arg_hash) = @_;
154 7 Eve::Support::arguments(
155 \%arg_hash, my ($name, $value), my $expiration_interval = \undef);
156  
157 7 my $result = $self->session->param(-name => $name, -value => $value);
158  
159 7 ▶ if (defined $expiration_interval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expiration_interval) 3 4 7
160 3 $self->session->expire($name, $expiration_interval);
161 }
162  
163 7 $self->_flush();
164  
165 7 return $result;
166 }
167  
168 =head2 B<get_parameter()>
169  
170 =head3 Arguments
171  
172 =over 4
173  
174 =item C<name>
175  
176 =back
177  
178 =head3 Returns
179  
180 A parameter value.
181  
182 =cut
183  
184 6 ▶sub get_parameter {
Subroutine get_parameter: called CC=1 SCAR=0.0
Pod documented
185 6 my ($self, %arg_hash) = @_;
186 6 Eve::Support::arguments(\%arg_hash, my $name);
187  
188 6 return $self->session->param($name);
189 }
190  
191 =head2 B<clear_parameter()>
192  
193 Clears the parameter from the session.
194  
195 =head3 Arguments
196  
197 =over 4
198  
199 =item C<name>
200  
201 =back
202  
203 =head3 Returns
204  
205 An old parameter value.
206  
207 =cut
208  
209 2 ▶sub clear_parameter {
Subroutine clear_parameter: called CC=1 SCAR=0.0
Pod documented
210 2 my ($self, %arg_hash) = @_;
211 2 Eve::Support::arguments(\%arg_hash, my $name);
212  
213 2 my $result = $self->session->param($name);
214 2 $self->session->clear($name);
215  
216 2 $self->_flush();
217  
218 2 return $result;
219 }
220  
221 24 ▶sub _flush {
Subroutine _flush: called CC=2 SCAR=6.9
222 24 my $self = shift;
223  
224 24 ▶ if (not $self->session->flush()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->session->flush) 1 23 24
225 1 Eve::Error::Session->throw(message => CGI::Session->errstr());
226 }
227 }
228  
229 =head1 SEE ALSO
230  
231 =over 4
232  
233 =item L<CGI::Session>
234  
235 =back
236  
237 =head1 LICENSE AND COPYRIGHT
238  
239 Copyright 2012 Igor Zinovyev.
240  
241 This program is free software; you can redistribute it and/or modify it
242 under the terms of either: the GNU General Public License as published
243 by the Free Software Foundation; or the Artistic License.
244  
245 See http://dev.perl.org/licenses/ for more information.
246  
247  
248 =head1 AUTHOR
249  
250 L<Sergey Konoplev|mailto:gray.ru@gmail.com>
251  
252 =cut
253  
254 2 1;
« Eve/Uri.pm ↑ Summary Eve/HttpOutput.pm »