Sessions.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 93.7% 15 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 6 / 6 totaltotal 93.9% 78 / 83 CC 10 SCAR 9.4
CC
10
Cov
95%
new
4.0
new_session
3.0
get_session
3.0
CRAP 2.6
« Sessions/Session.pm ↑ Summary Sessions/Base.pm »
1 package Concierge::Sessions v0.11.3;
2 4 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Session manager with flexible session information storage
5  
6 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Concierge::Sessions::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $DEFAULT_SESSION_TIMEOUT = 3600;
11  
12 # Sessions manager
13 74 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
14 74 my ($class, %args) = @_;
15  
16 my $backend_class = delete $args{backend_class}
17 74 ▶ or croak "Concierge::Sessions->new requires a 'backend_class' class name";
Branch: 1 branch
BranchTrueFalseTotal
my $backend_class = delete $args{"backend_class"} or croak("Concierge::Sessions->new requires a 'backend_class' class name") 73 1 74
18  
19 73 ▶ eval "require $backend_class; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $backend_class; 1" or croak("Cannot load Sessions backend ${backend_class}: $@") 72 1 73
20 or croak "Cannot load Sessions backend $backend_class: $@";
21  
22 72 my $backend;
23 72 eval {
24 72 $backend = $backend_class->new(%args);
25 };
26 72 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 72 72
27 0 croak "Failed to initialize backend $backend_class: $@";
28 }
29  
30 72 my $self = bless {
31 storage => $backend,
32 }, $class;
33  
34 72 return $self;
35 }
36  
37 # Session object
38 69 ▶sub new_session {
Subroutine new_session: called CC=3 SCAR=11.0
Pod documented
39 69 my ($self, %args) = @_;
40  
41 69 ▶ unless ($args{user_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"user_id"}) 3 66 69
42 3 return { success => 0, message => "user_id required to create a new session" };
43 }
44  
45 66 ▶ $args{session_timeout} ||= $DEFAULT_SESSION_TIMEOUT;
Condition66%2/3
$args{"session_timeout"} ||= $Concierge::Sessions::DEFAULT_SESSION_TIMEOUT
l!l&&r!l&&!r
22440
MC/DC0%0/2
$args{"session_timeout"} ||= $Concierge::Sessions::DEFAULT_SESSION_TIMEOUT
$args{"session_timeout"} $Concierge::Sessions::DEFAULT_SESSION_TIMEOUT
Truth table66%2/3
A ||= B
A= $args{"session_timeout"}B= $Concierge::Sessions::DEFAULT_SESSION_TIMEOUT
ABresult
1X1
011
000
46 66 $args{storage} = $self->{storage};
47  
48 66 my $session_result = Concierge::Sessions::Session->new( %args );
49  
50 66 return $session_result;
51 }
52  
53 26 ▶sub get_session {
Subroutine get_session: called CC=3 SCAR=11.0
Pod documented
54 26 my ($self, $session_id) = @_;
55  
56 26 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 1 25 26
57 1 return { success => 0, message => "Session ID required to retrieve a session" };
58 }
59  
60 # Load session info from backend
61 25 my $result = $self->{storage}->get_session_info($session_id);
62  
63 25 ▶ unless ($result->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->{"success"}) 8 17 25
64 8 return { success => 0, message => "get_session_info: " . $result->{message} };
65 }
66  
67 17 my %ses_args = $result->{info}->%*;
68 17 $ses_args{storage} = $self->{storage};
69  
70 # Instantiate Refreshed Session object
71 17 my $session = bless { %ses_args }, 'Concierge::Sessions::Session';
72  
73 17 return { success => 1, session => $session };
74 }
75  
76 # Administrative methods - handled by backends
77  
78 # delete sessions that have expired
79 3 ▶sub cleanup_sessions {
Subroutine cleanup_sessions: called CC=1 SCAR=0.0
Pod documented
80 3 my ($self) = @_;
81 3 return $self->{storage}->cleanup_sessions();
82 }
83  
84 # delete session by session_id
85 6 ▶sub delete_session {
Subroutine delete_session: called CC=2 SCAR=6.9
Pod documented
86 6 my ($self, $session_id) = @_;
87  
88 6 ▶ unless ($session_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id) 1 5 6
89 1 return { success => 0, message => "Session ID required to delete a session" };
90 }
91  
92 5 return $self->{storage}->delete_session($session_id);
93 }
94  
95 # delete session by user_id
96 # sub delete_user_sessions {
97 3 ▶sub delete_user_session {
Subroutine delete_user_session: called CC=2 SCAR=6.9
Pod documented
98 3 my ($self, $user_id) = @_;
99  
100 3 ▶ unless ($user_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user_id) 1 2 3
101 1 return { success => 0, message => "user_id required to delete user sessions" };
102 }
103  
104 2 return $self->{storage}->delete_user_session($user_id);
105 }
106  
107  
108 4 1;
109  
110 __END__
« Sessions/Session.pm ↑ Summary Sessions/Base.pm »