Articulate/Storage/Local.pm

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