| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$class and $class->isa("Unexpected")
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")
| |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | elsif ($class) { $exception = exception $proto, $bindv // [], @args } | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$bindv // []
MC/DC0%0/1
$bindv // []
$bindv
Truth table0%0/2
A
A= $bindv
| |||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | else { $exception = exception $proto, $bindv // [], level => 2, @args } | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$bindv // []
MC/DC0%0/1
$bindv // []
$bindv
Truth table50%1/2
A
A= $bindv
| |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | $self->log->error($exception, $context) if $self->can('log'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | my $code = $exception->rv || HTTP_INTERNAL_SERVER_ERROR; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$exception->rv || 500
MC/DC0%0/1
$exception->rv || 500
$exception->rv
Truth table50%1/2
A
A= $exception->rv
| |||||||||||||||||||||||||||||||||
| 116 | 2 | my $nav = $context->stash($self->navigation_key); | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | $code = HTTP_OK if $nav && $nav->is_script_request; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$nav and $nav->is_script_request
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 156 | or throw UnknownMethod, [blessed $self, $method]; | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 5 | ▶ | $method = NUL unless $self->is_authorised($context, $coderef); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 5 | ▶ | $self->$method($context, @{$uri_args}) if $method; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 5 | ▶ | return $stash->{response} if $stash->{response}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | 3 | ▶ | $stash->{$nav_key}->finalise_script_request if exists $stash->{$nav_key}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | return if $stash->{finalised} || exists $stash->{redirect}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$stash->{"finalised"} or exists $stash->{"redirect"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$stash->{"code"} //= 200
MC/DC100%1/1
$stash->{"code"} //= 200
$stash->{"code"}
Truth table100%2/2
A
A= $stash->{"code"}
| |||||||||||||||||||||||||||||||||
| 211 | 3 | $stash->{finalised} = TRUE; | |||||||||||||||||||||||||||||||
| 212 | 3 | ▶ | $stash->{page} //= { %{$self->_template_wrappers} }; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$stash->{"page"} //= {%{$self->_template_wrappers;}}
MC/DC100%1/1
$stash->{"page"} //= {%{$self->_template_wrappers;}}
$stash->{"page"}
Truth table100%2/2
A
A= $stash->{"page"}
| |||||||||||||||||||||||||||||||||
| 213 | 3 | ▶ | $stash->{page}->{layout} //= $self->moniker . "/${method}"; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$stash->{"page"}{"layout"} //= $self->moniker . "/$method"
MC/DC100%1/1
$stash->{"page"}{"layout"} //= $self->moniker . "/$method"
$stash->{"page"}{"layout"}
Truth table100%2/2
A
A= $stash->{"page"}{"layout"}
| |||||||||||||||||||||||||||||||||
| 214 | 3 | ▶ | $stash->{view} //= $self->_default_view; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$stash->{"view"} //= $self->_default_view
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
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||