RWDE/Web/Session.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 173 CC 10 SCAR 27.6
CC
10
Cov
0%
AUTOLOAD
56.0
keys
6.0
dump
6.0
CRAP 15.9
1 ## @file
2 # (Enter your file info here)
3 #
4 # @copy 2007 MailerMailer LLC
5 # $Id: Session.pm 465 2008-05-10 01:54:58Z damjan $
6  
7 ## @class RWDE::Web::Session
8 # (Enter RWDE::Web::Session info here)
9 package RWDE::Web::Session;
10  
11 use strict;
12 use warnings;
13  
14 use Apache::Session::Postgres;
15 use Error qw(:try);
16  
17 use RWDE::Exceptions;
18  
19 use base qw(RWDE::DB::DefaultDB);
20  
21 use vars qw($VERSION);
22 $VERSION = sprintf "%d", q$Revision: 572 $ =~ /(\d+)/;
23  
24 # Session values are retrieved and set using the session hash key names,
25 # just as if you were accessing the hash directly.
26 #
27 # <% USE s = session %>
28 # <% GET s.something %>
29 # <% SET s.whatever = 234 %>
30 # @return Session
31 ## @cmethod object new()
32 # Returns a container for the session hash from the request object
33 # pnotes section.
34 # @return
35 sub new {
36 my ($class, $session_id) = @_;
37  
38 my $self = { verbose => 0 };
39  
40 bless $self, $class;
41  
42 my %session;
43 my $dbh = $self->get_dbh();
44  
45 # Check if the key is in database; raises an exception if unable to create
46 # if no session_id supplied, creates a new entry
47 tie %session, 'Apache::Session::Postgres', $session_id,
48 {
49 Handle => $dbh,
50 Commit => 0,
51 };
52  
53 $self->{_session} = \%session
54 or throw RWDE::Web::SessionMissingException({ info => 'No session found.' });
55  
56 return $self;
57 }
58  
59 sub get_ccrcontext {
60 my ($self, $params) = @_;
61
62 return 7;
63 }
64 ## @method void remove()
65 # (Enter remove info here)
66 sub remove {
67 my ($self, $params) = @_;
68  
69 my %session = %{ $self->{_session} }
70 or throw RWDE::Web::SessionMissingException({ info => 'No session found.' });
71  
72 tied(%{ $self->{_session} })->delete(); # session failure ignored here.
73  
74 return;
75 }
76  
77 sub set_limited {
78 my ($self, $params) = @_;
79
80 $self->{_limited} = 1;
81
82 return;
83 }
84  
85 sub set_not_limited {
86 my ($self, $params) = @_;
87
88 $self->{_limited} = undef;
89
90 return;
91 }
92  
93 ## @method void verbose()
94 # Turn on/off debugging to the error_log/STDERR. Sets verbosity to
95 # $level. Currently only boolean.
96 sub verbose {
97 my ($self, $v) = @_;
98  
99 $self->{verbose} = $v || 0;
100 warn "session $self verbose => $v\n";
101  
102 return;
103 }
104  
105 ## @method object keys()
106 # Returns the keys of the session hash.
107 # @return
108 sub keys {
109 my $self = shift;
110  
111 #if we are in a limitted mode
112 if (defined $self->{_limited}){
113 return CORE::keys %{ $self->{_session}->{limited_storage} };
114 }
115 else{
116 return CORE::keys %{ $self->{_session} };
117 }
118 }
119  
120 ## @method object retrieve($session_id)
121 # (Enter retrieve info here)
122 # @param session_id (Enter explanation for param here)
123 # @return (Enter explanation for return value--Session--here)
124 sub retrieve {
125 my ($self, $params) = @_;
126  
127 my $session;
128  
129 try {
130 my $session_id = $$params{session_id}
131 or throw RWDE::DataNotFoundException({ info => 'No session_id specified' });
132  
133 #Retrive the session
134 $session = $self->new($session_id);
135 }
136  
137 catch Error with {
138 my $ex = shift;
139  
140 throw RWDE::DataNotFoundException({ info => 'Could not retrieve the session: ' . $ex });
141 };
142  
143 return $session;
144 }
145  
146 ## @method void dump()
147 # (Enter dump info here)
148 sub dump {
149 my $self = shift;
150  
151 require Data::Dumper; # pull in only if we're in verbose mode.
152 $Data::Dumper::Terse = 1;
153 foreach my $k (CORE::keys %{ $self->{_session} }) {
154 if (ref $self->{_session}->{$k}) {
155 warn "session $self var $k => " . Data::Dumper::Dumper($self->{_session}->{$k}) . "\n";
156 }
157 else {
158 warn "session $self var $k => $self->{_session}->{$k}\n";
159 }
160 }
161  
162 return;
163 }
164  
165 # use autoload to set and get the fields of the session. setting a field
166 # automatically updates the "timestamp" field to force updating the backing
167 # store by Apache::Session::Postgres;
168  
169 use vars qw($AUTOLOAD);
170  
171 ## @cmethod object AUTOLOAD()
172 # (Enter AUTOLOAD info here)
173 # @return
174 sub AUTOLOAD {
175 my $self = shift;
176  
177 if (not defined $self->{_session}){
178 throw RWDE::DevelException({ info => 'No session found. '})
179 }
180  
181 my $name = $AUTOLOAD;
182 $name =~ s/.*://; # strip fully-qualified portion
183  
184 #if we are in a limitted mode
185 if (defined $self->{_limited}){
186  
187 if (@_) { # given value, so set it.
188 $self->{_session}->{limited_storage}->{$name} = $_[0];
189 $self->{_session}->{timestamp} = time;
190 warn "session $self->{_session}->{_session_id} set $name => $_[0]\n"
191 if $self->{verbose};
192 }
193 return $self->{_session}->{limited_storage}->{$name};
194 }
195
196 else{
197 if (@_) { # given value, so set it.
198  
199 $self->{_session}->{$name} = $_[0];
200 $self->{_session}->{timestamp} = time;
201 warn "session $self->{_session}->{_session_id} set $name => $_[0]\n"
202 if $self->{verbose};
203 }
204 return $self->{_session}->{$name};
205 }
206 }
207  
208 ## @cmethod void DESTROY()
209 # only here for debugging when verbose set.
210 sub DESTROY {
211 my $self = shift;
212  
213 if ($self->{verbose} and ref($self->{_session}) eq 'HASH') {
214 $self->dump();
215 }
216  
217 return;
218 }
219  
220 1;