blib/lib/Mojolicious/Plugin/Branding.pm

filter: Statementstmt 100% 180 / 180 Branchbran 94.1% 64 / 68 Conditioncond 81.6% 40 / 49 MC/DCmcdc 60.0% 18 / 30 Subroutinesub 100% 28 / 28 Podpod 100% 1 / 1 totaltotal 92.9% 331 / 356 CC 62 SCAR 20.4
CC
62
Cov
96%
_register_individual
18.0
_normalize_brand_spec
11.0
_item_for
8.0
CRAP 7.7
↑ Summary
1 package Mojolicious::Plugin::Branding;
2 8 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use Mojo::File qw< path >;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 { our $VERSION = '0.004' }
8  
9 package Mojolicious::Plugin::Branding::IndividualBaseRole;
10 8 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 requires qw< paths _create_from >;
13 8 has '_brand_name';
14 166 ▶has _spec_for => sub { return +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 166 ▶has _cache_for => sub { return +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 8 has '_stash_key';
17  
18 192 ▶sub _add_brand {
Subroutine _add_brand: called CC=4 SCAR=14.0
19 192 my ($self, $name, $spec) = @_;
20 192 ▶ croak "invalid specification for brand '$name'"
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec and ref $spec ne "HASH" 0 192 192
Condition66%2/3
defined $spec and ref $spec ne "HASH"
!ll&&!rl&&r
81840
MC/DC0%0/2
defined $spec and ref $spec ne "HASH"
defined $spec ref $spec ne "HASH"
Truth table66%2/3
A and B
A= defined $specB= ref $spec ne "HASH"
ABresult
0X0
100
111
21 if defined($spec) && ref($spec) ne 'HASH';
22 192 my $s4 = $self->_spec_for;
23 192 delete($s4->{$name});
24 192 ▶ $self->_spec_for->{$name} = $spec if defined($spec);
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec 184 8 192
25 192 delete($self->_cache_for->{$name});
26 192 return $self;
27 }
28  
29 176 ▶sub _add_brands {
Subroutine _add_brands: called CC=3 SCAR=11.1
30 176 my $self = shift;
31 176 ▶ my %brands = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 176 0 176
32 176 $self->_add_brand($_, $brands{$_}) for keys(%brands);
33 176 return $self;
34 }
35  
36 502 ▶sub _item_for {
Subroutine _item_for: called CC=8 SCAR=20.8
37 502 my ($self, $c) = @_;
38  
39 # do not allow sub-items inside sub-item
40 502 ▶ return if defined($self->_brand_name);
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_brand_name 157 345 502
41  
42 # cope with missing/non-existing brands
43 345 ▶ my $brand = $c->stash->{$self->_stash_key} // return;
Condition100%2/2
$c->stash->{$self->_stash_key} // (return)
l!l
27471
MC/DC100%1/1
$c->stash->{$self->_stash_key} // (return)
$c->stash->{$self->_stash_key}
Truth table100%2/2
A
A= $c->stash->{$self->_stash_key}
Aresult
11
00
44 274 ▶ my $spec = $self->_spec_for->{$brand} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $self->_spec_for->{$brand} or return 180 94 274
45  
46 180 ▶ return unless @{$spec->{paths}} || $spec->{ignore_app_paths};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$spec->{"paths"};} or $spec->{"ignore_app_paths"} 23 157 180
Condition66%2/3
@{$spec->{"paths"};} or $spec->{"ignore_app_paths"}
l!l&&r!l&&!r
157023
MC/DC50%1/2
@{$spec->{"paths"};} or $spec->{"ignore_app_paths"}
@{$spec->{"paths"};} $spec->{"ignore_app_paths"}
Truth table66%2/3
A or B
A= @{$spec->{"paths"};}B= $spec->{"ignore_app_paths"}
ABresult
1X1
011
000
47 157 ▶ return $self->_cache_for->{$brand} //= do {
Condition66%2/3
$self->_cache_for->{$brand} //= do { my(@paths) = @{$spec->{"paths"};}; push @paths, @{$self->paths;} unless $spec->{"ignore_app_paths"}; $self->_create_from($brand, \@paths) }
l!l&&r!l&&!r
561010
MC/DC0%0/2
$self->_cache_for->{$brand} //= do { my(@paths) = @{$spec->{"paths"};}; push @paths, @{$self->paths;} unless $spec->{"ignore_app_paths"}; $self->_create_from($brand, \@paths) }
$self->_cache_for->{$brand} do { my(@paths) = @{$spec->{"paths"};}; push @paths, @{$self->paths;} unless $spec->{"ignore_app_paths"}; $self->_create_from($brand, \@paths) }
Truth table66%2/3
A //= B
A= $self->_cache_for->{$brand}B= do { my(@paths) = @{$spec->{"paths"};}; push @paths, @{$self->paths;} unless $spec->{"ignore_app_paths"}; $self->_create_from($brand, \@paths) }
ABresult
1X1
011
000
48 101 my @paths = @{$spec->{paths}};
49 101 ▶ push @paths, @{$self->paths} unless $spec->{ignore_app_paths};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"ignore_app_paths"} 98 3 101
50 101 $self->_create_from($brand, \@paths);
51 };
52 }
53  
54 package Mojolicious::Plugin::Branding::IndividualStaticRole;
55 8 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 8 with 'Mojolicious::Plugin::Branding::IndividualBaseRole';
57  
58 181 ▶around serve => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 181 my ($orig, $self, $c, $path) = @_;
60 181 my $static = $self->_item_for($c);
61 181 ▶ return $static->serve($c, $path) if defined($static);
Branch: 1 branch
BranchTrueFalseTotal
if defined $static 61 120 181
62 120 return $self->$orig($c, $path);
63 8 };
64  
65 42 ▶sub _create_from {
Subroutine _create_from: called CC=1 SCAR=0.0
66 42 my ($self, $brand, $paths) = @_;
67  
68 return ref($self)->new(
69 (
70 42 map { $_ => $self->$_ } qw<
71 asset_dir
72 classes
73 extra
74 prefix
75 >
76 ),
77 paths => $paths,
78 _brand_name => $brand,
79 );
80 }
81  
82 package Mojolicious::Plugin::Branding::IndividualRendererRole;
83 8 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 8 ▶use Mojo::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 8 with 'Mojolicious::Plugin::Branding::IndividualBaseRole';
86  
87 321 ▶around render => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
88 321 my ($orig, $self, $c) = @_;
89 321 my $renderer = $self->_item_for($c);
90 321 ▶ return $renderer->render($c) if defined($renderer);
Branch: 1 branch
BranchTrueFalseTotal
if defined $renderer 96 225 321
91 225 return $self->$orig($c);
92 8 };
93  
94 59 ▶sub _create_from {
Subroutine _create_from: called CC=1 SCAR=0.0
95 59 my ($self, $brand, $paths) = @_;
96  
97 return ref($self)->new(
98 (
99 59 map { $_ => $self->$_ } qw<
100 handlers
101 helpers
102 classes
103 default_format
104 default_handler
105 encoding
106 >
107 ),
108 paths => $paths,
109 _brand_name => $brand,
110  
111 # get a separate cache to avoid different brands to tread on
112 # one another
113 cache => ref($self->cache)->new(max_keys => $self->cache->max_keys),
114 );
115 }
116  
117 package Mojolicious::Plugin::Branding;
118  
119 716 ▶sub __values_from_constrained_hash {
Subroutine __values_from_constrained_hash: called CC=4 SCAR=13.9
120 716 my $href = shift;
121 716 my $msg = shift;
122 716 my %hash = %$href;
123 716 my @retval;
124 716 for my $item (@_) {
125 1574 my ($key, $default) = @$item;
126 1574 ▶ push @retval, delete($hash{$key}) // $default;
Condition100%3/3
delete $hash{$key} // $default
l!l&&r!l&&!r
449872253
MC/DC100%2/2
delete $hash{$key} // $default
delete $hash{$key} $default
Truth table100%3/3
A // B
A= delete $hash{$key}B= $default
ABresult
1X1
011
000
127 }
128 716 ▶ croak "$msg: @{[ sort keys %hash ]}" if %hash;
Branch: 1 branch
BranchTrueFalseTotal
if %hash 10 706 716
129 706 return @retval;
130 }
131  
132 79 ▶sub __isa {
Subroutine __isa: called CC=2 SCAR=7.1
133 79 my ($target, $class) = @_;
134 79 ▶ return blessed($target) && $target->isa($class);
Condition66%2/3
builtin::blessed($target) && $target->isa($class)
!ll&&!rl&&r
65014
MC/DC50%1/2
builtin::blessed($target) && $target->isa($class)
builtin::blessed($target) $target->isa($class)
Truth table66%2/3
A && B
A= builtin::blessed($target)B= $target->isa($class)
ABresult
0X0
100
111
135 }
136  
137 366 ▶sub _resolve_dir {
Subroutine _resolve_dir: called CC=8 SCAR=20.8
138 366 my ($self, $home, $path, $msg) = @_;
139 366 ▶ return unless defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 8 358 366
140 358 ▶ croak "invalid empty path ($msg)" unless length($path);
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 10 348 358
141 348 ▶ croak "invalid path, either Mojo::File or plain string accepted ($msg)"
Branch: 1 branch
BranchTrueFalseTotal
if ref $path and not __isa($path, "Mojo::File") 7 341 348
Condition100%3/3
ref $path and not __isa($path, "Mojo::File")
!ll&&!rl&&r
33297
MC/DC100%2/2
ref $path and not __isa($path, "Mojo::File")
ref $path not __isa($path, "Mojo::File")
Truth table100%3/3
A and not(B)
A= ref $pathB= not __isa($path, "Mojo::File")
ABresult
0X0
100
111
142 if ref($path) && ! __isa($path, 'Mojo::File');
143 341 my $candidate = path($path);
144 341 ▶ $candidate = $home->child($path) unless $candidate->is_abs;
Branch: 1 branch
BranchTrueFalseTotal
unless $candidate->is_abs 334 7 341
145 341 ▶ return $candidate if -r $candidate && -d _;
Branch: 1 branch
BranchTrueFalseTotal
if -r $candidate and -d _ 305 36 341
Condition66%2/3
-r $candidate and -d _
!ll&&!rl&&r
360305
MC/DC50%1/2
-r $candidate and -d _
-r $candidate -d _
Truth table66%2/3
A and B
A= -r $candidateB= -d _
ABresult
0X0
100
111
146 36 return;
147 }
148  
149 274 ▶sub _normalize_sub_spec {
Subroutine _normalize_sub_spec: called CC=4 SCAR=13.9
150 274 my ($self, $title, $key, $src, $home) = @_;
151 274 ▶ $src //= {};
Condition50%1/2
$src //= {}
l!l
2740
MC/DC0%0/1
$src //= {}
$src
Truth table50%1/2
A
A= $src
Aresult
11
00
152 274 ▶ $src = { paths => $src } if ref($src) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $src ne "HASH" 13 261 274
153 274 my ($paths, $ignore) = __values_from_constrained_hash($src,
154 "unknown keys in $key for $title",
155 [ paths => []], [ignore_app_paths => 0]);
156 272 ▶ $paths = [ $paths ] if ref($paths) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $paths ne "ARRAY" 5 267 272
157 return {
158 ignore_app_paths => !!$ignore,
159 272 paths => [ map { $self->_resolve_dir($home, $_, "$title, $key") } @$paths ],
160 };
161 }
162  
163 267 ▶sub _normalize_brand_spec {
Subroutine _normalize_brand_spec: called CC=11 SCAR=24.0
164 267 my ($self, $title, $spec, $home) = @_;
165 267 ▶ return undef unless defined($spec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 123 144 267
166 144 ▶ $spec = [ $spec ]
Branch: 1 branch
BranchTrueFalseTotal
if not ref $spec or __isa($spec, "Mojo::File") 86 58 144
Condition100%3/3
not ref $spec or __isa($spec, "Mojo::File")
l!l&&r!l&&!r
81558
MC/DC100%2/2
not ref $spec or __isa($spec, "Mojo::File")
not ref $spec __isa($spec, "Mojo::File")
Truth table100%3/3
not(A) or B
A= not ref $specB= __isa($spec, "Mojo::File")
ABresult
1X1
011
000
167 if (! ref($spec)) || __isa($spec, 'Mojo::File');
168 144 ▶ $spec = { paths => $spec } if ref($spec) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec eq "ARRAY" 111 33 144
169  
170 # here we demand that we have a hash reference
171 144 ▶ croak "invalid specification for $title" if ref($spec) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec ne "HASH" 2 142 144
172  
173 142 my ($common, $templates, $public) = __values_from_constrained_hash($spec,
174 "unknown keys in $title",
175 [paths => []], [templates => {}], [public => {}]);
176 139 $templates = $self->_normalize_sub_spec($title, templates => $templates, $home);
177 135 $public = $self->_normalize_sub_spec($title, public => $public, $home);
178  
179 133 my %spec = (
180 templates => $templates,
181 public => $public,
182 );
183  
184 133 ▶ $common = [ $common ] if ref($common) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $common ne "ARRAY" 11 122 133
185 133 for my $path ($common->@*) {
186 133 ▶ my $upper = $self->_resolve_dir($home, $path, $title) // next;
Condition100%2/2
$self->_resolve_dir($home, $path, $title) // (next)
l!l
10713
MC/DC100%1/1
$self->_resolve_dir($home, $path, $title) // (next)
$self->_resolve_dir($home, $path, $title)
Truth table100%2/2
A
A= $self->_resolve_dir($home, $path, $title)
Aresult
11
00
187 107 for my $key (qw< templates public >) {
188 214 ▶ my $child = $self->_resolve_dir($upper, $key, "$title, $key") // next;
Condition100%2/2
$self->_resolve_dir($upper, $key, "$title, $key") // (next)
l!l
18727
MC/DC100%1/1
$self->_resolve_dir($upper, $key, "$title, $key") // (next)
$self->_resolve_dir($upper, $key, "$title, $key")
Truth table100%2/2
A
A= $self->_resolve_dir($upper, $key, "$title, $key")
Aresult
11
00
189 187 push @{$spec{$key}{paths}}, $child;
190 }
191 }
192  
193 120 return \%spec;
194 }
195  
196 155 ▶sub _normalize_global {
Subroutine _normalize_global: called CC=4 SCAR=13.9
197 155 my ($self, $app, $conf) = @_;
198 155 ▶ my $spec = $self->_normalize_brand_spec('global', $conf, $app->home) // return;
Condition100%2/2
$self->_normalize_brand_spec("global", $conf, $app->home) // (return)
l!l
28119
MC/DC100%1/1
$self->_normalize_brand_spec("global", $conf, $app->home) // (return)
$self->_normalize_brand_spec("global", $conf, $app->home)
Truth table100%2/2
A
A= $self->_normalize_brand_spec("global", $conf, $app->home)
Aresult
11
00
199 croak 'ignore_app_paths not allowed in the global context'
200 if $spec->{templates}{ignore_app_paths}
201 28 ▶ || $spec->{public}{ignore_app_paths};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"templates"}{"ignore_app_paths"} or $spec->{"public"}{"ignore_app_paths"} 1 27 28
Condition66%2/3
$spec->{"templates"}{"ignore_app_paths"} or $spec->{"public"}{"ignore_app_paths"}
l!l&&r!l&&!r
1027
MC/DC50%1/2
$spec->{"templates"}{"ignore_app_paths"} or $spec->{"public"}{"ignore_app_paths"}
$spec->{"templates"}{"ignore_app_paths"} $spec->{"public"}{"ignore_app_paths"}
Truth table66%2/3
A or B
A= $spec->{"templates"}{"ignore_app_paths"}B= $spec->{"public"}{"ignore_app_paths"}
ABresult
1X1
011
000
202 27 return $spec;
203 }
204  
205 119 ▶sub _register_normalized_global {
Subroutine _register_normalized_global: called CC=2 SCAR=6.9
206 119 my ($self, $app, $spec) = @_;
207 119 ▶ return unless defined($spec);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 97 22 119
208 22 unshift @{$app->renderer->paths}, @{$spec->{templates}{paths}};
209 22 unshift @{$app->static->paths}, @{$spec->{public}{paths}};
210 22 return;
211 }
212  
213 146 ▶sub _register_individual {
Subroutine _register_individual: called CC=18 SCAR=28.9
214 146 my ($self, $app, $conf) = @_;
215  
216 146 ▶ return unless defined($conf);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf 0 146 146
217 146 ▶ croak 'individual requires a hash reference or nothing/undef'
Branch: 1 branch
BranchTrueFalseTotal
if ref $conf ne "HASH" 2 144 146
218 if ref($conf) ne 'HASH';
219  
220 144 my ($brands, $key) = __values_from_constrained_hash($conf,
221 'unknown keys in individual', [brands => {}], [stash_key => undef]);
222  
223 140 ▶ croak 'invalid brands in individual' if ref($brands) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $brands ne "HASH" 2 138 140
224 138 ▶ return unless defined($key) || scalar(keys(%$brands));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key or scalar keys %$brands 31 107 138
Condition100%2/2
defined $key or scalar keys %$brands
l!l
11127
MC/DC100%1/1
defined $key or scalar keys %$brands
defined $key
Truth table100%2/2
A
A= defined $key
Aresult
11
00
225  
226 107 my $home = $app->home;
227 $brands = {
228 107 map { $_ => $self->_normalize_brand_spec("brand '$_'", $brands->{$_}, $home) }
229 keys(%$brands)
230 };
231  
232 # an empty brand that ignores the application has no files at all,
233 # probably its directories are missing on this installation
234 91 for my $name (sort keys(%$brands)) {
235 96 ▶ next unless defined($brands->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $brands->{$name} 4 92 96
236 92 for my $key (qw< templates public >) {
237 184 my $spec = $brands->{$name}{$key};
238 $app->log->warn(qq{Branding: brand "$name" has no $key }
239 . 'directories and ignores the application ones')
240 184 ▶ if $spec->{ignore_app_paths} && ! @{$spec->{paths}};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"ignore_app_paths"} and not @{$spec->{"paths"};} 2 182 184
Condition100%3/3
$spec->{"ignore_app_paths"} and not @{$spec->{"paths"};}
!ll&&!rl&&r
17842
MC/DC100%2/2
$spec->{"ignore_app_paths"} and not @{$spec->{"paths"};}
$spec->{"ignore_app_paths"} not @{$spec->{"paths"};}
Truth table100%3/3
A and not(B)
A= $spec->{"ignore_app_paths"}B= not @{$spec->{"paths"};}
ABresult
0X0
100
111
241 }
242 }
243  
244 # this will be needed later, but we're going to make sure that the
245 # role is only composed once and this is also useful here to check that
246 # the stash_key is only set once or always to the same value.
247 91 my $static = $app->static;
248 91 my $static_role = 'Mojolicious::Plugin::Branding::IndividualStaticRole';
249  
250 91 ▶ if (Role::Tiny::does_role($static, $static_role)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Role::Tiny::does_role($static, $static_role)) { } 7 84 91
251 7 my $registered_key = $static->_stash_key;
252 7 ▶ $key //= $registered_key;
Condition66%2/3
$key //= $registered_key
l!l&&r!l&&!r
430
MC/DC0%0/2
$key //= $registered_key
$key $registered_key
Truth table66%2/3
A //= B
A= $keyB= $registered_key
ABresult
1X1
011
000
253 7 ▶ croak "stash_key '$key' conflicts with '$registered_key', set previously"
Branch: 1 branch
BranchTrueFalseTotal
if $key ne $registered_key 3 4 7
254 if $key ne $registered_key;
255 }
256 else { # first time here
257 84 $static->with_roles($static_role);
258  
259 84 ▶ $key //= 'branding.brand';
Condition100%2/2
$key //= "branding.brand"
l!l
777
MC/DC100%1/1
$key //= "branding.brand"
$key
Truth table100%2/2
A
A= $key
Aresult
11
00
260 84 $static->_stash_key($key);
261  
262 7 ▶ $app->helper(brand => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
263 7 my $c = shift;
264 7 ▶ return $c->stash($key) if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 7 7
265 7 $c->stash($key => shift);
266 7 return $c;
267 84 });
268  
269 # this is another thing that we only want to do once
270 84 $app->renderer
271 ->with_roles('Mojolicious::Plugin::Branding::IndividualRendererRole')
272 ->_stash_key($key);
273 }
274  
275 88 my %b4 = (templates => {}, public => {});
276 88 for my $name (keys(%$brands)) {
277 96 my $value = $brands->{$name};
278 96 for my $key (qw< templates public >) {
279 192 ▶ $b4{$key}{$name} = defined($value) ? $value->{$key} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 184 8 192
280 }
281 }
282 88 $app->renderer->_add_brands($b4{templates});
283 88 $static->_add_brands($b4{public});
284  
285 88 return;
286 }
287  
288 156 ▶sub register {
Subroutine register: called CC=2 SCAR=7.0
Pod documented
289 156 my ($self, $app, $conf) = @_;
290 156 ▶ my ($global, $individual) = __values_from_constrained_hash($conf // {},
Condition50%1/2
$conf // {}
l!l
1560
MC/DC0%0/1
$conf // {}
$conf
Truth table50%1/2
A
A= $conf
Aresult
11
00
291 'unknown keys in configuration',
292 [global => undef], [individual => {}]);
293  
294 # Validation and normalizations are needed before usage, so we do this
295 # in the following order:
296 # - validate/normalize global
297 # - validate/normalize individual
298 # - install individual
299 # - install global
300 155 my $global_spec = $self->_normalize_global($app, $global);
301 146 $self->_register_individual($app, $individual);
302 119 $self->_register_normalized_global($app, $global_spec);
303 119 return $self;
304 }
305  
306 8 1;
↑ Summary