blib/lib/Yukki/Web.pm

filter: Statementstmt 41.5% 54 / 130 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.3% 16 / 30 Podpod 90.0% 9 / 10 totaltotal 39.1% 79 / 202 SCAR 43.6
CC
15
Cov
35%
__ANON__
56.0
__ANON__
30.0
_build_plugins
12.0
CRAP 77.9
« blib/lib/Yukki/Web/Controller/Attachment.pm ↑ Summary blib/lib/Yukki/Web/View/Admin/User.pm »
1 package Yukki::Web;
2 4 $Yukki::Web::VERSION = '0.991_002'; # TRIAL
3  
4 4 ▶$Yukki::Web::VERSION = '0.991002';use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 extends qw( Yukki );
9  
10 4 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Yukki::Error qw( http_throw http_exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Yukki::Types qw( PluginList YukkiWebSettings );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Yukki::Web::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Yukki::Web::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Yukki::Web::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use LWP::MediaTypes qw( add_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Plack::Session::Store::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # ABSTRACT: the Yukki web server
28  
29  
30 4 has '+settings' => (
31 isa => YukkiWebSettings,
32 coerce => 1,
33 );
34  
35  
36 4 has router => (
37 is => 'ro',
38 isa => class_type('Path::Router'),
39 required => 1,
40 lazy => 1,
41 builder => '_build_router',
42 );
43  
44 sub _build_router {
45 0 ▶ my $self = shift;
Subroutine _build_router: not called CC=1 SCAR=6.9
46 0 Yukki::Web::Router->new( app => $self );
47 }
48  
49  
50 4 has plugins => (
51 is => 'ro',
52 isa => PluginList,
53 required => 1,
54 lazy => 1,
55 builder => '_build_plugins',
56 );
57  
58 sub all_plugins {
59 0 ▶ my $self = shift;
Subroutine all_plugins: not called CC=1 SCAR=6.9
Pod documented
60 0 $self->plugins->@*;
61 }
62  
63 sub format_helper_plugins {
64 0 ▶ my $self = shift;
Subroutine format_helper_plugins: not called CC=1 SCAR=6.9
Pod documented
65 0 grep { $_->does('Yukki::Web::Plugin::Role::FormatHelper') }
66 $self->plugins->@*;
67 }
68  
69 sub formatter_plugins {
70 0 ▶ my $self = shift;
Subroutine formatter_plugins: not called CC=1 SCAR=6.9
Pod documented
71 0 grep { $_->does('Yukki::Web::Plugin::Role::Formatter') }
72 $self->plugins->@*;
73 }
74  
75 sub _build_plugins {
76 0 ▶ my $self = shift;
Subroutine _build_plugins: not called CC=3 SCAR=24.8
77  
78 0 my @plugins;
79 0 for my $plugin_settings (@{ $self->settings->plugins }) {
80 0 my $module = $plugin_settings->{module};
81  
82 0 my $class = $module;
83 0 ▶ $class = "Yukki::Web::Plugin::$class" unless $class =~ s/^\+//;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ s/^\+//u 0 0 0
84  
85 0 Class::Load::load_class($class);
86  
87 0 push @plugins, $class->new(%$plugin_settings, app => $self);
88 }
89  
90 0 return \@plugins;
91 }
92  
93  
94 sub BUILD {
95 0 ▶ my $self = shift;
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
96  
97 0 my $types = $self->settings->media_types;
98 0 ▶ while (my ($mime_type, $ext) = each %$types) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$types) 0 0 0
99 0 ▶ my @ext = ref $ext ? @$ext : ($ext);
Branch: 1 branch
BranchTrueFalseTotal
ref $ext ? : 0 0 0
100 0 add_type($mime_type, @ext);
101 }
102 };
103  
104  
105 sub component {
106 0 ▶ my ($self, $type, $name) = @_;
Subroutine component: not called CC=1 SCAR=6.9
Pod documented
107 0 my $class_name = join '::', 'Yukki::Web', $type, $name;
108 0 Class::Load::load_class($class_name);
109 0 return $class_name->new(app => $self);
110 }
111  
112  
113 sub controller {
114 0 ▶ my ($self, $name) = @_;
Subroutine controller: not called CC=1 SCAR=6.9
Pod documented
115 0 return $self->component(Controller => $name);
116 }
117  
118  
119 sub view {
120 0 ▶ my ($self, $name) = @_;
Subroutine view: not called CC=1 SCAR=6.9
Pod documented
121 0 return $self->component(View => $name);
122 }
123  
124  
125 sub dispatch {
126 0 ▶ my ($self, $env) = @_;
Subroutine dispatch: not called CC=1 SCAR=6.9
Pod documented
127  
128 0 my $ctx = Yukki::Web::Context->new(env => $env);
129  
130 0 $env->{'yukki.app'} = $self;
131 0 $env->{'yukki.settings'} = $self->settings;
132 0 $env->{'yukki.ctx'} = $ctx;
133 0 weaken $env->{'yukki.ctx'};
134  
135 0 my $response;
136  
137 try {
138 0 ▶ my $match = $self->router->match($ctx->request->path);
Subroutine __ANON__: not called CC=7 SCAR=40.3
139  
140 0 ▶ http_throw('No action found matching that URL.', {
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
141 status => 'NotFound',
142 }) unless $match;
143  
144 0 $ctx->request->path_parameters($match->mapping);
145  
146 0 my $access_level_needed = $match->access_level;
147 http_throw('You are not authorized to run this action.', {
148 status => 'Forbidden',
149 }) unless $self->check_access(
150 user => $ctx->session->{user},
151 repository => $match->mapping->{repository} // '-',
152 0 ▶ special => $match->mapping->{special} // '-',
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_access("user", $ctx->session->{"user"}, "repository", $match->mapping->{"repository"} // "-", "special", $match->mapping->{"special"} // "-", "needs", $access_level_needed) 0 0 0
Condition0%0/4
$match->mapping->{"repository"} // "-"
l!l
00
$match->mapping->{"special"} // "-"
l!l
00
MC/DC0%0/1
$match->mapping->{"repository"} // "-"
$match->mapping->{"repository"}
MC/DC0%0/1
$match->mapping->{"special"} // "-"
$match->mapping->{"special"}
Truth table0%0/2
A
A= $match->mapping->{"repository"}
Aresult
11
00
Truth table0%0/2
A
A= $match->mapping->{"special"}
Aresult
11
00
153 needs => $access_level_needed,
154 );
155  
156 0 ▶ if ($ctx->session->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->session->{'user'}) { } 0 0 0
157 $ctx->response->add_navigation_item(user => {
158 label => $ctx->session->{user}{name},
159 0 href => 'profile',
160 sort => 200,
161 });
162 0 $ctx->response->add_navigation_item(user => {
163 label => 'Sign out',
164 href => 'logout',
165 sort => 100,
166 });
167 }
168  
169 else {
170 0 $ctx->response->add_navigation_item(user => {
171 label => 'Sign in',
172 href => 'login',
173 sort => 100,
174 });
175 }
176  
177 0 for my $repository (keys %{ $self->settings->repositories }) {
178 0 my $config = $self->settings->repositories->{$repository};
179  
180 0 my $name = $config->name;
181 0 $ctx->response->add_navigation_item(repository => {
182 label => $name,
183 href => join('/', 'page/view', $repository),
184 sort => $config->sort,
185 });
186 }
187  
188 0 my $controller = $match->target;
189  
190 0 $controller->fire($ctx);
191 0 $response = $ctx->response->finalize;
192 }
193  
194 catch {
195  
196 0 ▶ if (blessed $_ and $_->isa('Yukki::Error')) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa('Yukki::Error')) { } 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("Yukki::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Yukki::Error")
builtin::blessed($_) $_->isa("Yukki::Error")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Yukki::Error")
ABresult
0X0
100
111
197  
198 0 ▶ if ($_->does('HTTP::Throwable::Role::Status::Forbidden')
Branch: 1 branch
BranchTrueFalseTotal
if ($_->does('HTTP::Throwable::Role::Status::Forbidden') and not $ctx->session->{'user'}) { } 0 0 0
Condition0%0/3
$_->does("HTTP::Throwable::Role::Status::Forbidden") and not $ctx->session->{"user"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->does("HTTP::Throwable::Role::Status::Forbidden") and not $ctx->session->{"user"}
$_->does("HTTP::Throwable::Role::Status::Forbidden") not $ctx->session->{"user"}
Truth table0%0/3
A and not(B)
A= $_->does("HTTP::Throwable::Role::Status::Forbidden")B= not $ctx->session->{"user"}
ABresult
0X0
100
111
199 and not $ctx->session->{user}) {
200  
201 0 $response = http_exception('Please login first.', {
202 status => 'Found',
203 location => ''.$ctx->rebase_url('login'),
204 })->as_psgi($env);
205 }
206  
207 else {
208 0 $response = $_->as_psgi($env);
209 }
210 }
211  
212 else {
213 0 warn "ISE: $_";
214  
215 0 $response = http_exception("Oh darn. Something went wrong.", {
216 status => 'InternalServerError',
217 show_stack_trace => 0,
218 })->as_psgi($env);
219 }
220 0 };
221  
222 0 return $response;
223 }
224  
225  
226 sub session_middleware {
227 0 ▶ my $self = shift;
Subroutine session_middleware: not called CC=1 SCAR=6.9
Pod documented
228  
229 # TODO Make this configurable
230 0 return ('Session',
231 store => Plack::Session::Store::Cache->new(
232 cache => CHI->new(driver => 'FastMmap'),
233 ),
234 );
235 }
236  
237  
238 sub munge_label {
239 0 ▶ my ($self, $link) = @_;
Subroutine munge_label: not called CC=1 SCAR=6.9
Pod documented
240  
241 0 $link =~ m{([^/]+)$};
242  
243 0 $link =~ s{([a-zA-Z])'([a-zA-Z])}{$1$2}g; # foo's -> foos, isn't -> isnt
244 0 $link =~ s{[^a-zA-Z0-9-_./]+}{-}g;
245 0 $link =~ s{-+}{-}g;
246 0 $link =~ s{^-}{};
247 0 $link =~ s{-$}{};
248  
249 0 $link .= '.yukki';
250  
251 0 return $link;
252 }
253  
254  
255 4 1;
256  
257 __END__
258  
259 =pod
260  
261 =encoding UTF-8
262  
263 =head1 NAME
264  
265 Yukki::Web - the Yukki web server
266  
267 =head1 VERSION
268  
269 version 0.991_002
270  
271 =head1 DESCRIPTION
272  
273 This class handles the work of dispatching incoming requests to the various
274 controllers.
275  
276 =head1 ATTRIBUTES
277  
278 =head2 router
279  
280 This is the L<Path::Router> that will determine where incoming requests are
281 sent. It is automatically set to a L<Yukki::Web::Router> instance.
282  
283 =head2 plugins
284  
285 my @plugins = $app->all_plugins;
286 my @format_helpers = $app->format_helper_plugins;
287 my @formatters = $app->format_plugins;
288  
289 This attribute stores all the loaded plugins.
290  
291 =head1 METHODS
292  
293 =head2 component
294  
295 Helper method used by L</controller> and L</view>.
296  
297 =head2 controller
298  
299 my $controller = $app->controller($name);
300  
301 Returns an instance of the named L<Yukki::Web::Controller>.
302  
303 =head2 view
304  
305 my $view = $app->view($name);
306  
307 Returns an instance of the named L<Yukki::Web::View>.
308  
309 =head2 dispatch
310  
311 my $response = $app->dispatch($env);
312  
313 This is a PSGI application in a method call. Given a L<PSGI> environment, maps
314 that to the appropriate controller and fires it. Whether successful or failure,
315 it returns a PSGI response.
316  
317 =head2 session_middleware
318  
319 enable $app->session_middleware;
320  
321 Returns the setup for the PSGI session middleware.
322  
323 =head2 munge_label
324  
325 my $link = $app->munch_label("This is a label");
326  
327 Turns some label into a link slug using the standard means for doing so.
328  
329 =head2 all_plugins
330  
331 A convenience accessor that returns C<plugins> as a list.
332  
333 =head2 format_helper_plugins
334  
335 Returns all the format helper plugins as a list.
336  
337 =head2 formatter_plugins
338  
339 Returns all the formatter plugins as a list.
340  
341 =for Pod::Coverage BUILD
342  
343 =head1 AUTHOR
344  
345 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
346  
347 =head1 COPYRIGHT AND LICENSE
348  
349 This software is copyright (c) 2017 by Qubling Software LLC.
350  
351 This is free software; you can redistribute it and/or modify it under
352 the same terms as the Perl 5 programming language system itself.
353  
354 =cut
« blib/lib/Yukki/Web/Controller/Attachment.pm ↑ Summary blib/lib/Yukki/Web/View/Admin/User.pm »