blib/lib/Plack/Middleware/EasyHooks.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 80.0% 32 / 40 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 94.7% 18 / 19 Podpod 75.0% 6 / 8 totaltotal 90.3% 141 / 156 CC 23 SCAR 11.6
CC
23
Cov
90%
handle_full_response
6.0
__ANON__
5.0
call
4.6
CRAP 3.2
↑ Summary
1 package Plack::Middleware::EasyHooks;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use parent qw(Plack::Middleware);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION = '0.02';
10  
11 20 ▶sub before {
Subroutine before: called CC=2 SCAR=6.9
Pod documented
12 20 my ($self, $env) = @_;
13  
14 20 ▶ $self->{before}->($env) if defined $self->{before};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"before"} 5 15 20
15 }
16  
17 20 ▶sub after {
Subroutine after: called CC=2 SCAR=6.9
Pod documented
18 20 my ($self, $env, $res) = @_;
19  
20 20 ▶ $self->{after}->($env, $res) if defined $self->{after};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"after"} 15 5 20
21 }
22  
23 36 ▶sub filter {
Subroutine filter: called CC=2 SCAR=6.9
Pod documented
24 36 my ($self, $env, $chunk) = @_;
25  
26 36 ▶ $chunk = $self->{filter}->($env, $chunk) if defined $self->{filter};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"filter"} 9 27 36
27  
28 36 return $chunk;
29 }
30  
31 20 ▶sub tail {
Subroutine tail: called CC=2 SCAR=6.9
Pod documented
32 20 my ($self, $env) = @_;
33  
34 20 ▶ $self->{tail}->($env) if defined $self->{tail};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tail"} 5 15 20
35 }
36  
37 20 ▶sub finalize {
Subroutine finalize: called CC=2 SCAR=6.9
Pod documented
38 20 my ($self, $env) = @_;
39  
40 20 ▶ $self->{finalize}->($env) if defined $self->{finalize};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"finalize"} 5 15 20
41 }
42  
43 25 ▶sub call {
Subroutine call: called CC=4 SCAR=15.2
Pod documented
44 25 my ($self, $env) = @_;
45  
46 25 $self->before($env);
47 25 my $res = $self->app->($env);
48  
49 25 ▶ if ( $env->{"psgix.cleanup"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.cleanup"}) 0 25 25
50 0 ▶ push @{ $env->{"psgix.cleanup.handlers"} }, sub { $self->finalize($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 }
52  
53 25 ▶ if ( ref $res eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "ARRAY") 15 10 25
54 15 return $self->handle_full_response($env, $res);
55 }
56  
57 10 ▶ if ( ref $res eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "CODE") 10 0 10
58 10 return $self->handle_delayed_response($env, $res);
59 }
60  
61 # Unknown response type...
62 0 return $res;
63 }
64  
65 20 ▶sub handle_full_response {
Subroutine handle_full_response: called CC=6 SCAR=17.9
Pod undocumented
66 20 my ($self, $env, $res) = @_;
67  
68 20 my $override = $self->after($env, $res);
69 20 ▶ if (defined($override) && ref $override eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $override and ref $override eq "ARRAY") 4 16 20
Condition100%3/3
defined $override and ref $override eq "ARRAY"
!ll&&!rl&&r
4124
MC/DC100%2/2
defined $override and ref $override eq "ARRAY"
defined $override ref $override eq "ARRAY"
Truth table100%3/3
A and B
A= defined $overrideB= ref $override eq "ARRAY"
ABresult
0X0
100
111
70 4 $res->[0] = $override->[0];
71 4 $res->[1] = $override->[1];
72 4 $res->[2] = $override->[2];
73 }
74  
75 20 my ($status, $header, $body) = @$res;
76  
77 20 ▶ if (ref $body eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq "ARRAY") 10 10 20
78 10 my $tail;
79 $body = [
80 10 ▶ (map { $self->filter($env, $_) } @$body),
Branch: 1 branch
BranchTrueFalseTotal
defined($tail = $self->tail($env)) ? : 10 0 10
81 (defined($tail = $self->tail($env)) ? $tail : ())
82 ];
83  
84 10 ▶ $self->finalize($env) unless $env->{'psgix.cleanup'};
Branch: 1 branch
BranchTrueFalseTotal
unless $env->{"psgix.cleanup"} 10 0 10
85 10 return [ $status, $header, $body ];
86 }
87  
88 10 my $done;
89 my $wrapped = Plack::Util::inline_object(
90 35 ▶ getline => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
91 35 ▶ return if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 10 25 35
92  
93 25 my $chunk = $body->getline();
94  
95 25 ▶ if (defined $chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chunk) 15 10 25
96 15 return $self->filter($env, $chunk);
97 }
98  
99 10 $done = 1;
100 10 return $self->tail($env);
101 },
102 10 ▶ close => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
103 10 $body->close;
104  
105 10 ▶ $self->finalize($env) unless $env->{"psgix.cleanup"};
Branch: 1 branch
BranchTrueFalseTotal
unless $env->{"psgix.cleanup"} 10 0 10
106 },
107 10 );
108  
109 10 return [ $status, $header, $wrapped ];
110 }
111  
112 10 ▶sub handle_delayed_response {
Subroutine handle_delayed_response: called CC=1 SCAR=0.0
Pod undocumented
113 10 my ($self, $env, $res) = @_;
114  
115 10 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 10 my $responder = shift;
117  
118 $res->(
119 10 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
120 10 my ($res) = @_;
121  
122 10 ▶ if (exists( $res->[2] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $res->[2]) 5 5 10
123 5 return $responder->( $self->handle_full_response($env, $res) );
124 }
125  
126 5 my $override = $self->after($env, $res);
127 5 ▶ if (defined($override) && ref $override eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $override and ref $override eq "ARRAY") 1 4 5
Condition100%3/3
defined $override and ref $override eq "ARRAY"
!ll&&!rl&&r
131
MC/DC100%2/2
defined $override and ref $override eq "ARRAY"
defined $override ref $override eq "ARRAY"
Truth table100%3/3
A and B
A= defined $overrideB= ref $override eq "ARRAY"
ABresult
0X0
100
111
128 1 $res->[0] = $override->[0];
129 1 $res->[1] = $override->[1];
130 1 ▶ $res->[2] = $override->[2] if exists $override->[2];
Branch: 1 branch
BranchTrueFalseTotal
if exists $override->[2] 1 0 1
131 }
132  
133 5 my $writer = $responder->( $res );
134  
135 return Plack::Util::inline_object(
136 10 ▶ write => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
137 10 $writer->write( $self->filter($env, @_) );
138 },
139 5 ▶ close => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
140 5 my $tail = $self->tail();
141 5 ▶ $writer->write($tail) if defined($tail);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tail 5 0 5
142  
143 5 ▶ $self->finalize($env) unless $env->{'psgix.cleanup'};
Branch: 1 branch
BranchTrueFalseTotal
unless $env->{"psgix.cleanup"} 5 0 5
144 5 $writer->close;
145 },
146 5 );
147 }
148 10 );
149 10 };
150 }
151  
152  
153 5 1;
154  
155 __END__
↑ Summary