Articulate/Storage/Local.pm

filter: Statementstmt 72.4% 121 / 167 Branchbran 46.0% 35 / 76 Conditioncond 43.7% 7 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 83.3% 25 / 30 Podpod 85.7% 12 / 14 totaltotal 64.2% 201 / 313 CC 50 SCAR 18.7
CC
50
Cov
63%
patch_meta
20.0
get_settings
20.0
get_settings_complete
20.0
CRAP 6.5
« Articulate/Flow/MetaSwitch.pm ↑ Summary Articulate/Location.pm »
1 package Articulate::Storage::Local;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 with 'Articulate::Role::Component';
7 6 with 'Articulate::Role::Storage';
8 5 ▶use Articulate::Syntax qw(hash_merge);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use IO::All;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Articulate::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Articulate::Content::Local - store your content locally
20  
21 =cut
22  
23 =head1 DESCRIPTION
24  
25 This content storage interface works by placing content and metadata in
26 a folder structure.
27  
28 For a given location, metadata is stored in C<meta.yml>, content in
29 C<content.blob>.
30  
31 Set C<content_base> in your config to specify where to place the
32 content.
33  
34 Caching is not implemented: get_content_cached simpy calls get_content.
35  
36 =cut
37  
38 =head1 METHODS
39  
40 =cut
41  
42 has content_base => (
43 is => 'rw',
44 lazy => 1, # because depends on app
45 default => sub {
46 5 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=8.1
47 return (
48 undef
49  
50 # $self->framework->appdir
51 5 ▶ // $FindBin::Bin
Condition33%1/3
undef // $FindBin::Bin
l!l&&r!l&&!r
050
MC/DC0%0/2
undef // $FindBin::Bin
undef $FindBin::Bin
Truth table33%1/3
A // B
A= undefB= $FindBin::Bin
ABresult
1X1
011
000
52 ) . '/content/';
53 },
54 coerce => sub {
55 5 ▶ my $content_base = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
56 5 ▶ unless ( -d $content_base ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $content_base) 1 4 5
57 1 File::Path::make_path $content_base;
58 1 ▶ throw_error( Internal => 'Could not initialise content base' )
Branch: 1 branch
BranchTrueFalseTotal
unless -d $content_base 0 1 1
59 unless ( -d $content_base );
60 }
61 5 return $content_base;
62 },
63 6 );
64  
65 sub ensure_exists { # internal method
66 10 ▶ my $self = shift;
Subroutine ensure_exists: called CC=4 SCAR=14.0
Pod undocumented
67 10 ▶ my $true_location_full = shift // return undef;
Condition50%1/2
shift() // (return undef)
l!l
100
MC/DC0%0/1
shift() // (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
68 10 my $true_location = $true_location_full;
69 10 $true_location =~ s~[^/]+\.[^/]+$~~; #:5.12 doesn't have s///r
70 10 ▶ unless ( -d $true_location ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $true_location) 3 7 10
71 3 File::Path::make_path $true_location;
72 }
73 10 ▶ return -d $true_location
Branch: 1 branch
BranchTrueFalseTotal
-d $true_location ? : 10 0 10
74 ? $true_location_full
75 : throw_error( 'Internal' => 'Could not create directory for location' );
76 }
77  
78 sub true_location {
79 69 ▶ my $self = shift;
Subroutine true_location: called CC=1 SCAR=0.0
Pod undocumented
80 69 return $self->content_base . shift;
81 }
82  
83 =head3 get_item
84  
85 $storage->get_item( 'zone/public/article/hello-world' )
86  
87 Retrieves the metadata for the content at that location.
88  
89 =cut
90  
91 sub get_item {
92 2 ▶ my $self = shift;
Subroutine get_item: called CC=3 SCAR=11.1
Pod documented
93 2 my $location = shift->location;
94 2 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 2 2
95 unless $self->navigation->valid_location($location);
96 2 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 0 2 2
97 unless $self->item_exists($location);
98 2 my $item = $self->construction->construct( { location => $location } );
99 2 $item->meta( $self->get_meta($item) );
100 2 $item->content( $self->get_content($item) );
101 2 return $item;
102 }
103  
104 =head3 get_meta
105  
106 $storage->get_meta( 'zone/public/article/hello-world' )
107  
108 Retrieves the metadata for the content at that location.
109  
110 =cut
111  
112 sub get_meta {
113 5 ▶ my $self = shift;
Subroutine get_meta: called CC=4 SCAR=14.2
Pod documented
114 5 my $item = shift;
115 5 my $location = $item->location;
116 5 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 5 5
117 unless $self->navigation->valid_location($location);
118 5 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 2 3 5
119 unless $self->item_exists($location);
120 3 my $fn = $self->true_location( $location . '/meta.yml' );
121 3 ▶ return YAML::LoadFile($fn) if -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn 3 0 3
122 0 return {};
123 }
124  
125 =head3 set_meta
126  
127 $storage->set_meta( 'zone/public/article/hello-world', {...} )
128  
129 Sets the metadata for the content at that location.
130  
131 =cut
132  
133 sub set_meta {
134 7 ▶ my $self = shift;
Subroutine set_meta: called CC=3 SCAR=11.0
Pod documented
135 7 my $item = shift;
136 7 my $location = $item->location;
137 7 ▶ throw_error Internal => "Bad location " . $location
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 7 7
138 unless $self->navigation->valid_location($location);
139 5 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 1 4 5
140 unless $self->item_exists($location);
141 3 my $fn =
142 $self->ensure_exists( $self->true_location( $location . '/meta.yml' ) );
143 3 YAML::DumpFile( $fn, $item->meta );
144 3 return $item;
145 }
146  
147 =head3 patch_meta
148  
149 $storage->patch_meta( 'zone/public/article/hello-world', {...} )
150  
151 Alters the metadata for the content at that location. Existing keys are
152 retained.
153  
154 CURRENTLY this affects top-level keys only, but a descent algorigthm is
155 planned.
156  
157 =cut
158  
159 sub patch_meta {
160 0 ▶ my $self = shift;
Subroutine patch_meta: not called CC=4 SCAR=30.0
Pod documented
161 0 my $item = shift;
162 0 my $location = $item->location;
163 0 ▶ throw_error Internal => "Bad location " . $location
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 0 0
164 unless $self->navigation->valid_location($location);
165 0 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 0 0 0
166 unless $self->item_exists($location);
167 0 my $fn =
168 $self->ensure_exists( $self->true_location( $location . '/meta.yml' ) );
169 0 my $old_data = {};
170 0 ▶ $old_data = YAML::LoadFile($fn) if -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn 0 0 0
171 0 return YAML::DumpFile( $fn, hash_merge( $old_data, $item->meta ) );
172 }
173  
174 =head3 get_settings
175  
176 $storage->get_settings('zone/public/article/hello-world')
177  
178 Retrieves the settings for the content at that location.
179  
180 =cut
181  
182 sub get_settings {
183 0 ▶ my $self = shift;
Subroutine get_settings: not called CC=4 SCAR=30.0
Pod documented
184 0 my $item = shift;
185 0 my $location = $item->location;
186 0 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 0 0
187 unless $self->navigation->valid_location($location);
188 0 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 0 0 0
189 unless $self->item_exists($location);
190 0 my $fn = $self->true_location( $location . '/settings.yml' );
191 0 ▶ return YAML::LoadFile($fn) if -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn 0 0 0
192 0 return {};
193 }
194  
195 =head3 set_settings
196  
197 $storage->set_settings('zone/public/article/hello-world', $amended_settings)
198  
199 Retrieves the settings for the content at that location.
200  
201 =cut
202  
203 sub set_settings {
204 0 ▶ my $self = shift;
Subroutine set_settings: not called CC=3 SCAR=24.8
Pod documented
205 0 my $location = shift->location;
206 0 my $settings = shift;
207 0 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 0 0
208 unless $self->navigation->valid_location($location);
209 0 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 0 0 0
210 unless $self->item_exists($location);
211 0 my $fn =
212 $self->ensure_exists( $self->true_location( $location . '/settings.yml' ) );
213 0 YAML::DumpFile( $fn, $settings );
214 0 return $settings;
215 }
216  
217 =head3 get_settings_complete
218  
219 $storage->get_settings_complete('zone/public/article/hello-world')
220  
221 Retrieves the settings for the content at that location.
222  
223 =cut
224  
225 sub get_settings_complete {
226 0 ▶ my $self = shift;
Subroutine get_settings_complete: not called CC=4 SCAR=30.0
Pod documented
227 0 my $location = shift->location;
228 0 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 0 0
229 unless $self->navigation->valid_location($location);
230 0 my @paths = split /\//, $location;
231 0 my $current_path = $self->true_location('') . '/';
232 0 my $settings = {};
233 0 foreach my $p (@paths) {
234 0 my $fn = $current_path . 'settings.yml';
235 0 my $lvl_settings = {};
236 0 ▶ $lvl_settings = YAML::LoadFile($fn) if -e $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn 0 0 0
237 0 $settings = hash_merge( $settings, $lvl_settings );
238 }
239 0 return $settings;
240 }
241  
242 =head3 get_content
243  
244 $storage->get_content('zone/public/article/hello-world')
245  
246 Retrieves the content at that location.
247  
248 =cut
249  
250 sub get_content {
251 10 ▶ my $self = shift;
Subroutine get_content: called CC=4 SCAR=14.0
Pod documented
252 10 my $location = shift->location;
253 10 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 10 10
254 unless $self->navigation->valid_location($location);
255 10 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 2 8 10
256 unless $self->item_exists($location);
257 8 my $fn = $self->true_location( $location . '/content.blob' );
258 8 ▶ open my $fh, '<', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $fn or throw_error("Internal", "Cannot open file $fn to read") 0 8 8
259 or throw_error Internal => "Cannot open file $fn to read";
260 8 return '' . ( join '', <$fh> );
261 }
262  
263 =head3 set_content
264  
265 $storage->set_content('zone/public/article/hello-world', $blob);
266  
267 Places content at that location.
268  
269 =cut
270  
271 sub _is_upload {
272 5 ▶ my $content = shift;
Subroutine _is_upload: called CC=2 SCAR=8.1
273 5 ▶ return ( blessed $content and $content->isa('Articulate::File') )
Condition33%1/3
builtin::blessed($content) && $content->isa('Articulate::File')
!ll&&!rl&&r
500
MC/DC0%0/2
builtin::blessed($content) && $content->isa('Articulate::File')
builtin::blessed($content) $content->isa('Articulate::File')
Truth table33%1/3
A && B
A= builtin::blessed($content)B= $content->isa('Articulate::File')
ABresult
0X0
100
111
274 ; # todo: have this wrapped by an articulate class which interfaces with the FrameworkAdapter
275 }
276  
277 sub _write_data {
278 5 ▶ my ( $content, $fn ) = @_;
Subroutine _write_data: called CC=2 SCAR=7.0
279 5 ▶ open my $fh, '>', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fn or throw_error("Internal", "Cannot open file $fn to write") 0 5 5
280 or throw_error Internal => "Cannot open file $fn to write";
281 5 print $fh $content;
282 5 close $fh;
283 }
284  
285 sub _copy_upload {
286 0 ▶ my ( $content, $fn ) = @_;
Subroutine _copy_upload: not called CC=3 SCAR=24.8
287 0 my $content_fh = $content->io;
288 0 local $/;
289 0 ▶ open my $fh, '>', $fn
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $fn or throw_error("Internal", "Cannot open file $fn to write") 0 0 0
290 or throw_error Internal => "Cannot open file $fn to write";
291 0 binmode $fh, ':raw';
292 0 ▶ print $fn while <$content_fh>;
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $content_fh) and do { print $fn } 0 0 0
293 0 close $fh;
294 }
295  
296 sub _write_content {
297 5 ▶ my ( $content, $fn ) = @_;
Subroutine _write_content: called CC=3 SCAR=12.0
298 5 ▶ $content //= '';
Condition50%1/2
$content //= ""
l!l
50
MC/DC0%0/1
$content //= ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
299 5 ▶ if ( _is_upload($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_upload($content)) { } 0 5 5
300 0 _copy_upload( $content, $fn );
301 }
302 else {
303 5 _write_data( $content, $fn );
304 }
305 }
306  
307 sub set_content {
308 2 ▶ my $self = shift;
Subroutine set_content: called CC=3 SCAR=11.1
Pod documented
309 2 my $item = shift;
310 2 my $location = $item->location;
311 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 3 3
312 unless $self->navigation->valid_location($location);
313 ▶ throw_error NotFound => "No content at $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->item_exists($location) 1 2 3
314 unless $self->item_exists($location);
315 my $fn =
316 $self->ensure_exists( $self->true_location( $location . '/content.blob' ) );
317 _write_content( $item->content, $fn );
318 return $location;
319 }
320  
321 =head3 create_item
322  
323 $storage->create_item('zone/public/article/hello-world', $meta, $blob);
324  
325 Places meta and content at that location.
326  
327 =cut
328  
329 sub create_item {
330 7 ▶ my $self = shift;
Subroutine create_item: called CC=3 SCAR=11.0
Pod documented
331 7 my $item = shift;
332 7 my $location = $item->location;
333 7 ▶ throw_error Internal => "Bad location " . $location
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 7 7
334 unless $self->navigation->valid_location($location);
335 7 ▶ throw_error AlreadyExists => "Cannot create: item already exists at "
Branch: 1 branch
BranchTrueFalseTotal
if $self->item_exists($location) 2 5 7
336 . $location
337 if $self->item_exists($location);
338 {
339 5 my $fn =
340 $self->ensure_exists(
341 $self->true_location( $location . '/content.blob' ) );
342 5 _write_content( $item->content, $fn );
343 }
344 {
345 5 my $fn =
346 $self->ensure_exists( $self->true_location( $location . '/meta.yml' ) );
347 4 YAML::DumpFile( $fn, $item->meta );
348 }
349 4 $item->content( $self->get_content($item) );
350 4 return $item;
351 }
352  
353 =head3 item_exists
354  
355 if ($storage->item_exists( 'zone/public/article/hello-world')) {
356 ...
357 }
358  
359 Determines if the item has been created (only the C<meta.yml> is
360 tested).
361  
362 =cut
363  
364 sub item_exists {
365 44 ▶ my $self = shift;
Subroutine item_exists: called CC=2 SCAR=7.0
Pod documented
366 44 my $location = shift->location;
367 44 ▶ throw_error Internal => "Bad location $location"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->navigation->valid_location($location) 0 44 44
368 unless $self->navigation->valid_location($location);
369 44 return -e $self->true_location( $location . '/meta.yml' );
370 }
371  
372 =head3 list_items
373  
374 $storage->list_items ('/zone/public'); # 'hello-world', 'second-item' )
375  
376 Returns a list of items in the.
377  
378 =cut
379  
380 sub list_items {
381 2 ▶ my $self = shift;
Subroutine list_items: called CC=7 SCAR=19.8
Pod documented
382 2 my $location = shift->location;
383  
384 # throw_error Internal => "Bad location $location" unless $self->navigation->valid_location( $location ); # actually, no, because /zone fails but /zone/foo passes
385 2 my $true_location = $self->true_location($location);
386 2 my @contents;
387 2 ▶ return @contents unless -d $true_location;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $true_location 0 2 2
388 2 ▶ opendir( my $dh, $true_location )
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $true_location or throw_error("NotFound", 'Could not open ' . $true_location) 0 2 2
389 or throw_error NotFound => ( 'Could not open ' . $true_location );
390 2 ▶ while ( my $fn = readdir $dh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = readdir $dh)) 6 2 8
391 6 my $child_dn = $true_location . '/' . $fn;
392 6 ▶ next unless -d $child_dn;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $child_dn 0 6 6
393 6 ▶ push @contents, $fn
Branch: 1 branch
BranchTrueFalseTotal
if $self->navigation->valid_location($location . '/' . $fn) and $self->item_exists(loc($location . '/' . $fn)) 2 4 6
Condition66%2/3
$self->navigation->valid_location($location . '/' . $fn) and $self->item_exists(loc($location . '/' . $fn))
!ll&&!rl&&r
042
MC/DC50%1/2
$self->navigation->valid_location($location . '/' . $fn) and $self->item_exists(loc($location . '/' . $fn))
$self->navigation->valid_location($location . '/' . $fn) $self->item_exists(loc($location . '/' . $fn))
Truth table66%2/3
A and B
A= $self->navigation->valid_location($location . '/' . $fn)B= $self->item_exists(loc($location . '/' . $fn))
ABresult
0X0
100
111
394 if $self->navigation->valid_location( $location . '/' . $fn )
395 and $self->item_exists( loc $location. '/' . $fn );
396 }
397 2 return @contents;
398 }
399  
400 =head3 empty_all_content
401  
402 $storage->empty_all_content;
403  
404 Removes all content. This is totally irreversible, unless you took a
405 backup!
406  
407 =cut
408  
409 sub empty_all_content {
410 my $self = shift;
411 my $true_location = $self->content_base;
412  
413 ▶ throw_error Internal => "Won't empty all content, this looks too dangerous"
Condition33%1/3
-d "$true_location/.git" or -f "$true_location/Makefile.PL"
l!l&&r!l&&!r
005
MC/DC0%0/2
-d "$true_location/.git" or -f "$true_location/Makefile.PL"
-d "$true_location/.git" -f "$true_location/Makefile.PL"
Truth table33%1/3
A or B
A= -d "$true_location/.git"B= -f "$true_location/Makefile.PL"
ABresult
1X1
011
000
414 if ( -d "$true_location/.git"
415 or -f "$true_location/Makefile.PL" );
416  
417 File::Path::remove_tree( $self->content_base, { keep_root => 1 } );
418 }
419  
420 =head3 delete_item
421  
422 $storage->delete_item ('/zone/public');
423  
424 Deletes the item and all its descendants.
425  
426 =cut
427  
428 sub delete_item {
429 my $self = shift;
430 my $location = shift->location;
431  
432 throw_error Internal => "Use empty_all_content instead to delete the root"
433 if "$location" eq '/';
434 throw_error Internal => "Bad location $location"
435 unless $self->navigation->valid_location($location);
436 throw_error NotFound => "No content at $location"
437 unless $self->item_exists($location);
438  
439 my $true_location = $self->true_location($location);
440 File::Path::remove_tree($true_location);
441 }
442  
443 =head1 SEE ALSO
444  
445 =over
446  
447 =item * L<Articulate>
448  
449 =item * L<Articulate::Storage::DBIC::Simple>
450  
451 =back
452  
453 =cut
454  
455 6 1;
« Articulate/Flow/MetaSwitch.pm ↑ Summary Articulate/Location.pm »