blib/lib/PAGI/Utils.pm

filter: Statementstmt 100% 51 / 51 Branchbran 96.4% 27 / 28 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 95.2% 100 / 105 CC 18 SCAR 19.3
CC
18
Cov
97%
to_app
12.0
handle_lifespan
5.0
is_response
3.0
CRAP 6.9
« blib/lib/PAGI/Middleware/WebSocket/RateLimit.pm ↑ Summary blib/lib/PAGI/Request/Negotiate.pm »
1 package PAGI::Utils;
2 35 $PAGI::Utils::VERSION = '0.002002';
3 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 35 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 35 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 35 ▶use PAGI::Lifespan;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 35 our @EXPORT_OK = qw(handle_lifespan to_app is_response);
12 35 our %EXPORT_TAGS = (all => \@EXPORT_OK);
13  
14 # True if $x is a PAGI response value: a blessed object that can respond($send).
15 # The single source of truth for the "did the handler return a response?" check
16 # (used by the endpoint and router dispatch paths).
17 53 ▶sub is_response {
Subroutine is_response: called CC=3 SCAR=11.0
Pod documented
18 53 my ($x) = @_;
19 53 ▶ return blessed($x) && $x->can('respond') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($x) && $x->can('respond') ? : 45 8 53
Condition100%3/3
builtin::blessed($x) and $x->can("respond")
!ll&&!rl&&r
7145
MC/DC100%2/2
builtin::blessed($x) and $x->can("respond")
builtin::blessed($x) $x->can("respond")
Truth table100%3/3
A and B
A= builtin::blessed($x)B= $x->can("respond")
ABresult
0X0
100
111
20 }
21  
22 6 ▶async sub handle_lifespan {
Subroutine handle_lifespan: called CC=5 SCAR=16.2
Pod documented
23 6 my ($scope, $receive, $send, %opts) = @_;
24  
25 6 ▶ my $type = $scope->{type} // '';
Condition50%1/2
$scope->{"type"} // ""
l!l
60
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
26 6 ▶ croak "handle_lifespan called with scope type '$type' (expected 'lifespan'). "
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "lifespan" 1 5 6
27 . "Check scope type before calling: "
28 . "return await handle_lifespan(...) if \$scope->{type} eq 'lifespan'"
29 unless $type eq 'lifespan';
30  
31 5 my $manager = PAGI::Lifespan->for_scope($scope);
32 5 ▶ $manager->register(%opts) if $opts{startup} || $opts{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"startup"} or $opts{"shutdown"} 4 1 5
Condition66%2/3
$opts{"startup"} or $opts{"shutdown"}
l!l&&r!l&&!r
401
MC/DC50%1/2
$opts{"startup"} or $opts{"shutdown"}
$opts{"startup"} $opts{"shutdown"}
Truth table66%2/3
A or B
A= $opts{"startup"}B= $opts{"shutdown"}
ABresult
1X1
011
000
33  
34 5 return await $manager->handle($scope, $receive, $send);
35 35 }
36  
37 339 ▶sub to_app {
Subroutine to_app: called CC=12 SCAR=24.9
Pod documented
38 339 my ($thing) = @_;
39  
40 339 ▶ croak "to_app() requires an app, component, or class name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 1 338 339
41 unless defined $thing;
42  
43 338 ▶ return $thing if ref($thing) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $thing eq "CODE" 305 33 338
44  
45 33 ▶ if (blessed($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing)) 20 13 33
46 20 ▶ return $thing->to_app if $thing->can('to_app');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("to_app") 18 2 20
47 2 ▶ croak ref($thing) . " looks like middleware, not an app"
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("wrap") 1 1 2
48 . " - pass it to enable(), or wrap an app with ->wrap(\$app)"
49 if $thing->can('wrap');
50 1 croak "Cannot coerce " . ref($thing) . " object to a PAGI app (no to_app method)";
51 }
52  
53 13 ▶ if (!ref($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $thing) 12 1 13
54 12 ▶ croak "Cannot coerce '$thing' to a PAGI app"
Branch: 1 branch
BranchTrueFalseTotal
unless $thing =~ /\A\w+(?:::\w+)*\z/ 1 11 12
55 unless $thing =~ /\A\w+(?:::\w+)*\z/;
56 11 ▶ unless ($thing->can('to_app')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($thing->can("to_app")) 5 6 11
57 5 local $@;
58 5 ▶ eval "require $thing; 1" or croak "Failed to load '$thing': $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $thing; 1" or croak("Failed to load '${thing}': $@") 3 2 5
59 }
60 9 ▶ return $thing->to_app if $thing->can('to_app');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("to_app") 8 1 9
61 1 ▶ croak "'$thing' looks like middleware, not an app - pass it to enable()"
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("wrap") 0 1 1
62 if $thing->can('wrap');
63 1 croak "'$thing' does not have a to_app() method";
64 }
65  
66 1 croak "Cannot coerce " . ref($thing) . " reference to a PAGI app";
67 }
68  
69 35 1;
70  
71 __END__
« blib/lib/PAGI/Middleware/WebSocket/RateLimit.pm ↑ Summary blib/lib/PAGI/Request/Negotiate.pm »