WebApp/Segment/Template/Loader.pm

filter: Statementstmt 80.3% 45 / 56 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 78.3% 65 / 83 CC 6 SCAR 8.1
CC
6
Cov
74%
template_not_found
6.0
__ANON__
3.8
dispatch
2.0
CRAP 2.2
« WebApp/Segment/User.pm ↑ Summary WebApp/Session.pm »
1 =head1 NAME
2  
3 OpenFrame::WebApp::Segment::Template::Loader - a pipeline segment to load
4 templates.
5  
6 =head1 SYNOPSIS
7  
8 use Pipeline;
9 use OpenFrame::WebApp;
10  
11 my $pipe = new Pipeline;
12 # ... add segments that put a Template in the store ...
13 $pipe->add_segment(new OpenFrame::WebApp::Segment::Template::Loader);
14  
15 $pipe->dispatch; # will load any OpenFrame::WebApp::Template in the store.
16  
17 =cut
18  
19 package OpenFrame::WebApp::Segment::Template::Loader;
20  
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Error qw( :try );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use OpenFrame::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use OpenFrame::WebApp::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use OpenFrame::WebApp::Template::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 our $VERSION = (split(/ /, '$Revision: 1.1 $'))[1];
30  
31 1 ▶use base qw( OpenFrame::WebApp::Segment::Template );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33  
34 ## dispatch this segment
35 2 ▶sub dispatch {
Subroutine dispatch: called CC=2 SCAR=7.0
Pod documented
36 2 my $self = shift;
37 2 my $template = $self->get_template_from_store;
38 2 ▶ if ($template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template) 2 0 2
39 2 return $self->process_template( $template );
40 }
41 }
42  
43 ## process template given
44 2 ▶sub process_template {
Subroutine process_template: called CC=1 SCAR=0.2
Pod documented
45 2 my $self = shift;
46 2 my $template = shift;
47 2 my $response;
48  
49 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 2 $response = $template->process;
51 1 ▶ } catch OpenFrame::WebApp::Template::Error with {
Subroutine __ANON__: called CC=3 SCAR=13.3
52 1 my $e = shift;
53 1 ▶ if ($e->flag eq eTemplateNotFound) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->flag eq 'error.template.not.found') { } 0 1 1
elsif ($e->flag eq 'error.template.process') { } 1 0 1
54 0 $response = $self->template_not_found( $e->template );
55 } elsif ($e->flag eq eTemplateError) {
56 1 $response = $self->template_error( $e->message );
57 } else {
58 0 $response = $self->template_error( 'unknown error' );
59 }
60 2 };
61  
62 2 return $response;
63 }
64  
65 ## generate response if template not found
66 0 ▶sub template_not_found {
Subroutine template_not_found: not called CC=2 SCAR=17.9
Pod documented
67 0 my $self = shift;
68 0 my $file = shift;
69 0 my $request = $self->store->get('OpenFrame::Request');
70 0 my $response = new OpenFrame::Response;
71 0 ▶ my $uri = $request->uri if $request;
Branch: 1 branch
BranchTrueFalseTotal
if $request 0 0 0
72  
73 0 $response->code(ofERROR);
74 0 my $date = localtime(time());
75 0 $response->message(
76 qq{
77 <html>
78 <head>
79 <title>Template Not Found</title>
80 </head>
81 <body>
82 <h1>Template Not Found</h1>
83 <p>The template associated with $uri was not found.</p>
84 <p>Template file: $file</p>
85 <hr>
86 <i>$date, OpenFrame $OpenFrame::VERSION (WebApp $OpenFrame::WebApp::VERSION)</i>
87 </body>
88 </html>
89 }
90 );
91 0 return $response;
92 }
93  
94 ## generate response if template error
95 1 ▶sub template_error {
Subroutine template_error: called CC=2 SCAR=6.9
Pod documented
96 1 my $self = shift;
97 1 my $message = shift;
98 1 my $request = $self->store->get('OpenFrame::Request');
99 1 my $response = new OpenFrame::Response;
100 1 ▶ my $uri = $request->uri if $request;
Branch: 1 branch
BranchTrueFalseTotal
if $request 1 0 1
101  
102 1 $response->code(ofERROR);
103 1 my $date = localtime(time());
104 1 $response->message(
105 qq{
106 <html>
107 <head>
108 <title>Template Error</title>
109 </head>
110 <body>
111 <h1>Template Error</h1>
112 <p>There was an error in the template associated with $uri.</p>
113 <p>Error message:</p>
114 <xmp>$message</xmp>
115 <hr>
116 <i>$date, OpenFrame $OpenFrame::VERSION (WebApp $OpenFrame::WebApp::VERSION)</i>
117 </body>
118 </html>
119 }
120 );
121 1 return $response;
122 }
123  
124 1 1;
125  
126 __END__
« WebApp/Segment/User.pm ↑ Summary WebApp/Session.pm »