File Coverage

blib/lib/Ado/Sessions/Database.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 10 80.0
condition 2 2 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 48 50 96.0


line stmt bran cond sub pod time code
1             package Ado::Sessions::Database;
2 14     14   744 use Mojo::Base 'Ado::Sessions';
  14         33  
  14         81  
3 14     14   2172 use Mojo::JSON;
  14         28  
  14         683  
4 14     14   74 use Mojo::Util qw(b64_decode b64_encode);
  14         31  
  14         686  
5 14     14   552 use Ado::Model::Sessions;
  14         32  
  14         3816  
6              
7             sub load {
8 123     123 1 64852 my ($self, $c) = @_;
9              
10 123         259 my $session = {};
11 123   100     669 my $id = $self->session_id($c) || '';
12              
13 123 100       18824 if ($id) {
14 63         430 my $adosession = Ado::Model::Sessions->find($id);
15 63 50       9027 if ($adosession->data) {
16             return
17 63 50       8006 unless $session = Mojo::JSON::j(b64_decode $adosession->sessiondata);
18             }
19             }
20              
21 123         15730 return $self->prepare_load($c, $session);
22             }
23              
24             sub store {
25 111     111 1 131955 my ($self, $c) = @_;
26              
27 111         687 my ($id, $session) = $self->prepare_store($c);
28 111 100       373 return unless $id;
29 78         376 my $value = b64_encode(Mojo::JSON::encode_json($session), '');
30 78         10375 my $adosession = Ado::Model::Sessions->find($id);
31 78 100       11180 if ($adosession->data) {
32 63         7418 $adosession->sessiondata($value)->update();
33 63         14561 return;
34             }
35 15         1757 Ado::Model::Sessions->create(id => $id, tstamp => time(), sessiondata => $value);
36              
37 15         79 return;
38             }
39              
40             1;
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             Ado::Sessions::Database - manage sessions stored in the database
49              
50             =head1 DESCRIPTION
51              
52             L manages sessions for
53             L. All data gets serialized with L and stored
54             C encoded in the database. A cookie or a request parameter can
55             be used to share the session id between the server and the user agents.
56              
57             =head1 METHODS
58              
59             =head2 load
60              
61             Load session data from database.
62              
63             =head2 store
64              
65             Save session data in database.
66              
67             =head1 SEE ALSO
68              
69             L, L
70              
71             =cut