blib/lib/Yukki/Web.pm

filter: Statementstmt 39.3% 57 / 145 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 54.8% 17 / 31 Podpod 90.0% 9 / 10 totaltotal 36.4% 83 / 228 CC 20 SCAR 34.5
CC
20
Cov
31%
__ANON__
110.7
__ANON__
42.0
_build_plugins
12.0
CRAP 31.6
« blib/lib/Yukki/Web/Controller/Page.pm ↑ Summary blib/lib/Yukki/Web/Plugin/YukkiText.pm »
1 package Yukki::Web;
2 6 $Yukki::Web::VERSION = '0.991_007'; # TRIAL
3 6 $Yukki::Web::VERSION = '0.991007';
4 6 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 extends qw( Yukki );
9  
10 6 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Yukki::Error qw( http_throw http_exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Yukki::Types qw( PluginList YukkiWebSettings );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Yukki::Web::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Yukki::Web::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Yukki::Web::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use LWP::MediaTypes qw( add_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use Plack::Session::Store::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # ABSTRACT: the Yukki web server
28  
29  
30 6 has '+settings' => (
31 isa => YukkiWebSettings,
32 coerce => 1,
33 );
34  
35  
36 6 has router => (
37 is => 'ro',
38 isa => class_type('Path::Router'),
39 required => 1,
40 lazy => 1,
41 builder => '_build_router',
42 );
43  
44 0 ▶sub _build_router {
Subroutine _build_router: not called CC=1 SCAR=6.9
45 0 my $self = shift;
46 0 Yukki::Web::Router->new( app => $self );
47 }
48  
49  
50 6 has plugins => (
51 is => 'ro',
52 isa => PluginList,
53 required => 1,
54 lazy => 1,
55 builder => '_build_plugins',
56 );
57  
58 0 ▶sub all_plugins {
Subroutine all_plugins: not called CC=1 SCAR=6.9
Pod documented
59 0 my $self = shift;
60 0 $self->plugins->@*;
61 }
62  
63 0 ▶sub format_helper_plugins {
Subroutine format_helper_plugins: not called CC=1 SCAR=6.9
Pod documented
64 0 my $self = shift;
65 0 grep { $_->does('Yukki::Web::Plugin::Role::FormatHelper') }
66 $self->plugins->@*;
67 }
68  
69 0 ▶sub formatter_plugins {
Subroutine formatter_plugins: not called CC=1 SCAR=6.9
Pod documented
70 0 my $self = shift;
71 0 grep { $_->does('Yukki::Web::Plugin::Role::Formatter') }
72 $self->plugins->@*;
73 }
74  
75 0 ▶sub _build_plugins {
Subroutine _build_plugins: not called CC=3 SCAR=24.8
76 0 my $self = shift;
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 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my $self = shift;
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 0 ▶sub component {
Subroutine component: not called CC=1 SCAR=6.9
Pod documented
106 0 my ($self, $type, $name) = @_;
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 0 ▶sub controller {
Subroutine controller: not called CC=1 SCAR=6.9
Pod documented
114 0 my ($self, $name) = @_;
115 0 return $self->component(Controller => $name);
116 }
117  
118  
119 0 ▶sub view {
Subroutine view: not called CC=1 SCAR=6.9
Pod documented
120 0 my ($self, $name) = @_;
121 0 return $self->component(View => $name);
122 }
123  
124  
125 0 ▶sub dispatch {
Subroutine dispatch: not called CC=1 SCAR=6.4
Pod documented
126 0 my ($self, $env) = @_;
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 0 ▶ try {
Subroutine __ANON__: not called CC=11 SCAR=47.1
138 0 my $match = $self->router->match($ctx->request->path);
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 0 my $admin = 0;
158 0 my $user = $ctx->session->{user};
159 0 ▶ if ($self->check_access(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_access("user", $user, "special", "admin_user", "needs", "read")) 0 0 0
160 user => $user,
161 special => 'admin_user',
162 needs => 'read',
163 )) {
164 0 $admin++;
165 0 $ctx->response->add_navigation_item(admin => {
166 label => 'Users',
167 href => 'admin/user/list',
168 sort => 50,
169 });
170 }
171  
172 0 ▶ if ($self->check_access(
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_access("user", $user, "special", "admin_repository", "needs", "read")) 0 0 0
173 user => $user,
174 special => 'admin_repository',
175 needs => 'read',
176 )) {
177 0 $admin++;
178 0 $ctx->response->add_navigation_item(admin => {
179 label => 'Repositories',
180 href => 'admin/repository/list',
181 sort => 50,
182 });
183 }
184  
185 0 ▶ if ($admin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($admin) 0 0 0
186 0 $ctx->response->add_navigation_item(repository => {
187 6 ▶ label => "\N{TRIGRAM FOR HEAVEN}",
Subroutine BEGIN: called CC=1 SCAR=0.0
188 href => '/',
189 sort => 0,
190 class => 'admin-nav popup',
191 id => 'admin-popup',
192 });
193 }
194  
195 0 $ctx->response->add_navigation_item(user => {
196 label => $user->name,
197 href => 'profile',
198 sort => 200,
199 });
200 0 $ctx->response->add_navigation_item(user => {
201 label => 'Sign out',
202 href => 'logout',
203 sort => 100,
204 });
205 }
206  
207 else {
208 0 $ctx->response->add_navigation_item(user => {
209 label => 'Sign in',
210 href => 'login',
211 sort => 100,
212 });
213 }
214  
215 0 for my $repo ($self->model('Root')->list_repositories) {
216 0 ▶ next unless $repo->repository_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $repo->repository_exists 0 0 0
217 0 my $config = $repo->repository_settings;
218  
219 0 my $name = $config->name;
220 0 $ctx->response->add_navigation_item(repository => {
221 label => $name,
222 href => join('/', 'page/view', $repo->name),
223 sort => $config->sort,
224 });
225 }
226  
227 0 my $controller = $match->target;
228  
229 0 $controller->fire($ctx);
230 0 $response = $ctx->response->finalize;
231 }
232  
233 0 ▶ catch {
Subroutine __ANON__: not called CC=6 SCAR=37.4
234  
235 0 ▶ if (blessed $_ and $_->isa('Yukki::Error')) {
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
236  
237 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
238 and not $ctx->session->{user}) {
239  
240 0 $response = http_exception('Please login first.', {
241 status => 'Found',
242 location => ''.$ctx->rebase_url('login'),
243 })->as_psgi($env);
244 }
245  
246 else {
247 0 $response = $_->as_psgi($env);
248 }
249 }
250  
251 else {
252 0 warn "ISE: $_";
253  
254 0 my $message = "Oh darn. Something went wrong.";
255 0 ▶ $message = "ERROR: $_" if $ENV{YUKKI_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"YUKKI_DEBUG"} 0 0 0
256  
257 0 $response = http_exception($message, {
258 status => 'InternalServerError',
259 show_stack_trace => 0,
260 })->as_psgi($env);
261 }
262 0 };
263  
264 0 return $response;
265 }
266  
267  
268 0 ▶sub session_middleware {
Subroutine session_middleware: not called CC=1 SCAR=6.9
Pod documented
269 0 my $self = shift;
270  
271 # TODO Make this configurable
272 0 return ('Session',
273 store => Plack::Session::Store::Cache->new(
274 cache => CHI->new(driver => 'FastMmap'),
275 ),
276 );
277 }
278  
279  
280 0 ▶sub munge_label {
Subroutine munge_label: not called CC=1 SCAR=6.9
Pod documented
281 0 my ($self, $link) = @_;
282  
283 0 $link =~ m{([^/]+)$};
284  
285 0 $link =~ s{([a-zA-Z])'([a-zA-Z])}{$1$2}g; # foo's -> foos, isn't -> isnt
286 0 $link =~ s{[^a-zA-Z0-9-_./]+}{-}g;
287 0 $link =~ s{-+}{-}g;
288 0 $link =~ s{^-}{};
289 0 $link =~ s{-$}{};
290  
291 0 $link .= '.yukki';
292  
293 0 return $link;
294 }
295  
296  
297 6 1;
298  
299 __END__
« blib/lib/Yukki/Web/Controller/Page.pm ↑ Summary blib/lib/Yukki/Web/Plugin/YukkiText.pm »