| 1 | package Concierge::Sessions::Session v0.11.3; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Individual session objects created by Concierge::Sessions | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 66 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 66 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 66 | my $backend = $args{storage}; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 66 | my $create_result = $backend->create_session( %args ); | |||||||||||||||||||||||||||
| 15 | 66 | ▶ | unless ($create_result->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 0 | return { success => 0, message => "new_session failure: " . $create_result->{message} }; | |||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Retrieve the full created session info with timestamps, etc. | ||||||||||||||||||||||||||||
| 20 | 66 | my $get_result = $backend->get_session_info( $create_result->{session_id} ); | |||||||||||||||||||||||||||
| 21 | 66 | ▶ | unless ($get_result->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | return { success => 0, message => "new_session info failure: " . $get_result->{message} }; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Create Session object | ||||||||||||||||||||||||||||
| 26 | my $self = bless { | ||||||||||||||||||||||||||||
| 27 | $get_result->{info}->%*, | ||||||||||||||||||||||||||||
| 28 | 66 | storage => $backend, | |||||||||||||||||||||||||||
| 29 | }, __PACKAGE__; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 66 | return { success => 1, session => $self }; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Data access methods - work with entire data field | ||||||||||||||||||||||||||||
| 35 | 25 | ▶ | sub get_data { | ||||||||||||||||||||||||||
|
Subroutine
get_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Return entire data field | ||||||||||||||||||||||||||||
| 39 | 25 | my $value = $self->{data}; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 25 | return { success => 1, value => $value }; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 23 | ▶ | sub set_data { | ||||||||||||||||||||||||||
|
Subroutine
set_data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 23 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # $value replaces entire data field | ||||||||||||||||||||||||||||
| 48 | 23 | $self->{data} = $value; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Only changed in memory, not in storage | ||||||||||||||||||||||||||||
| 51 | 23 | $self->{status}{dirty} = 1; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 23 | return { success => 1 }; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # Persistence method for app data, timestamped | ||||||||||||||||||||||||||||
| 57 | 18 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Check if dirty | ||||||||||||||||||||||||||||
| 61 | 18 | ▶ | my $dirty = $self->{status}{dirty} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'status'}{'dirty'} || 0
MC/DC100%1/1
$self->{'status'}{'dirty'} || 0
$self->{'status'}{'dirty'}
Truth table100%2/2
A
A= $self->{'status'}{'dirty'}
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 18 | ▶ | unless ($dirty) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 4 | return { success => 1 }; # Fine if not dirty | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Calculate new expiration time (sliding window extension) | ||||||||||||||||||||||||||||
| 68 | 14 | my $timeout = $self->{session_timeout}; | |||||||||||||||||||||||||||
| 69 | 14 | my $new_expires_at; | |||||||||||||||||||||||||||
| 70 | 14 | ▶ | if (defined $timeout && $timeout eq 'indefinite') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $timeout and $timeout eq "indefinite"
MC/DC50%1/2
defined $timeout and $timeout eq "indefinite"
defined $timeout
$timeout eq "indefinite"
Truth table66%2/3
A and B
A= defined $timeoutB= $timeout eq "indefinite"
| |||||||||||||||||||||||||||||
| 71 | 1 | $new_expires_at = 'indefinite'; | |||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||
| 73 | 13 | $new_expires_at = time() + $timeout; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Update internal expires_at | ||||||||||||||||||||||||||||
| 77 | 14 | $self->{expires_at} = $new_expires_at; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Save session data changes and new expiration time | ||||||||||||||||||||||||||||
| 80 | my $result = $self->{storage}->update_session( | ||||||||||||||||||||||||||||
| 81 | $self->{session_id}, | ||||||||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||||||||
| 83 | data => $self->{data}, | ||||||||||||||||||||||||||||
| 84 | 14 | expires_at => $new_expires_at, | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 14 | ▶ | unless ($result->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | return { success => 0, message => "save: " . $result->{message} }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Clear dirty flag | ||||||||||||||||||||||||||||
| 93 | 14 | $self->{status}{dirty} = 0; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 14 | return { success => 1 }; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Read-only Status booleans | ||||||||||||||||||||||||||||
| 99 | 6 | ▶ | sub is_valid { | ||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 101 | 6 | ▶ | return ($self->is_active() && !$self->is_expired()) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->is_active and not $self->is_expired
MC/DC100%2/2
$self->is_active and not $self->is_expired
$self->is_active
not $self->is_expired
Truth table100%3/3
A and not(B)
A= $self->is_activeB= not $self->is_expired
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 12 | ▶ | sub is_active { | ||||||||||||||||||||||||||
|
Subroutine
is_active: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 107 | 12 | ▶ | my $state = $self->{status}{state} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'status'}{'state'} || ''
MC/DC100%1/1
$self->{'status'}{'state'} || ''
$self->{'status'}{'state'}
Truth table100%2/2
A
A= $self->{'status'}{'state'}
| |||||||||||||||||||||||||||||
| 108 | 12 | ▶ | return $state eq 'active' ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 13 | ▶ | sub is_expired { | ||||||||||||||||||||||||||
|
Subroutine
is_expired: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 13 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | # Indefinite sessions never expire | ||||||||||||||||||||||||||||
| 114 | 13 | ▶ | return 0 if $self->{expires_at} eq 'indefinite'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 9 | ▶ | return (time() > $self->{expires_at}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 16 | ▶ | sub is_dirty { | ||||||||||||||||||||||||||
|
Subroutine
is_dirty: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 16 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | 16 | ▶ | return $self->{status}{dirty} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'status'}{'dirty'} || 0
MC/DC100%1/1
$self->{'status'}{'dirty'} || 0
$self->{'status'}{'dirty'}
Truth table100%2/2
A
A= $self->{'status'}{'dirty'}
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # Read-only system info | ||||||||||||||||||||||||||||
| 124 | 25 | ▶ | sub session_id { | ||||||||||||||||||||||||||
|
Subroutine
session_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 25 | my ($self) = @_; | |||||||||||||||||||||||||||
| 126 | 25 | return $self->{session_id}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | sub storage_backend { | ||||||||||||||||||||||||||
|
Subroutine
storage_backend: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 131 | 1 | return ref($self->{storage}); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | ▶ | sub created_at { | ||||||||||||||||||||||||||
|
Subroutine
created_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 5 | $self->{created_at}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 10 | ▶ | sub expires_at { | ||||||||||||||||||||||||||
|
Subroutine
expires_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 141 | 10 | $self->{expires_at}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 3 | ▶ | sub last_updated { | ||||||||||||||||||||||||||
|
Subroutine
last_updated: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 146 | 3 | return $self->{last_updated}; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 5 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 151 | 5 | ▶ | return $self->{status} || { state => 'active', dirty => 0 }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'status'} || {'state', 'active', 'dirty', 0}
MC/DC100%1/1
$self->{'status'} || {'state', 'active', 'dirty', 0}
$self->{'status'}
Truth table100%2/2
A
A= $self->{'status'}
| |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 4 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | __END__ | ||||||||||||||||||||||||||||