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   824 use Mojo::Base 'Ado::Sessions';
  14         48  
  14         93  
3 14     14   2639 use Mojo::JSON;
  14         22  
  14         786  
4 14     14   71 use Mojo::Util qw(slurp spurt b64_decode b64_encode);
  14         30  
  14         1015  
5 14     14   893 use Ado::Model::Sessions;
  14         25  
  14         4479  
6              
7             sub load {
8 123     123 1 55048 my ($self, $c) = @_;
9              
10 123         267 my $session = {};
11 123   100     781 my $id = $self->session_id($c) || '';
12              
13 123 100       19687 if ($id) {
14 63         554 my $adosession = Ado::Model::Sessions->find($id);
15 63 50       11161 if ($adosession->data) {
16             return
17 63 50       9535 unless $session = Mojo::JSON::j(b64_decode $adosession->sessiondata);
18             }
19             }
20              
21 123         15721 return $self->prepare_load($c, $session);
22             }
23              
24             sub store {
25 111     111 1 138226 my ($self, $c) = @_;
26              
27 111         900 my ($id, $session) = $self->prepare_store($c);
28 111 100       482 return unless $id;
29 78         475 my $value = b64_encode(Mojo::JSON::encode_json($session), '');
30 78         10705 my $adosession = Ado::Model::Sessions->find($id);
31 78 100       13518 if ($adosession->data) {
32 63         8627 $adosession->sessiondata($value)->update();
33 63         134449 return;
34             }
35 15         1792 Ado::Model::Sessions->create(id => $id, tstamp => time(), sessiondata => $value);
36              
37 15         78 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