lib/Web/Components/Loader.pm

filter: Statementstmt 70.9% 127 / 179 Branchbran 37.5% 21 / 56 Conditioncond 16.6% 5 / 30 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 76.0% 34 / 46 (1 uncoverable) Podpod 25.0% 1 / 4 totaltotal 57.4% 189 / 331 (1 uncoverable) CC 41 SCAR 29.2
CC
41
Cov
58%
_redirect
110.0
_render_exception
42.0
_recognise_signature
10.5
CRAP 18.5
↑ Summary blib/lib/Web/Components/ConfigLoader.pm »
1 package Web::Components::Loader;
2  
3 1 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use HTTP::Status qw( HTTP_BAD_REQUEST HTTP_FOUND
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 HTTP_INTERNAL_SERVER_ERROR );
7 1 ▶use Unexpected::Types qw( ArrayRef CodeRef HashRef NonEmptySimpleStr
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 Object RequestFactory );
9 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Web::Components::Util qw( deref exception is_arrayref
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 load_components throw );
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Web::ComposableRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Web::Simple::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 requires qw( config log );
17  
18 =pod
19  
20 =encoding utf-8
21  
22 =head1 Name
23  
24 Web::Components::Loader - Loads and instantiates MVC components
25  
26 =head1 Synopsis
27  
28 package Component::Server;
29  
30 use Plack::Builder;
31 use Web::Simple;
32  
33 with 'Web::Components::Loader';
34  
35 =head1 Description
36  
37 Loads and instantiates MVC components. Searches the namespaces; C<Controller>,
38 C<Model>, and C<View> in the consuming classes library root. Any components
39 found are loaded and instantiated
40  
41 The component collection references are passed to the component constructors
42 so that a component can discover any dependent components. The collection
43 references are not fully populated when the component is instantiated so
44 attributes that default to component references should be marked as lazy
45  
46 =head1 Configuration and Environment
47  
48 This role requires C<config> and C<log> methods in the consuming class
49  
50 Defines the following attributes;
51  
52 =over 3
53  
54 =item C<factory_args>
55  
56 A code reference used to instantiate the request factory
57  
58 =cut
59  
60 1 has 'factory_args' =>
61 is => 'lazy',
62 isa => CodeRef,
63 builder => '_build_factory_args';
64  
65 =item C<controllers>
66  
67 An array reference of controller object reference sorted into C<moniker>
68 order
69  
70 =cut
71  
72 has 'controllers' =>
73 is => 'lazy',
74 isa => HashRef[Object],
75 1 ▶ default => sub { load_components 'Controller', application => $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 =item C<models>
78  
79 A hash reference of model object references
80  
81 =cut
82  
83 has 'models' =>
84 is => 'lazy',
85 isa => HashRef[Object],
86 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 my $self = shift;
88 1 my @others = (controllers => $self->controllers, views => $self->views);
89  
90 1 return load_components 'Model', application => $self, @others
91 1 };
92  
93 =item C<views>
94  
95 A hash reference of view object references
96  
97 =cut
98  
99 has 'views' =>
100 is => 'lazy',
101 isa => HashRef[Object],
102 1 ▶ default => sub { load_components 'View', application => $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
103  
104 # Private attributes
105 has '_action_suffix' =>
106 is => 'lazy',
107 isa => NonEmptySimpleStr,
108 1 ▶ default => sub { deref $_[0]->config, 'action_suffix', '_action' };
Subroutine __ANON__: called CC=1 SCAR=0.0
109  
110 has '_config_attr' =>
111 is => 'lazy',
112 isa => NonEmptySimpleStr,
113 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 my $self = shift;
115  
116 # Deprecate use of loader_attr
117 0 ▶ return $self->config->can('component_loader')
Branch: 1 branch
BranchTrueFalseTotal
$self->config->can('component_loader') ? : 0 0 0
118 ? 'component_loader' : 'loader_attr';
119 1 };
120  
121 1 has '_factory' =>
122 is => 'lazy',
123 isa => RequestFactory,
124 builder => '_build__factory',
125 handles => ['new_from_simple_request'];
126  
127 1 has '_routes' => is => 'lazy', isa => ArrayRef, builder => '_build__routes';
128  
129 has '_tunnel_method' =>
130 is => 'lazy',
131 isa => NonEmptySimpleStr,
132 1 ▶ default => sub { deref $_[0]->config, 'tunnel_method', 'from_request' };
Subroutine __ANON__: called CC=1 SCAR=0.0
133  
134 =back
135  
136 =head1 Subroutines/Methods
137  
138 =over 3
139  
140 =item C<dispatch_request>
141  
142 Installs a response filter that processes and renders the responses from
143 the controller methods
144  
145 Controller responses that do not match the expected signature are allowed to
146 bubble up
147  
148 The expected controller return value signature is;
149  
150 [ 'model_moniker/method_name', @web_simple_request_parameters ]
151  
152 The L<Web::Simple> request parameters are used to instantiate an instance of
153 L<Web::ComposableRequest::Base>
154  
155 The specified method on the model select by the moniker is called passing the
156 request object in. A hash references, the stash, is the expected response and
157 this is passed along with the request object into the view which renders the
158 response
159  
160 Array references, a L<Plack> response, are allowed to bubble up and bypass
161 the call to the view
162  
163 If the stash contains a redirect attribute then a redirect response is
164 generated. Any message intended to be viewed by the user is stored in the
165 session and is retrieved by the next request
166  
167 =cut
168  
169 0 ▶sub dispatch_request { # uncoverable subroutine
Subroutine dispatch_request: not called (uncoverable) CC=1 SCAR=0.0
Pod documented
170 # Not applied if it already exists in the consuming class
171 }
172  
173 3 ▶around 'dispatch_request' => sub { \&_filter, @{$_[1]->_routes} };
Subroutine __ANON__: called CC=1 SCAR=0.0
174  
175 # Attribute constructors
176 1 ▶sub _build_factory_args {
Subroutine _build_factory_args: called CC=1 SCAR=0.0
177 1 my $self = shift;
178 1 my $prefix = deref $self->config, 'name';
179  
180 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
181 3 my ($self, $attr) = @_;
182  
183 3 ▶ $attr->{domain_prefix} = $prefix if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 3 3
184  
185 3 return $attr;
186 1 };
187 }
188  
189 1 ▶sub _build__factory {
Subroutine _build__factory: called CC=1 SCAR=0.0
190 1 my $self = shift;
191  
192 1 return Web::ComposableRequest->new(
193 buildargs => $self->factory_args,
194 config => $self->config,
195 );
196 }
197  
198 1 ▶sub _build__routes {
Subroutine _build__routes: called CC=1 SCAR=0.0
199 1 my $self = shift;
200 1 my $controllers = $self->controllers;
201 1 my @keys = keys %{$controllers};
202  
203 1 return [ map { $controllers->{$_}->dispatch_request } sort @keys ];
204 }
205  
206 # Private functions
207 0 ▶sub _header () {
Subroutine _header: not called CC=2 SCAR=17.9
208 0 ▶ return [ 'Content-Type' => 'text/plain', @{ $_[0] // [] } ];
Condition0%0/2
$_[0] // []
l!l
00
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
209 };
210  
211 # Private methods
212 2 ▶sub _get_context {
Subroutine _get_context: called CC=2 SCAR=7.2
213 2 my ($self, $req, $moniker, $method) = @_;
214  
215 2 my $model = $self->models->{$moniker};
216 2 my $action = "${moniker}/${method}";
217 2 my $args = {
218 action => $action,
219 controllers => $self->controllers,
220 models => $self->models,
221 request => $req,
222 views => $self->views,
223 };
224  
225 2 ▶ return $model->get_context($args) if $model->can('get_context');
Branch: 1 branch
BranchTrueFalseTotal
if $model->can("get_context") 2 0 2
226  
227 0 return Web::Components::Loader::Context->new($args);
228 }
229  
230 0 ▶sub _internal_server_error {
Subroutine _internal_server_error: not called CC=1 SCAR=6.9
231 0 my ($self, $e) = @_;
232  
233 0 $self->log->error($e);
234  
235 0 return [ HTTP_INTERNAL_SERVER_ERROR, _header, [$e] ];
236 }
237  
238 2 ▶sub _parse_sig {
Subroutine _parse_sig: called CC=3 SCAR=11.7
239 2 my ($self, $args) = @_;
240  
241 0 ▶ return @{$args} if exists $self->models->{$args->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->models->{$args->[0]} 0 2 2
242  
243 2 my ($moniker, $method) = split m{ / }mx, $args->[0], 2;
244  
245 2 ▶ if (exists $self->models->{$moniker}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->models->{$moniker}) 2 0 2
246 2 shift @{$args};
247 2 return $moniker, $method, @{$args};
248 }
249  
250 0 return;
251 }
252  
253 2 ▶sub _recognise_signature {
Subroutine _recognise_signature: called CC=6 SCAR=23.5
254 2 my ($self, $args) = @_;
255  
256 return 1 if is_arrayref $args and $args->[0]
257 2 ▶ and exists $self->models->{$args->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if &is_arrayref($args) and $args->[0] and exists $self->models->{$args->[0]} 0 2 2
Condition33%2/6
&is_arrayref($args) and $args->[0] and exists $self->models->{$args->[0]}
!ll&&!rl&&r
020
&is_arrayref($args) and $args->[0]
!ll&&!rl&&r
002
MC/DC0%0/3
&is_arrayref($args) and $args->[0] and exists $self->models->{$args->[0]}
&is_arrayref($args) $args->[0] exists $self->models->{$args->[0]}
Truth table25%1/4
A and B and C
A= &is_arrayref($args)B= $args->[0]C= exists $self->models->{$args->[0]}
ABCresult
0XX0
10X0
1100
1111
258  
259 2 my ($moniker, $method) = split m{ / }mx, $args->[0], 2;
260  
261 2 ▶ return 1 if $moniker and exists $self->models->{$moniker};
Branch: 1 branch
BranchTrueFalseTotal
if $moniker and exists $self->models->{$moniker} 2 0 2
Condition33%1/3
$moniker and exists $self->models->{$moniker}
!ll&&!rl&&r
002
MC/DC0%0/2
$moniker and exists $self->models->{$moniker}
$moniker exists $self->models->{$moniker}
Truth table33%1/3
A and B
A= $monikerB= exists $self->models->{$moniker}
ABresult
0X0
100
111
262  
263 0 return 0;
264 }
265  
266 0 ▶sub _redirect {
Subroutine _redirect: not called CC=10 SCAR=47.0
267 0 my ($self, $context) = @_;
268  
269 0 my $req = $context->request;
270 0 my $stash = $context->stash;
271 0 ▶ my $code = $stash->{code} // HTTP_FOUND;
Condition0%0/2
$stash->{"code"} // 302
l!l
00
MC/DC0%0/1
$stash->{"code"} // 302
$stash->{"code"}
Truth table0%0/2
A
A= $stash->{"code"}
Aresult
11
00
272 0 my $redirect = $stash->{redirect};
273 0 my $location = $redirect->{location};
274  
275 0 ▶ if (my $message = $redirect->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = $redirect->{"message"}) 0 0 0
276 0 my $default = {should_log_messages => 1};
277 0 my $attr = deref $self->config, $self->_config_attr, $default;
278  
279 0 ▶ if ($attr->{should_log_messages} && $req->can('loc_default')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"should_log_messages"} and $req->can("loc_default")) 0 0 0
Condition0%0/3
$attr->{"should_log_messages"} and $req->can("loc_default")
!ll&&!rl&&r
000
MC/DC0%0/2
$attr->{"should_log_messages"} and $req->can("loc_default")
$attr->{"should_log_messages"} $req->can("loc_default")
Truth table0%0/3
A and B
A= $attr->{"should_log_messages"}B= $req->can("loc_default")
ABresult
0X0
100
111
280 0 ▶ my $level = $redirect->{level} ? $redirect->{level} : 'info';
Branch: 1 branch
BranchTrueFalseTotal
$redirect->{'level'} ? : 0 0 0
281  
282 0 $self->log->$level($req->loc_default(@{$message}), $context);
283 }
284  
285 0 ▶ if ($req->can('session')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->can("session")) 0 0 0
286 0 my $session = $req->session;
287  
288 0 ▶ if ($session->can('add_status_message')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->can("add_status_message")) 0 0 0
289 0 ▶ if (my $mid = $session->add_status_message($message)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mid = $session->add_status_message($message)) 0 0 0
290 0 $location->query_form($location->query_form, 'mid' => $mid);
291 }
292 }
293 }
294 }
295  
296 0 ▶ my $headers = [ %{$redirect->{http_headers} // {}}, 'Location', $location ];
Condition0%0/2
$redirect->{"http_headers"} // {}
l!l
00
MC/DC0%0/1
$redirect->{"http_headers"} // {}
$redirect->{"http_headers"}
Truth table0%0/2
A
A= $redirect->{"http_headers"}
Aresult
11
00
297  
298 0 return [ $code, $headers, [] ];
299 }
300  
301 1 ▶sub _render_view {
Subroutine _render_view: called CC=5 SCAR=17.0
302 1 my ($self, $moniker, $context, $method) = @_;
303  
304 1 my $stash = $context->stash;
305  
306 1 ▶ return $self->_redirect($context) if exists $stash->{redirect};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash->{"redirect"} 0 1 1
307  
308 throw 'Model [_1] method [_2] stashed no view', [ $moniker, $method ]
309 1 ▶ unless $stash->{view};
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->{"view"} 0 1 1
310  
311 my $view = $self->views->{$stash->{view}}
312 or throw 'Model [_1] method [_2] unknown view [_3]',
313 1 ▶ [ $moniker, $method, $stash->{view} ];
Branch: 1 branch
BranchTrueFalseTotal
my $view = $self->views->{$stash->{"view"}} or &throw("Model [_1] method [_2] unknown view [_3]", [$moniker, $method, $stash->{"view"}]) 1 0 1
314 my $res = $view->serialize($context)
315 1 ▶ or throw 'View [_1] returned false', [ $stash->{view} ];
Branch: 1 branch
BranchTrueFalseTotal
my $res = $view->serialize($context) or &throw("View [_1] returned false", [$stash->{"view"}]) 1 0 1
316  
317 1 return $res;
318 }
319  
320 0 ▶sub _render_exception {
Subroutine _render_exception: not called CC=6 SCAR=37.4
321 0 my ($self, $moniker, $context, $e) = @_;
322  
323 0 ▶ $e = exception $e, { level => 2, rv => HTTP_INTERNAL_SERVER_ERROR }
Branch: 1 branch
BranchTrueFalseTotal
unless $e and builtin::blessed($e) and $e->can("rv") and $e->rv > 400 0 0 0
Condition0%0/9
$e and builtin::blessed($e) and $e->can("rv") and $e->rv > 400
!ll&&!rl&&r
000
$e and builtin::blessed($e) and $e->can("rv")
!ll&&!rl&&r
000
$e and builtin::blessed($e)
!ll&&!rl&&r
000
MC/DC0%0/4
$e and builtin::blessed($e) and $e->can("rv") and $e->rv > 400
$e builtin::blessed($e) $e->can("rv") $e->rv > 400
Truth table0%0/5
A and B and C and D
A= $eB= builtin::blessed($e)C= $e->can("rv")D= $e->rv > 400
ABCDresult
0XXX0
10XX0
110X0
11100
11111
324 unless $e && blessed $e && $e->can('rv') && $e->rv > HTTP_BAD_REQUEST;
325  
326 0 my $default = { should_log_errors => 1 };
327 0 my $attr = deref $self->config, $self->_config_attr, $default;
328  
329 0 ▶ if ($attr->{should_log_errors}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"should_log_errors"}) 0 0 0
330 0 my $msg = "${e}"; chomp $msg;
331  
332 0 $self->log->error($msg, $context);
333 }
334  
335 0 my $res;
336  
337 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
338 0 my $model = $self->models->{$moniker};
339 0 ▶ my $method = $model->can('error') ? 'error' : 'exception_handler';
Branch: 1 branch
BranchTrueFalseTotal
$model->can('error') ? : 0 0 0
340  
341 0 $model->$method($context, $e);
342 0 $res = $self->_render_view($moniker, $context, $method);
343 }
344 0 ▶ catch { $res = $self->_internal_server_error("${e}\n${_}") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
345  
346 0 return $res;
347 }
348  
349 2 ▶sub _render {
Subroutine _render: called CC=5 SCAR=16.8
350 2 my ($self, @args) = @_;
351  
352 2 ▶ return @args unless $self->_recognise_signature($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_recognise_signature($args[0]) 0 2 2
353  
354 2 my ($moniker, $method, undef, @request) = $self->_parse_sig($args[0]);
355  
356 2 my $opts = { domain => $moniker };
357 2 my ($req, $res);
358  
359 2 ▶ try { $req = $self->new_from_simple_request($opts, @request) }
Subroutine __ANON__: called CC=1 SCAR=0.0
360 0 ▶ catch { $res = $self->_internal_server_error($_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
361  
362 2 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 2 2
363  
364 2 my $context;
365  
366 2 ▶ try { $context = $self->_get_context($req, $moniker, $method) }
Subroutine __ANON__: called CC=1 SCAR=0.0
367 0 ▶ catch { $res = $self->_internal_server_error($_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
368  
369 2 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 2 2
370  
371 2 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=14.2
372 2 ▶ $method = $req->tunnel_method.$self->_action_suffix
Branch: 1 branch
BranchTrueFalseTotal
if $method eq $self->_tunnel_method 0 2 2
373 if $method eq $self->_tunnel_method;
374  
375 2 $res = $self->models->{$moniker}->execute($context, $method);
376  
377 2 ▶ $res = $self->_render_view($moniker, $context, $method)
Branch: 1 branch
BranchTrueFalseTotal
unless $res and &is_arrayref($res) 1 1 2
Condition66%2/3
$res and &is_arrayref($res)
!ll&&!rl&&r
101
MC/DC50%1/2
$res and &is_arrayref($res)
$res &is_arrayref($res)
Truth table66%2/3
A and B
A= $resB= &is_arrayref($res)
ABresult
0X0
100
111
378 unless $res && is_arrayref $res; # Plack response short circuits view
379 }
380 0 ▶ catch { $res = $self->_render_exception($moniker, $context, $_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
381  
382 2 ▶ $req->session->update if $req->can('session');
Branch: 1 branch
BranchTrueFalseTotal
if $req->can("session") 0 2 2
383  
384 2 return $res;
385 }
386  
387 2 ▶sub _filter () {
Subroutine _filter: called CC=1 SCAR=0.0
388 2 ▶ my $self = shift; return response_filter { $self->_render(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
389 }
390  
391 package
392 Web::Components::Loader::Context;
393  
394 1 ▶use List::Util qw( pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
395 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
396  
397 1 has 'action' => is => 'ro';
398  
399 1 has 'controllers' => is => 'ro';
400  
401 1 has 'models' => is => 'ro';
402  
403 1 has 'request' => is => 'ro', required => 1;
404  
405 1 has 'views' => is => 'ro';
406  
407 4 ▶has '_stash' => is => 'ro', default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
408  
409 0 ▶sub get_body_parameters {
Subroutine get_body_parameters: not called CC=1 SCAR=6.9
Pod undocumented
410 0 return {};
411 }
412  
413 17 ▶sub stash {
Subroutine stash: called CC=4 SCAR=13.9
Pod undocumented
414 17 my ($self, @args) = @_;
415  
416 17 ▶ return $self->_stash unless $args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] 10 7 17
417  
418 7 ▶ return $self->_stash->{$args[0]} unless $args[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $args[1] 2 5 7
419  
420 5 for my $pair (pairs @args) {
421 11 $self->_stash->{$pair->key} = $pair->value;
422 }
423  
424 5 return $self->_stash;
425 }
426  
427 1 ▶sub verify_form_post {
Subroutine verify_form_post: called CC=1 SCAR=0.0
Pod undocumented
428 1 return 'Not implemented';
429 }
430  
431 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
432  
433 1 1;
434  
435 __END__
↑ Summary blib/lib/Web/Components/ConfigLoader.pm »