lib/Web/Components/Model.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 53.1% 17 / 32 Conditioncond 58.3% 14 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 76.2% 135 / 177 CC 28 SCAR 20.1
CC
28
Cov
78%
error
14.8
execute
9.8
_finalise_stash
6.0
CRAP 7.5
« blib/lib/Web/Components/Navigation.pm ↑ Summary lib/Web/Components/Util.pm »
1 package Web::Components::Model;
2  
3 1 ▶use Web::ComposableRequest::Constants qw( EXCEPTION_CLASS FALSE NUL TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use HTTP::Status qw( HTTP_OK HTTP_INTERNAL_SERVER_ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Unexpected::Types qw( HashRef LoadableClass Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Unexpected::Functions qw( BadCSRFToken UnknownMethod );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Web::Components::Util qw( exception throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =pod
13  
14 =encoding utf-8
15  
16 =head1 Name
17  
18 Web::Components::Model - Base class for Web Component models
19  
20 =head1 Synopsis
21  
22 use Moo;
23  
24 extends 'Web::Components::Model';
25  
26 =head1 Description
27  
28 Base class for Web Component models
29  
30 =head1 Configuration and Environment
31  
32 Defines the following attributes;
33  
34 =over 3
35  
36 =item C<context_class>
37  
38 A required loadable class. The classname of the object created by
39 C<get_context>
40  
41 =cut
42  
43 1 has 'context_class' =>
44 is => 'lazy',
45 isa => LoadableClass,
46 coerce => TRUE,
47 default => 'Web::Components::Loader::Context';
48  
49 =item C<navigation_key>
50  
51 An immutable string which defaults to C<nav>. Key used to stash the
52 L<Web::Components::Navigation|navigation object>
53  
54 =cut
55  
56 1 has 'navigation_key' => is => 'ro', isa => Str, default => 'nav';
57  
58 has '_default_view' =>
59 is => 'lazy',
60 isa => Str,
61 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
62 1 my $self = shift;
63  
64 1 ▶ return $self->config->can('default_view')
Branch: 1 branch
BranchTrueFalseTotal
$self->config->can('default_view') ? : 0 1 1
65 ? $self->config->default_view : 'HTML';
66 1 };
67  
68 has '_exception_layout' =>
69 is => 'lazy',
70 isa => Str,
71 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
72 1 my $self = shift;
73  
74 1 ▶ return $self->config->can('exception_layout')
Branch: 1 branch
BranchTrueFalseTotal
$self->config->can('exception_layout') ? : 0 1 1
75 ? $self->config->exception_layout : 'page/exception';
76 1 };
77  
78 has '_template_wrappers' =>
79 is => 'lazy',
80 isa => HashRef,
81 1 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
82 1 my $self = shift;
83  
84 1 ▶ return $self->config->can('template_wrappers')
Branch: 1 branch
BranchTrueFalseTotal
$self->config->can('template_wrappers') ? : 0 1 1
85 ? $self->config->template_wrappers : {};
86 1 };
87  
88 =back
89  
90 =head1 Subroutines/Methods
91  
92 Defines the following methods;
93  
94 =over 3
95  
96 =item C<error>
97  
98 Stash exception handler output to print an exception page. Also called by
99 component loader if model dies
100  
101 =cut
102  
103 2 ▶sub error {
Subroutine error: called CC=11 SCAR=27.0
Pod documented
104 2 my ($self, $context, $proto, $bindv, @args) = @_;
105  
106 2 my $exception;
107 2 my $class = blessed $proto;
108  
109 2 ▶ if ($class and $class->isa('Unexpected')) { $exception = $proto }
Branch: 2 branches
BranchTrueFalseTotal
if ($class and $class->isa('Unexpected')) { } 1 1 2
elsif ($class) { } 0 1 1
Condition66%2/3
$class and $class->isa("Unexpected")
!ll&&!rl&&r
101
MC/DC50%1/2
$class and $class->isa("Unexpected")
$class $class->isa("Unexpected")
Truth table66%2/3
A and B
A= $classB= $class->isa("Unexpected")
ABresult
0X0
100
111
110 0 ▶ elsif ($class) { $exception = exception $proto, $bindv // [], @args }
Condition0%0/2
$bindv // []
l!l
00
MC/DC0%0/1
$bindv // []
$bindv
Truth table0%0/2
A
A= $bindv
Aresult
11
00
111 1 ▶ else { $exception = exception $proto, $bindv // [], level => 2, @args }
Condition50%1/2
$bindv // []
l!l
01
MC/DC0%0/1
$bindv // []
$bindv
Truth table50%1/2
A
A= $bindv
Aresult
11
00
112  
113 2 ▶ $self->log->error($exception, $context) if $self->can('log');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("log") 2 0 2
114  
115 2 ▶ my $code = $exception->rv || HTTP_INTERNAL_SERVER_ERROR;
Condition50%1/2
$exception->rv || 500
l!l
20
MC/DC0%0/1
$exception->rv || 500
$exception->rv
Truth table50%1/2
A
A= $exception->rv
Aresult
11
00
116 2 my $nav = $context->stash($self->navigation_key);
117  
118 2 ▶ $code = HTTP_OK if $nav && $nav->is_script_request;
Branch: 1 branch
BranchTrueFalseTotal
if $nav and $nav->is_script_request 0 2 2
Condition33%1/3
$nav and $nav->is_script_request
!ll&&!rl&&r
200
MC/DC0%0/2
$nav and $nav->is_script_request
$nav $nav->is_script_request
Truth table33%1/3
A and B
A= $navB= $nav->is_script_request
ABresult
0X0
100
111
119  
120 $context->stash(
121 code => $code,
122 exception => $exception,
123 page => {
124 2 %{$self->_template_wrappers},
125 layout => $self->_exception_layout
126 },
127 view => $self->_default_view,
128 );
129  
130 2 $self->_finalise_stash($context);
131  
132 2 ▶ $nav->finalise_script_request if $nav;
Branch: 1 branch
BranchTrueFalseTotal
if $nav 0 2 2
133  
134 2 return;
135 }
136  
137 =item C<execute>
138  
139 Called by component loader for all model method calls
140  
141 =cut
142  
143 3 ▶sub execute {
Subroutine execute: called CC=9 SCAR=22.8
Pod documented
144 3 my ($self, $context, $methods) = @_;
145  
146 3 my $stash = $context->stash;
147 3 my $nav_key = $self->navigation_key;
148 3 my $options = { optional => TRUE, scrubber => FALSE };
149 3 my $uri_args = $context->request->uri_params->($options);
150 3 my $last_method;
151  
152 3 $stash->{method_chain} = $methods;
153  
154 3 for my $method (split m{ / }mx, $methods) {
155 5 ▶ my $coderef = $self->can($method)
Branch: 1 branch
BranchTrueFalseTotal
my $coderef = $self->can($method) or &throw(UnknownMethod(), [builtin::blessed($self), $method]) 5 0 5
156 or throw UnknownMethod, [blessed $self, $method];
157  
158 5 ▶ $method = NUL unless $self->is_authorised($context, $coderef);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_authorised($context, $coderef) 0 5 5
159  
160 5 ▶ $self->$method($context, @{$uri_args}) if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 5 0 5
161  
162 5 ▶ return $stash->{response} if $stash->{response};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"response"} 2 3 5
163  
164 3 ▶ $stash->{$nav_key}->finalise_script_request if exists $stash->{$nav_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash->{$nav_key} 0 3 3
165  
166 3 ▶ return if $stash->{finalised} || exists $stash->{redirect};
Branch: 1 branch
BranchTrueFalseTotal
if $stash->{"finalised"} or exists $stash->{"redirect"} 0 3 3
Condition33%1/3
$stash->{"finalised"} or exists $stash->{"redirect"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$stash->{"finalised"} or exists $stash->{"redirect"}
$stash->{"finalised"} exists $stash->{"redirect"}
Truth table33%1/3
A or B
A= $stash->{"finalised"}B= exists $stash->{"redirect"}
ABresult
1X1
011
000
167  
168 3 $last_method = $method;
169 }
170  
171 1 $self->_finalise_stash($context, $last_method);
172 1 return;
173 }
174  
175 =item C<get_context>
176  
177 Creates and returns a new context object from the request
178  
179 =cut
180  
181 4 ▶sub get_context {
Subroutine get_context: called CC=1 SCAR=0.0
Pod documented
182 4 my ($self, $args) = @_;
183  
184 4 return $self->context_class->new({ %{$args}, config => $self->config });
185 }
186  
187 =item C<verify_form_post>
188  
189 Stash an exception if the CSRF token is bad
190  
191 =cut
192  
193 0 ▶sub verify_form_post {
Subroutine verify_form_post: not called CC=2 SCAR=17.9
Pod documented
194 0 my ($self, $context) = @_;
195  
196 0 my $reason = $context->verify_form_post;
197  
198 0 ▶ return TRUE unless $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless $reason 0 0 0
199  
200 0 $self->error($context, BadCSRFToken, [$reason], level => 3);
201 0 return FALSE;
202 }
203  
204 # Private methods
205 3 ▶sub _finalise_stash { # Add necessary defaults for the view to render
Subroutine _finalise_stash: called CC=6 SCAR=18.0
206 3 my ($self, $context, $method) = @_;
207  
208 3 my $stash = $context->stash;
209  
210 3 ▶ $stash->{code} //= HTTP_OK unless exists $stash->{redirect};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $stash->{"redirect"} 3 0 3
Condition100%2/2
$stash->{"code"} //= 200
l!l
21
MC/DC100%1/1
$stash->{"code"} //= 200
$stash->{"code"}
Truth table100%2/2
A
A= $stash->{"code"}
Aresult
11
00
211 3 $stash->{finalised} = TRUE;
212 3 ▶ $stash->{page} //= { %{$self->_template_wrappers} };
Condition100%2/2
$stash->{"page"} //= {%{$self->_template_wrappers;}}
l!l
21
MC/DC100%1/1
$stash->{"page"} //= {%{$self->_template_wrappers;}}
$stash->{"page"}
Truth table100%2/2
A
A= $stash->{"page"}
Aresult
11
00
213 3 ▶ $stash->{page}->{layout} //= $self->moniker . "/${method}";
Condition100%2/2
$stash->{"page"}{"layout"} //= $self->moniker . "/$method"
l!l
21
MC/DC100%1/1
$stash->{"page"}{"layout"} //= $self->moniker . "/$method"
$stash->{"page"}{"layout"}
Truth table100%2/2
A
A= $stash->{"page"}{"layout"}
Aresult
11
00
214 3 ▶ $stash->{view} //= $self->_default_view;
Condition66%2/3
$stash->{"view"} //= $self->_default_view
l!l&&r!l&&!r
210
MC/DC0%0/2
$stash->{"view"} //= $self->_default_view
$stash->{"view"} $self->_default_view
Truth table66%2/3
A //= B
A= $stash->{"view"}B= $self->_default_view
ABresult
1X1
011
000
215 3 return;
216 }
217  
218 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 1 1;
221  
222 __END__
« blib/lib/Web/Components/Navigation.pm ↑ Summary lib/Web/Components/Util.pm »