blib/lib/Dancer/Session/Redis.pm

filter: Statementstmt 60.6% 37 / 61 Branchbran 35.7% 5 / 14 Conditioncond 42.8% 9 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 69.2% 9 / 13 Podpod 100% 6 / 6 totaltotal 52.7% 67 / 127 CC 17 SCAR 27.8
CC
17
Cov
53%
redis
42.0
init
16.9
retrieve
2.0
CRAP 16.2
↑ Summary
1 package Dancer::Session::Redis;
2  
3 # ABSTRACT: Redis backend for Dancer Session Engine
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use parent 'Dancer::Session::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Redis 1.955;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.22'; # VERSION
14 3 our $AUTHORITY = 'cpan:CHIM'; # AUTHORITY
15  
16 3 my $_redis;
17 3 my %options = ();
18  
19 3 ▶sub init {
Subroutine init: called CC=12 SCAR=28.3
Pod documented
20 3 my $self = shift;
21  
22 3 $self->SUPER::init(@_);
23  
24 # backend settings
25 3 ▶ if (my $opts = setting('redis_session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = setting('redis_session')) { } 2 1 3
26 2 ▶ if (ref $opts and ref $opts eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts and ref $opts eq 'HASH') { } 1 1 2
Condition66%2/3
ref $opts and ref $opts eq "HASH"
!ll&&!rl&&r
011
MC/DC50%1/2
ref $opts and ref $opts eq "HASH"
ref $opts ref $opts eq "HASH"
Truth table66%2/3
A and B
A= ref $optsB= ref $opts eq "HASH"
ABresult
0X0
100
111
27 %options = (
28 server => $opts->{server} || undef,
29 sock => $opts->{sock} || undef,
30 database => $opts->{database} || 0,
31 expire => $opts->{expire} || 900,
32 debug => $opts->{debug} || 0,
33 password => $opts->{password} || undef,
34 1 ▶ );
Condition50%6/12
$opts->{'server'} || undef
l!l
01
$opts->{'sock'} || undef
l!l
01
$opts->{'database'} || 0
l!l
01
$opts->{'expire'} || 900
l!l
01
$opts->{'debug'} || 0
l!l
01
$opts->{'password'} || undef
l!l
01
MC/DC0%0/1
$opts->{'server'} || undef
$opts->{'server'}
MC/DC0%0/1
$opts->{'sock'} || undef
$opts->{'sock'}
MC/DC0%0/1
$opts->{'database'} || 0
$opts->{'database'}
MC/DC0%0/1
$opts->{'expire'} || 900
$opts->{'expire'}
MC/DC0%0/1
$opts->{'debug'} || 0
$opts->{'debug'}
MC/DC0%0/1
$opts->{'password'} || undef
$opts->{'password'}
Truth table50%1/2
A
A= $opts->{'server'}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{'sock'}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{'database'}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{'expire'}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{'debug'}
Aresult
11
00
Truth table50%1/2
A
A= $opts->{'password'}
Aresult
11
00
35 }
36 else {
37 1 Carp::croak 'Settings redis_session must be a hash reference!';
38 }
39 }
40 else {
41 1 Carp::croak 'Settings redis_session is not defined!';
42 }
43  
44 1 ▶ unless (defined $options{server} || defined $options{sock}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"server"} or defined $options{"sock"}) 1 0 1
Condition33%1/3
defined $options{"server"} or defined $options{"sock"}
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $options{"server"} or defined $options{"sock"}
defined $options{"server"} defined $options{"sock"}
Truth table33%1/3
A or B
A= defined $options{"server"}B= defined $options{"sock"}
ABresult
1X1
011
000
45 1 Carp::croak 'Settings redis_session should include either server or sock parameter!';
46 }
47  
48 # get radis handle
49 0 $self->redis;
50 }
51  
52 # create a new session
53 3 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
54 3 my ($class) = @_;
55  
56 3 $class->new->flush;
57 }
58  
59 # fetch the session object by id
60 0 ▶sub retrieve($$) {
Subroutine retrieve: not called CC=1 SCAR=6.9
Pod documented
61 0 my ($class, $id) = @_;
62  
63 0 my $self = $class->new;
64 0 $self->redis->select($options{database});
65 0 $self->redis->expire($id => $options{expire});
66  
67 0 Storable::thaw($self->redis->get($id));
68 }
69  
70 # delete session
71 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
72 0 my ($self) = @_;
73  
74 0 $self->redis->select($options{database});
75 0 $self->redis->del($self->id);
76 }
77  
78 # flush session
79 0 ▶sub flush {
Subroutine flush: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self) = @_;
81  
82 0 $self->redis->select($options{database});
83 0 $self->redis->set($self->id => Storable::freeze($self));
84 0 $self->redis->expire($self->id => $options{expire});
85  
86 0 $self;
87 }
88  
89 # get redis handle
90 0 ▶sub redis {
Subroutine redis: not called CC=6 SCAR=37.4
Pod documented
91 0 my ($self) = @_;
92  
93 0 ▶ if (!$_redis || !$_redis->ping) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_redis and $_redis->ping) 0 0 0
Condition0%0/3
$_redis and $_redis->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$_redis and $_redis->ping
$_redis $_redis->ping
Truth table0%0/3
A and B
A= $_redisB= $_redis->ping
ABresult
0X0
100
111
94 my %params = (
95 debug => $options{debug},
96 0 reconnect => 10,
97 every => 100,
98 );
99  
100 0 ▶ if (defined $options{sock}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'sock'}) { } 0 0 0
101 0 $params{sock} = $options{sock};
102 }
103 else {
104 0 $params{server} = $options{server};
105 }
106  
107 0 ▶ $params{password} = $options{password} if $options{password};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"password"} 0 0 0
108  
109 0 $_redis = Redis->new(%params);
110 }
111  
112 0 ▶ $_redis and return $_redis;
Branch: 1 branch
BranchTrueFalseTotal
$_redis and return $_redis 0 0 0
113  
114 0 Carp::croak "Unable connect to redis-server...";
115 }
116  
117 3 1; # End of Dancer::Session::Redis
118  
119 __END__
↑ Summary