blib/lib/PAGI/Utils.pm

filter: Statementstmt 100% 49 / 49 Branchbran 96.1% 25 / 26 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 94.6% 89 / 94 CC 16 SCAR 19.9
CC
16
Cov
96%
to_app
12.0
handle_lifespan
5.0
BEGIN
1.0
CRAP 7.3
« blib/lib/PAGI/Middleware/ConditionalGet.pm ↑ Summary blib/lib/PAGI/Middleware/WebSocket/RateLimit.pm »
1 package PAGI::Utils;
2 26 $PAGI::Utils::VERSION = '0.002000';
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 26 ▶use PAGI::Lifespan;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 26 our @EXPORT_OK = qw(handle_lifespan to_app);
12 26 our %EXPORT_TAGS = (all => \@EXPORT_OK);
13  
14 6 ▶async sub handle_lifespan {
Subroutine handle_lifespan: called CC=5 SCAR=16.2
Pod documented
15 6 my ($scope, $receive, $send, %opts) = @_;
16  
17 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
18 6 ▶ croak "handle_lifespan called with scope type '$type' (expected 'lifespan'). "
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "lifespan" 1 5 6
19 . "Check scope type before calling: "
20 . "return await handle_lifespan(...) if \$scope->{type} eq 'lifespan'"
21 unless $type eq 'lifespan';
22  
23 5 my $manager = PAGI::Lifespan->for_scope($scope);
24 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
25  
26 5 return await $manager->handle($scope, $receive, $send);
27 26 }
28  
29 sub to_app {
30 333 ▶ my ($thing) = @_;
Subroutine to_app: called CC=12 SCAR=24.9
Pod documented
31  
32 333 ▶ croak "to_app() requires an app, component, or class name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 1 332 333
33 unless defined $thing;
34  
35 332 ▶ return $thing if ref($thing) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $thing eq "CODE" 299 33 332
36  
37 33 ▶ if (blessed($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($thing)) 20 13 33
38 20 ▶ return $thing->to_app if $thing->can('to_app');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("to_app") 18 2 20
39 2 ▶ croak ref($thing) . " looks like middleware, not an app"
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("wrap") 1 1 2
40 . " - pass it to enable(), or wrap an app with ->wrap(\$app)"
41 if $thing->can('wrap');
42 1 croak "Cannot coerce " . ref($thing) . " object to a PAGI app (no to_app method)";
43 }
44  
45 13 ▶ if (!ref($thing)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $thing) 12 1 13
46 12 ▶ croak "Cannot coerce '$thing' to a PAGI app"
Branch: 1 branch
BranchTrueFalseTotal
unless $thing =~ /\A\w+(?:::\w+)*\z/ 1 11 12
47 unless $thing =~ /\A\w+(?:::\w+)*\z/;
48 11 ▶ unless ($thing->can('to_app')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($thing->can("to_app")) 5 6 11
49 5 local $@;
50 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}': $@") 2 3 5
51 }
52 9 ▶ return $thing->to_app if $thing->can('to_app');
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("to_app") 8 1 9
53 1 ▶ croak "'$thing' looks like middleware, not an app - pass it to enable()"
Branch: 1 branch
BranchTrueFalseTotal
if $thing->can("wrap") 0 1 1
54 if $thing->can('wrap');
55 1 croak "'$thing' does not have a to_app() method";
56 }
57  
58 1 croak "Cannot coerce " . ref($thing) . " reference to a PAGI app";
59 }
60  
61 26 1;
62  
63 __END__
« blib/lib/PAGI/Middleware/ConditionalGet.pm ↑ Summary blib/lib/PAGI/Middleware/WebSocket/RateLimit.pm »