blib/lib/PAGI/Middleware/Session/State/Callback.pm

filter: Statementstmt 84.0% 21 / 25 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 4 / 4 totaltotal 84.0% 37 / 44 CC 5 SCAR 9.7
CC
5
Cov
82%
clear
6.0
new
3.0
inject
2.0
CRAP 2.6
« blib/lib/PAGI/Headers.pm ↑ Summary blib/lib/PAGI/Middleware.pm »
1 package PAGI::Middleware::Session::State::Callback;
2 1 $PAGI::Middleware::Session::State::Callback::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware::Session::State';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::Middleware::Session::State::Callback - Custom coderef-based session ID transport
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::Middleware::Session::State::Callback;
14  
15 my $state = PAGI::Middleware::Session::State::Callback->new(
16 extract => sub {
17 my ($scope) = @_;
18 # Return session ID or undef
19 return $scope->{headers}[0][1];
20 },
21 inject => sub {
22 my ($headers, $id, $options) = @_;
23 push @$headers, ['X-Session-ID', $id];
24 },
25 );
26  
27 # Extract session ID from request
28 my $id = $state->extract($scope);
29  
30 =head1 DESCRIPTION
31  
32 Implements the L<PAGI::Middleware::Session::State> interface using custom
33 coderefs for session ID extraction and injection. This allows callers to
34 define arbitrary session ID transport without writing a subclass.
35  
36 =head1 CONFIGURATION
37  
38 =over 4
39  
40 =item * extract (required)
41  
42 A coderef that receives C<($scope)> and returns the session ID or undef.
43  
44 =item * inject (optional)
45  
46 A coderef that receives C<(\@headers, $id, \%options)> and modifies the
47 response headers. Defaults to a no-op if not provided.
48  
49 =item * clear (optional)
50  
51 A coderef that receives C<(\@headers)> and clears the client-side
52 session state. Called when a session is destroyed. Defaults to a
53 no-op if not provided.
54  
55 =back
56  
57 =cut
58  
59 10 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
60 10 my ($class, %options) = @_;
61  
62 die "extract is required for $class"
63 10 ▶ unless defined $options{extract};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"extract"} 1 9 10
64 die "extract must be a CODE ref for $class"
65 9 ▶ unless ref($options{extract}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $options{"extract"} eq "CODE" 3 6 9
66  
67 6 return $class->SUPER::new(%options);
68 }
69  
70 =head2 extract
71  
72 my $session_id = $state->extract($scope);
73  
74 Calls the configured C<extract> coderef with C<$scope> and returns
75 its result.
76  
77 =cut
78  
79 3 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
Pod documented
80 3 my ($self, $scope) = @_;
81  
82 3 return $self->{extract}->($scope);
83 }
84  
85 =head2 inject
86  
87 $state->inject(\@headers, $id, \%options);
88  
89 Calls the configured C<inject> coderef with C<(\@headers, $id, \%options)>
90 if one was provided. Otherwise does nothing.
91  
92 =cut
93  
94 3 ▶sub inject {
Subroutine inject: called CC=2 SCAR=6.9
Pod documented
95 3 my ($self, $headers, $id, $options) = @_;
96  
97 3 ▶ if ($self->{inject}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inject"}) 2 1 3
98 2 $self->{inject}->($headers, $id, $options);
99 }
100  
101 3 return;
102 }
103  
104 =head2 clear
105  
106 $state->clear(\@headers);
107  
108 Calls the configured C<clear> coderef with C<(\@headers)> if one was
109 provided. Otherwise does nothing.
110  
111 =cut
112  
113 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
Pod documented
114 0 my ($self, $headers) = @_;
115  
116 0 ▶ if ($self->{clear}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"clear"}) 0 0 0
117 0 return $self->{clear}->($headers);
118 }
119  
120 0 return;
121 }
122  
123 1 1;
124  
125 __END__
« blib/lib/PAGI/Headers.pm ↑ Summary blib/lib/PAGI/Middleware.pm »