blib/lib/Web/Machine/FSM.pm

filter: Statementstmt 82.6% 86 / 104 Branchbran 64.2% 27 / 42 Conditioncond 64.2% 9 / 14 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 90.0% 18 / 20 Podpod 80.0% 4 / 5 totaltotal 77.2% 149 / 193 CC 29 SCAR 27.4
CC
29
Cov
76%
__ANON__
40.8
run
6.1
filter_response
5.0
CRAP 15.5
↑ Summary blib/lib/Web/Machine/Util.pm »
1 package Web::Machine::FSM;
2 # ABSTRACT: The State Machine runner
3  
4 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 19 our $VERSION = '0.17';
8  
9 19 ▶use IO::Handle::Util 'io_from_getline';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 19 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use HTTP::Status qw[ is_error ];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use Web::Machine::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use Web::Machine::FSM::States qw[
Subroutine BEGIN: called CC=1 SCAR=0.0
15 start_state
16 is_status_code
17 is_new_state
18 get_state_name
19 get_state_desc
20 19 ];
21  
22 50 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
23 50 my ($class, %args) = @_;
24 bless {
25 tracing => !!$args{'tracing'},
26 50 ▶ tracing_header => $args{'tracing_header'} || 'X-Web-Machine-Trace'
Condition50%1/2
$args{'tracing_header'} || 'X-Web-Machine-Trace'
l!l
050
MC/DC0%0/1
$args{'tracing_header'} || 'X-Web-Machine-Trace'
$args{'tracing_header'}
Truth table50%1/2
A
A= $args{'tracing_header'}
Aresult
11
00
27 } => $class
28 }
29  
30 138 ▶sub tracing { (shift)->{'tracing'} }
Subroutine tracing: called CC=1 SCAR=0.0
Pod documented
31 270 ▶sub tracing_header { (shift)->{'tracing_header'} }
Subroutine tracing_header: called CC=1 SCAR=0.0
Pod documented
32  
33 138 ▶sub run {
Subroutine run: called CC=5 SCAR=18.1
Pod documented
34 138 my ( $self, $resource ) = @_;
35  
36 138 my $DEBUG;
37 138 ▶ if ( $ENV{WM_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"WM_DEBUG"}) 0 138 138
38 $DEBUG
39 = $ENV{WM_DEBUG} eq 'diag'
40 0 ▶ ? sub { Test::More::diag( $_[0] ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 ▶ : sub { warn "$_[0]\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'WM_DEBUG'} eq 'diag' ? : 0 0 0
42 }
43  
44 138 my $request = $resource->request;
45 138 my $response = $resource->response;
46 138 my $metadata = {};
47 138 $request->env->{'web.machine.context'} = $metadata;
48  
49 138 my @trace;
50 138 my $tracing = $self->tracing;
51  
52 138 my $state = start_state;
53  
54 138 ▶ try {
Subroutine __ANON__: called CC=16 SCAR=37.1
55 138 while (1) {
56 2866 ▶ $DEBUG->( 'entering '
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 2866 2866
57 . get_state_name($state) . ' ('
58 . get_state_desc($state)
59 . ')' )
60 if $DEBUG;
61 2866 ▶ push @trace => get_state_name( $state ) if $tracing;
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 1792 1074 2866
62 2866 my $result = $state->( $resource, $request, $response, $metadata );
63 2862 ▶ if ( ! ref $result ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $result) { } 0 2862 2862
elsif (is_status_code($result)) { } 134 2728 2862
elsif (is_new_state($result)) { } 2728 0 2728
64 # TODO:
65 # We should be I18N this
66 # specific error
67 # - SL
68 0 ▶ $DEBUG->( '! ERROR with ' . ( $result || 'undef' ) )
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
Condition0%0/2
$result || 'undef'
l!l
00
MC/DC0%0/1
$result || 'undef'
$result
Truth table0%0/2
A
A= $result
Aresult
11
00
69 if $DEBUG;
70 0 $response->status( 500 );
71 0 $response->header( 'Content-Type' => 'text/plain' );
72 0 ▶ $response->body( [ "Got bad state: " . ($result || 'undef') ] );
Condition0%0/2
$result || 'undef'
l!l
00
MC/DC0%0/1
$result || 'undef'
$result
Truth table0%0/2
A
A= $result
Aresult
11
00
73 0 last;
74 }
75 elsif ( is_status_code( $result ) ) {
76 0 ▶ $DEBUG->( '.. terminating with ' . ${$result} ) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 134 134
77 134 $response->status( $$result );
78  
79 134 ▶ if ( is_error( $$result ) && !$response->body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_error($$result) and not $response->body) 60 74 134
Condition100%3/3
&is_error($$result) and not $response->body
!ll&&!rl&&r
73160
MC/DC100%2/2
&is_error($$result) and not $response->body
&is_error($$result) not $response->body
Truth table100%3/3
A and not(B)
A= &is_error($$result)B= not $response->body
ABresult
0X0
100
111
80 # NOTE:
81 # this will default to en, however I
82 # am not really confident that this
83 # will end up being sufficient.
84 # - SL
85 my $lang = Web::Machine::I18N->get_handle( $metadata->{'Language'} || 'en' )
86 60 ▶ or die "Could not get language handle for " . $metadata->{'Language'};
Branch: 1 branch
BranchTrueFalseTotal
my $lang = "Web::Machine::I18N"->get_handle($metadata->{"Language"} || "en") or die 'Could not get language handle for ' . $metadata->{"Language"} 60 0 60
Condition100%2/2
$metadata->{'Language'} || 'en'
l!l
1446
MC/DC100%1/1
$metadata->{'Language'} || 'en'
$metadata->{'Language'}
Truth table100%2/2
A
A= $metadata->{'Language'}
Aresult
11
00
87 60 $response->header( 'Content-Type' => 'text/plain' );
88 60 $response->body([ $lang->maketext( $$result ) ]);
89 }
90  
91 134 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 134 134
92 0 require Data::Dumper;
93 0 local $Data::Dumper::Terse = 1;
94 0 local $Data::Dumper::Indent = 1;
95 0 local $Data::Dumper::Useqq = 1;
96 0 local $Data::Dumper::Deparse = 1;
97 0 local $Data::Dumper::Quotekeys = 0;
98 0 local $Data::Dumper::Sortkeys = 1;
99 0 $DEBUG->( Data::Dumper::Dumper( $request->env ) );
100 0 $DEBUG->( Data::Dumper::Dumper( $response->finalize ) );
101 }
102  
103 134 last;
104 }
105 elsif ( is_new_state( $result ) ) {
106 2728 ▶ $DEBUG->( '-> transitioning to ' . get_state_name($result) )
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 2728 2728
107 if $DEBUG;
108 2728 $state = $result;
109 }
110 }
111 4 ▶ } catch {
Subroutine __ANON__: called CC=3 SCAR=11.0
112 # TODO:
113 # We should be I18N the errors
114 # - SL
115 4 ▶ $DEBUG->($_) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 4 4
116  
117 4 ▶ if ( $request->logger ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->logger) 1 3 4
118 1 $request->logger->( { level => 'error', message => $_ } );
119 }
120  
121 4 $response->status( 500 );
122  
123 # NOTE:
124 # this way you can handle the
125 # exception if you like via
126 # the finish_request call below
127 # - SL
128 4 $metadata->{'exception'} = $_;
129 138 };
130  
131 $self->filter_response( $resource )
132 138 ▶ unless $request->env->{'web.machine.streaming_push'};
Branch: 1 branch
BranchTrueFalseTotal
unless $request->env->{"web.machine.streaming_push"} 133 5 138
133 138 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
134 138 $resource->finish_request( $metadata );
135 }
136 1 ▶ catch {
Subroutine __ANON__: called CC=3 SCAR=11.4
137 1 ▶ $DEBUG->($_) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 1 1
138  
139 1 ▶ if ( $request->logger ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->logger) 1 0 1
140 1 $request->logger->( { level => 'error', message => $_ } );
141 }
142  
143 1 $response->status( 500 );
144 138 };
145 138 ▶ $response->header( $self->tracing_header, (join ',' => @trace) )
Branch: 1 branch
BranchTrueFalseTotal
if $tracing 92 46 138
146 if $tracing;
147  
148 138 $response;
149 }
150  
151 133 ▶sub filter_response {
Subroutine filter_response: called CC=5 SCAR=16.1
Pod undocumented
152 133 my $self = shift;
153 133 my ($resource) = @_;
154  
155 133 my $response = $resource->response;
156 133 my $filters = $resource->request->env->{'web.machine.content_filters'};
157  
158 # XXX patch Plack::Response to make _body not private?
159 133 my $body = $response->_body;
160  
161 133 for my $filter (@$filters) {
162 39 ▶ if (ref($body) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq 'ARRAY') { } 26 13 39
163 26 $response->body( [ map { $filter->($_) } @$body ] );
164 26 $body = $response->body;
165 }
166 else {
167 13 my $old_body = $body;
168 13 ▶ $body = io_from_getline sub { $filter->($old_body->getline) };
Subroutine __ANON__: called CC=1 SCAR=0.0
169 13 $response->body($body);
170 }
171 }
172  
173 133 ▶ if (ref($body) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $body eq "ARRAY" and not Plack::Util::status_with_no_entity_body($response->status)) 109 24 133
Condition100%3/3
ref $body eq "ARRAY" and not Plack::Util::status_with_no_entity_body($response->status)
!ll&&!rl&&r
1311109
MC/DC100%2/2
ref $body eq "ARRAY" and not Plack::Util::status_with_no_entity_body($response->status)
ref $body eq "ARRAY" not Plack::Util::status_with_no_entity_body($response->status)
Truth table100%3/3
A and not(B)
A= ref $body eq "ARRAY"B= not Plack::Util::status_with_no_entity_body($response->status)
ABresult
0X0
100
111
174 && !Plack::Util::status_with_no_entity_body($response->status)) {
175 109 $response->header(
176 'Content-Length' => Plack::Util::content_length($body)
177 );
178 }
179 }
180  
181 19 1;
182  
183 __END__
↑ Summary blib/lib/Web/Machine/Util.pm »