Web/Context.pm

filter: Statementstmt 60.8% 84 / 138 Branchbran 37.9% 22 / 58 Conditioncond 14.8% 4 / 27 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 58.8% 10 / 17 Podpod 100% 13 / 13 totaltotal 49.2% 133 / 270 CC 44 SCAR 26.7
CC
44
Cov
49%
data_from_instance_method
56.0
section_content
20.0
resolve_data
14.6
CRAP 14.5
↑ Summary Web/Page/Handler.pm »
1 package EntityModel::Web::Context;
2 {
3 9 $EntityModel::Web::Context::VERSION = '0.004';
4 }
5 9 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 request => { type => 'EntityModel::Web::Request' },
7 response => { type => 'EntityModel::Web::Response' },
8 site => { type => 'EntityModel::Web::Site' },
9 page => { type => 'EntityModel::Web::Page' },
10 user => { type => 'EntityModel::Web::User' },
11 session => { type => 'EntityModel::Web::Session' },
12 data => { type => 'hash', subclass => 'data' },
13 template => { type => 'EntityModel::Template' },
14 9 };
15  
16 =head1 NAME
17  
18 EntityModel::Web::Context - handle context for a web request
19  
20 =head1 VERSION
21  
22 version 0.004
23  
24 =head1 SYNOPSIS
25  
26 my $web = EntityModel::Web->new;
27 my $req = EntityModel::Web::Request->new;
28 my $ctx = EntityModel::Web::Context->new(
29 request => $req
30 );
31 $ctx->find_page_and_data($web);
32 $ctx->resolve_data;
33 $ctx->process;
34 $ctx->save_session;
35 return $ctx->response;
36  
37 =head1 DESCRIPTION
38  
39 =cut
40  
41 9 ▶use EntityModel::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =head1 METHODS
44  
45 =cut
46  
47 9 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
48 9 my $class = shift;
49 9 my $self = $class->SUPER::new;
50 9 my %args = @_;
51 9 ▶ if(defined(my $req = delete $args{request})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $req = delete $args{"request"})) 9 0 9
52 9 $self->request($req);
53 }
54 9 ▶ if(defined(my $tmpl = delete $args{template})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $tmpl = delete $args{'template'})) { } 0 9 9
55 0 $self->template($tmpl);
56 } else {
57 9 $self->{template} = EntityModel::Template->new;
58 }
59 9 return $self;
60 }
61  
62 =head2 find_page_and_data
63  
64 Locate the page and populate any initial data from the path information.
65  
66 =cut
67  
68 9 ▶sub find_page_and_data {
Subroutine find_page_and_data: called CC=8 SCAR=21.1
Pod documented
69 9 my $self = shift;
70 9 my $web = shift;
71 9 my $host = $self->request->uri->host;
72 # my ($site) = grep { warn "have " . $_->host; $_->host eq $host } $web->site->list;
73 9 my ($site) = $web->site->list;
74 # grep(sub { $_[0] eq $host })->first;
75 9 logDebug("Check for site [%s]", $host);
76 9 ▶ return EntityModel::Error->new($self, "No site") unless $site;
Branch: 1 branch
BranchTrueFalseTotal
unless $site 0 9 9
77 9 $self->site($site);
78  
79 # If we have a regex match, return the remaining entries
80 9 my ($page, @data) = $site->page_from_uri($self->request->uri);
81 9 ▶ unless($page) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page) 0 9 9
82 0 logWarning("Failed to find page for URI [%s]", $self->request->uri);
83 0 logInfo("Page [%s]", $_->path) for $site->page->list;
84 }
85 9 ▶ return EntityModel::Error->new($self, "No page") unless $page;
Branch: 1 branch
BranchTrueFalseTotal
unless $page 0 9 9
86  
87 # Pick up the page entry first
88 9 $self->page($page);
89 9 logDebug("Page is [%s]", $page->name);
90  
91 # Get all page entries in order from first to last
92 9 my @pages = $page;
93 9 ▶ while($page->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->parent) 2 9 11
94 2 $page = $page->parent;
95 2 unshift @pages, $page;
96 }
97  
98 # Apply data for any entries that have regex captures
99 9 my %page_data;
100 9 foreach my $p (@pages) {
101 11 my %page_data = $p->extract_data(\@data);
102 11 $self->data->set($_, $page_data{$_}) for keys %page_data;
103 }
104  
105 9 return $self;
106 }
107  
108 =head2 data_missing
109  
110 Returns number of missing dependencies for the given L<EntityModel::Web::Page::Data> entry.
111  
112 =cut
113  
114 3 ▶sub data_missing {
Subroutine data_missing: called CC=6 SCAR=23.5
Pod documented
115 3 my ($self, $entry) = @_;
116 3 ▶ return 0 unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 3 3
117  
118 my @missing = grep {
119 0 ▶ defined($_->data) && !$self->data->exists($_->data)
Condition0%0/3
defined $_->data && !$self->data->exists($_->data)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_->data && !$self->data->exists($_->data)
defined $_->data !$self->data->exists($_->data)
Truth table0%0/3
A && not(B)
A= defined $_->dataB= !$self->data->exists($_->data)
ABresult
0X0
100
111
120 } $entry->parameter->list;
121 3 ▶ push @missing, $entry if $entry->data && !$self->data->exists($entry->data);
Branch: 1 branch
BranchTrueFalseTotal
if $entry->data and not $self->data->exists($entry->data) 0 3 3
Condition33%1/3
$entry->data and not $self->data->exists($entry->data)
!ll&&!rl&&r
300
MC/DC0%0/2
$entry->data and not $self->data->exists($entry->data)
$entry->data not $self->data->exists($entry->data)
Truth table33%1/3
A and not(B)
A= $entry->dataB= not $self->data->exists($entry->data)
ABresult
0X0
100
111
122  
123 0 ▶ logDebug("Data $entry requires " . join(', ', map { $_->value } @missing) . " items which are not ready yet") if @missing;
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 3 3
124 3 return scalar(@missing);
125 }
126  
127 =head2 resolve_data
128  
129 Process all data for this page, handling initial population and then going through each item in
130 turn, adding it back to the queue if the dependencies aren't ready.
131  
132 Chained method.
133  
134 =cut
135  
136 1 ▶sub resolve_data {
Subroutine resolve_data: called CC=8 SCAR=26.8
Pod documented
137 1 my $self = shift;
138 1 ▶ return EntityModel::Error->new($self, 'No page') unless $self->page;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->page 0 1 1
139  
140 # Get list of required items for this page
141 1 my @dataList = $self->page->data->list;
142  
143 # Iterate through them until we no longer have any entries to resolve (or all existing entries are
144 # failing).
145 1 my $failed = 0;
146 DATA:
147 1 ▶ while(@dataList) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dataList) 3 0 3
148 3 ▶ my $entry = shift(@dataList) or next DATA;
Branch: 1 branch
BranchTrueFalseTotal
my $entry = shift @dataList or next DATA 3 0 3
149  
150 3 ▶ logDebug("Resolve data for " . ($entry->key // 'undef'));
Condition50%1/2
$entry->key // "undef"
l!l
30
MC/DC0%0/1
$entry->key // "undef"
$entry->key
Truth table50%1/2
A
A= $entry->key
Aresult
11
00
151 3 ▶ if($self->resolve_data_item($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->resolve_data_item($entry)) { } 2 0 2
152 # Successful resolution means we should reset the failure counter
153 2 $failed = 0;
154 } else {
155 0 ++$failed;
156 0 push @dataList, $entry;
157 }
158  
159 # If all entries in the queue are failing, raise an error here
160 0 ▶ return EntityModel::Error->new($self, sub { "Could not resolve items [%s], population failed", join ',', map { $_->key // 'undef' } @dataList }) if $failed && $failed >= @dataList;
Subroutine __ANON__: not called CC=2 SCAR=12.2
Branch: 1 branch
BranchTrueFalseTotal
if $failed and $failed >= @dataList 0 2 2
Condition20%1/5
$failed and $failed >= @dataList
!ll&&!rl&&r
200
$_->key // "undef"
l!l
00
MC/DC0%0/2
$failed and $failed >= @dataList
$failed $failed >= @dataList
MC/DC0%0/1
$_->key // "undef"
$_->key
Truth table33%1/3
A and B
A= $failedB= $failed >= @dataList
ABresult
0X0
100
111
Truth table0%0/2
A
A= $_->key
Aresult
11
00
161 }
162  
163 0 return $self;
164 }
165  
166 =head2 find_data_value
167  
168 Retrieve data value for given L<EntityModel::Web::Page::Data> entry.
169  
170 =cut
171  
172 3 ▶sub find_data_value {
Subroutine find_data_value: called CC=6 SCAR=23.5
Pod documented
173 3 my $self = shift;
174 3 my $entry = shift;
175  
176 3 my $v;
177  
178 3 ▶ if(defined $entry->value) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $entry->value) { } 2 1 3
elsif ($entry->class) { } 1 0 1
elsif ($entry->instance) { } 0 0 0
elsif ($entry->data) { } 0 0 0
179 # Simple value, used for constants
180 2 $v = $entry->value;
181 } elsif($entry->class) {
182 # Class method
183 1 $v = $self->data_from_class_method($entry);
184 } elsif ($entry->instance) {
185 # Instance method
186 0 $v = $self->data_from_instance_method($entry);
187 } elsif ($entry->data) {
188 # Data value from somewhere else
189 0 $v = $self->data->get($entry->data);
190 } else {
191 # Default case - probably an error
192 0 logDebug(" * $_ => " . $entry->{$_}) foreach keys %$entry;
193 0 logError({ %$entry });
194 0 $v = EntityModel::Error->new($self, 'Unknown data type');
195 }
196 2 return $v;
197 }
198  
199 =head2 resolve_data_item
200  
201 Resolve a single data item if we can.
202  
203 Returns undef on failure, original entry on success.
204  
205 =cut
206  
207 3 ▶sub resolve_data_item {
Subroutine resolve_data_item: called CC=4 SCAR=14.8
Pod documented
208 3 my $self = shift;
209 3 my $entry = shift;
210 3 my $k = $entry->key;
211  
212 3 ▶ if($self->data_missing($entry)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->data_missing($entry)) 0 3 3
213 0 logDebug("Deferring " . $k);
214 0 return undef;
215 }
216  
217 3 my $v = $self->find_data_value($entry);
218 2 ▶ $self->data->{$k} = $v unless eval { $v->isa('EntityModel::Error') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $v->isa("EntityModel::Error") } } 2 0 2
219  
220 2 ▶ logDebug("Data [$k] is now " . ($self->data->{$k} // 'undef'));
Condition50%1/2
$self->data->{$k} // "undef"
l!l
20
MC/DC0%0/1
$self->data->{$k} // "undef"
$self->data->{$k}
Truth table50%1/2
A
A= $self->data->{$k}
Aresult
11
00
221 2 return $entry;
222 }
223  
224 =head2 args_for_data
225  
226 Generate list of arguments for a method call.
227  
228 =cut
229  
230 1 ▶sub args_for_data {
Subroutine args_for_data: called CC=1 SCAR=0.2
Pod documented
231 1 my $self = shift;
232 1 my $entry = shift;
233  
234 1 my @param;
235 0 ▶ $entry->parameter->each($self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 0 my ($self, $item) = @_;
237 0 push @param, $self->find_data_value($item);
238 1 }));
239 1 return @param;
240 }
241  
242 =head2 data_from_class_method
243  
244 Call class method to obtain new data value.
245  
246 =cut
247  
248 1 ▶sub data_from_class_method {
Subroutine data_from_class_method: called CC=3 SCAR=11.1
Pod documented
249 1 my $self = shift;
250 1 my $entry = shift;
251 1 my $class = $entry->class;
252 1 my $method = $entry->method;
253  
254 1 ▶ return EntityModel::Error->new($self, 'No class provided') unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 1 1
255 1 ▶ return EntityModel::Error->new($self, 'Invalid method %s for %s', $method, $class) unless $class->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($method) 0 1 1
256  
257 return try {
258 1 $class->$method($self->args_for_data($entry));
259 1 } catch {
260 1 EntityModel::Error->new($self, "Failed in %s->%s for %s: %s", $class, $method, $entry->key, $_);
261 };
262 }
263  
264 =head2 data_from_instance_method
265  
266 Instance method, in which case hopefully we already set this one up
267  
268 =cut
269  
270 0 ▶sub data_from_instance_method {
Subroutine data_from_instance_method: not called CC=7 SCAR=40.3
Pod documented
271 0 my $self = shift;
272 0 my $entry = shift;
273 0 logDebug("Look up [%s]", $entry->instance);
274  
275 0 my $obj = $self->data->get($entry->instance);
276 0 ▶ $obj ||= $self->site if $entry->instance eq 'site';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->instance eq "site" 0 0 0
Condition0%0/3
$obj ||= $self->site
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj ||= $self->site
$obj $self->site
Truth table0%0/3
A ||= B
A= $objB= $self->site
ABresult
1X1
011
000
277 0 ▶ $obj ||= $self->page if $entry->instance eq 'page';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->instance eq "page" 0 0 0
Condition0%0/3
$obj ||= $self->page
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj ||= $self->page
$obj $self->page
Truth table0%0/3
A ||= B
A= $objB= $self->page
ABresult
1X1
011
000
278 0 ▶ $obj ||= $self if $entry->instance eq 'context';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->instance eq "context" 0 0 0
Condition0%0/3
$obj ||= $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj ||= $self
$obj $self
Truth table0%0/3
A ||= B
A= $objB= $self
ABresult
1X1
011
000
279 0 logDebug("Got [%s]", $obj);
280 0 my $method = $entry->method;
281 my $v = try {
282 0 logDebug("Call $method");
283 0 my @args = $self->args_for_data($entry);
284 0 $obj->$method(@args);
285 0 } catch {
286 0 logError("Method [%s] not valid for class %s on key %s, error %s", $method, $obj, $entry->key, $_);
287 };
288 0 logDebug("Had [%s]", $v);
289 0 return $v;
290 }
291  
292 =head2 process
293  
294 =cut
295  
296 0 ▶sub process {
Subroutine process: not called CC=3 SCAR=24.8
Pod documented
297 0 my $self = shift;
298 0 logDebug("Try to handle request for this page");
299  
300 0 $self->page->handle_request(request => $self->request, data => $self->data);
301  
302 my %section = map {
303 0 $_->section => $self->section_content($_->section)
304 } $self->site->layout->list;
305 0 ▶ my $tmpl = $self->page->template // $self->site->template;
Condition0%0/3
$self->page->template // $self->site->template
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->page->template // $self->site->template
$self->page->template $self->site->template
Truth table0%0/3
A // B
A= $self->page->templateB= $self->site->template
ABresult
1X1
011
000
306 0 ▶ return '' unless $tmpl;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpl 0 0 0
307  
308 0 return $self->template->as_text($tmpl, {
309 context => $self,
310 page => $self->page,
311 data => $self->data,
312 section => \%section
313 });
314 }
315  
316  
317 =head2 section_content
318  
319 =cut
320  
321 0 ▶sub section_content {
Subroutine section_content: not called CC=4 SCAR=30.0
Pod documented
322 0 my $self = shift;
323 0 ▶ my $section = shift or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $section = shift() or return "" 0 0 0
324 0 ▶ return EntityModel::Error->new($self, 'No page defined') unless $self->page;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->page 0 0 0
325  
326 0 logDebug("Try section [%s]", $section);
327 0 my $content = $self->page->content_by_section->get($section);
328 0 logDebug("Had content [%s]", $content);
329 0 ▶ return EntityModel::Error->new($self, "Section [$section] not found") unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
330 0 logDebug("Template [%s]", $content->template);
331 0 return $self->template->as_text($content->template, {
332 context => $self,
333 page => $self->page,
334 data => $self->data,
335 });
336 }
337  
338 =head2 load_session
339  
340 Loads session data into the current context.
341  
342 =cut
343  
344 0 ▶sub load_session {
Subroutine load_session: not called CC=1 SCAR=6.9
Pod documented
345 0 my $self = shift;
346 }
347  
348 =head2 save_session
349  
350 =cut
351  
352 0 ▶sub save_session {
Subroutine save_session: not called CC=1 SCAR=6.9
Pod documented
353 0 my $self = shift;
354 }
355  
356 9 1;
357  
358 __END__
↑ Summary Web/Page/Handler.pm »