| 1 | package Web::ComposableRequest::Session; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Web::ComposableRequest::Constants qw( COMMA EXCEPTION_CLASS FALSE NUL TRUE); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Web::ComposableRequest::Util qw( bson64id is_arrayref throw ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Unexpected::Types qw( ArrayRef Bool HashRef | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | NonEmptySimpleStr NonZeroPositiveInt | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | Object SimpleStr Undef ); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # Public attributes | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | has 'authenticated' => is => 'rw', isa => Bool, default => FALSE; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | has 'messages' => is => 'ro', isa => HashRef[ArrayRef], builder => sub { {} }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_messages: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | has 'updated' => is => 'ro', isa => NonZeroPositiveInt, required => TRUE; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | has 'username' => is => 'rw', isa => SimpleStr, default => NUL; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Private attributes | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | has '_config' => is => 'ro', isa => Object, init_arg => 'config', | |||||||||||||||||||||||||||||||||||||||||
| 23 | required => TRUE; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | has '_mid' => is => 'rwp', isa => NonEmptySimpleStr | Undef; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | has '_request' => is => 'ro', isa => Object, init_arg => 'request', | |||||||||||||||||||||||||||||||||||||||||
| 28 | required => TRUE, weak_ref => TRUE; | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | has '_session' => is => 'ro', isa => HashRef, init_arg => 'session', | |||||||||||||||||||||||||||||||||||||||||
| 31 | required => TRUE; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | # Construction | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | around 'BUILDARGS' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 6 | my ($orig, $self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | my $attr = $orig->($self, @args); | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | for my $k (_session_attr($attr->{config})) { | |||||||||||||||||||||||||||||||||||||||||
| 40 | 29 | my $v = $attr->{session}->{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 29 | ▶ | $attr->{$k} = $v if defined $v; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | ▶ | $attr->{updated} //= time; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$attr->{"updated"} //= time
MC/DC100%1/1
$attr->{"updated"} //= time
$attr->{"updated"}
Truth table100%2/2
A
A= $attr->{"updated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | return $attr; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 6 | my $conf = $self->_config; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 6 | my $max_time = $conf->max_sess_time; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 6 | ▶ | if ($self->authenticated and $max_time | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$self->authenticated and $max_time and time > $self->updated + $max_time
$self->authenticated and $max_time
MC/DC66%2/3
$self->authenticated and $max_time and time > $self->updated + $max_time
$self->authenticated
$max_time
time > $self->updated + $max_time
Truth table75%3/4
A and B and C
A= $self->authenticatedB= $max_timeC= time > $self->updated + $max_time
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | and time > $self->updated + $max_time) { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $req = $self->_request; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | my $msg = $conf->expire_session->($self, $req); | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $self->authenticated(FALSE); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | $self->_set__mid($self->add_status_message($msg)); | |||||||||||||||||||||||||||||||||||||||||
| 61 | $req->_log->({ | ||||||||||||||||||||||||||||||||||||||||||
| 62 | level => 'debug', | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | message => $req->loc_default(@{$msg}), | |||||||||||||||||||||||||||||||||||||||||
| 64 | name => 'Session.build' | ||||||||||||||||||||||||||||||||||||||||||
| 65 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Public methods | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | sub add_status_message { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_status_message: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | my ($self, $msg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | is_arrayref $msg or throw 'Parameter [_1] not an array reference', [$msg]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | $self->messages->{ my $mid = bson64id } = $msg; | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | return $mid; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | sub collect_message_id { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collect_message_id: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | return $self->_mid && exists $self->messages->{$self->_mid} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_mid and exists $self->messages->{$self->_mid}
MC/DC100%2/2
$self->_mid and exists $self->messages->{$self->_mid}
$self->_mid
exists $self->messages->{$self->_mid}
Truth table100%3/3
A and B
A= $self->_midB= exists $self->messages->{$self->_mid}
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | ? $self->_mid : $req->query_params->('mid', { optional => TRUE }); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | sub collect_status_message { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collect_status_message: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | for my $mid ($self->_mid, $req->query_params->('mid', { optional => TRUE })){ | |||||||||||||||||||||||||||||||||||||||||
| 93 | 5 | ▶ | next unless $mid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | ▶ | my $msg = $self->messages->{$mid} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | delete $self->messages->{$mid} if $self->_config->delete_on_collect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | return $req->loc(@{$msg}); | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub collect_status_messages { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
collect_status_messages: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | my @messages = (); | |||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | my $mid = $req->query_params->('mid', { optional => TRUE }) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | or return \@messages; | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | my @keys = reverse sort keys %{$self->messages}; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | while (my $key = shift @keys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | next if $key gt $mid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | my $msg = $self->messages->{$key} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | ▶ | delete $self->messages->{$key} if $self->_config->delete_on_collect; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 5 | push @messages, $req->loc(@{$msg}); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | return \@messages; | |||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub serialise { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialise: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my $string = NUL; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | for my $attr (@{$self->_config->serialise_session_attr}) { | |||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $string .= COMMA . "${attr}:" . $self->$attr; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 4 | ▶ | sub trim_message_queue { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trim_message_queue: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | my @queue = sort keys %{$self->messages}; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 4 | ▶ | while (@queue > $self->_config->max_messages) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | my $mid = shift @queue; | |||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | delete $self->messages->{$mid}; | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 4 | for my $k (_session_attr($self->_config)) { | |||||||||||||||||||||||||||||||||||||||||
| 153 | 20 | $self->_session->{$k} = $self->$k(); | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | $self->_session->{updated} = time; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 4 | ▶ | before 'update' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | my $self = shift; $self->trim_message_queue; return; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | # Private functions | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 10 | ▶ | sub _session_attr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_session_attr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 10 | my $conf = shift; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 10 | my @public = qw( authenticated messages updated username ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 10 | return keys %{$conf->session_attr}, @public; | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||||||||||||||||