Articulate/Service/Simple.pm

filter: Statementstmt 94.8% 73 / 77 Branchbran 50.0% 18 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 82.6% 105 / 127 SCAR 30.8
CC
19
Cov
81%
handle_update
10.5
handle_create
6.4
handle_delete
4.2
CRAP 21.7
« Articulate/Authentication/Internal.pm ↑ Summary Articulate/Flow/ContentType.pm »
1 package Articulate::Service::Simple;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Articulate::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 with 'Articulate::Role::Service';
9  
10  
11 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Articulate::Service::Simple - provide create, read, update, delete
17  
18 =cut
19  
20 =head1 METHODS
21  
22 =head3 handle_create
23  
24  
25 create => {
26 meta => {}
27 content => '...',
28 location => '...'
29 }
30  
31 Creates new content. Throws an error if the content already exists or if the user has no write permission on that location.
32  
33 =head3 handle_read
34  
35 read => {
36 location => '...'
37 }
38  
39 Retrieves the content at that location. Throws an error if the content does not exist or if the user has no read permission on that location.
40  
41 =head3 handle_update
42  
43 update => {
44 meta => {}
45 content => '...',
46 location => '...'
47 }
48  
49 Updates the content at that location. Throws an error if the content does not exist or if the user has no write permission on that location.
50  
51 =head3 handle_delete
52  
53 delete => {
54 location => '...'
55 }
56  
57 Deletes the content at that location. Throws an error if the content does not exist or if the user has no write permission on that location.
58  
59 =cut
60  
61 sub handle_create {
62 1 ▶ my $self = shift;
Subroutine handle_create: called CC=6 SCAR=18.6
Pod documented
63 1 my $request = shift;
64 my $item = blessed $request->data ? $request->data : $self->construction->construct( {
65 meta => {},
66 1 ▶ (%{$request->data} ? %{$request->data} : ()),
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($request->data) ? : 0 1 1
%{$request->data;} ? : 1 0 1
67 } );
68 1 my $location = $item->location;
69  
70 1 my $user = $request->user_id;
71 1 my $permission = $self->authorisation->permitted ( $user, write => $location );
72 1 ▶ if ( $permission ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($permission) { } 1 0 1
73  
74 1 ▶ throw_error 'AlreadyExists' if $self->storage->item_exists($location);
Branch: 1 branch
BranchTrueFalseTotal
if $self->storage->item_exists($location) 0 1 1
75  
76 1 ▶ $self->validation->validate ($item) or throw_error BadRequest => 'The content did not validate';
Branch: 1 branch
BranchTrueFalseTotal
$self->validation->validate($item) or throw_error("BadRequest", "The content did not validate") 0 1 1
77 1 $self->enrichment->enrich ($item, $request); # this will throw if it fails
78 1 $self->storage->create_item ($item); # this will throw if it fails
79  
80 1 my $item_class = $item->location->[-2];
81 1 $self->augmentation->augment ($item); # this will throw if it fails
82  
83 return response $item_class, {
84 $item_class => {
85 schema => $item->meta->{schema},
86 1 content => $item->content,
87 location => $item->location, # as string or arrayref?
88 },
89 };
90 }
91 else {
92 0 throw_error Forbidden => $permission->reason;
93 }
94  
95 }
96  
97 sub handle_read {
98 1 ▶ my $self = shift;
Subroutine handle_read: called CC=3 SCAR=11.2
Pod documented
99 1 my $request = shift;
100 1 my $location = loc $request->data->{location};
101 1 my $user = $request->user_id;
102 1 my $permission = $self->authorisation->permitted ( $user, read => $location );
103 1 ▶ if ( $permission ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($permission) { } 1 0 1
104 1 ▶ throw_error 'NotFound' unless $self->storage->item_exists($location);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->storage->item_exists($location) 0 1 1
105 1 my $item = $self->construction->construct( {
106 meta => $self->storage->get_meta_cached ($location),
107 content => $self->storage->get_content_cached ($location),
108 location => $location,
109 } );
110  
111 1 my $item_class = $item->location->[-2];
112  
113 1 $self->augmentation->augment ($item); # or throw
114  
115 return response $item_class => {
116 $item_class => {
117 schema => $item->meta->{schema},
118 1 content => $item->content,
119 },
120 };
121 }
122 else {
123 0 return throw_error Forbidden => $permission->reason;
124 }
125 }
126  
127 sub handle_update {
128 1 ▶ my $self = shift;
Subroutine handle_update: called CC=9 SCAR=23.5
Pod documented
129 1 my $request = shift;
130  
131 my $item = blessed $request->data ? $request->data : $self->construction->construct( {
132 meta => {},
133 1 ▶ (%{$request->data} ? %{$request->data} : ()),
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($request->data) ? : 0 1 1
%{$request->data;} ? : 1 0 1
134 } );
135 1 my $location = $item->location;
136  
137 1 my $user = $request->user_id;
138 1 my $permission = $self->authorisation->permitted ( $user, write => $location );
139 1 ▶ if ( $permission ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($permission) { } 1 0 1
140  
141 1 ▶ throw_error 'NotFound' unless $self->storage->item_exists($location);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->storage->item_exists($location) 0 1 1
142  
143 1 ▶ $self->validation->validate ($item) or throw_error BadRequest => 'The content did not validate'; # or throw
Branch: 1 branch
BranchTrueFalseTotal
$self->validation->validate($item) or throw_error("BadRequest", "The content did not validate") 0 1 1
144 1 $self->enrichment->enrich ($item, $request); # this will throw if it fails
145 1 ▶ $self->storage->set_meta ($item) or throw_error 'Internal'; # or throw
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->set_meta($item) or throw_error("Internal") 0 1 1
146 1 ▶ $self->storage->set_content ($item) or throw_error 'Internal'; # or throw
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->set_content($item) or throw_error("Internal") 0 1 1
147  
148 1 my $item_class = $item->location->[-2];
149 1 ▶ $self->augmentation->augment ($item) or throw_error 'Internal'; # or throw
Branch: 1 branch
BranchTrueFalseTotal
$self->augmentation->augment($item) or throw_error("Internal") 0 1 1
150  
151 return response $item_class, {
152 $item_class => {
153 schema => $item->meta->{schema},
154 1 content => $item->content,
155 location => $item->location, # as string or arrayref?
156 },
157 };
158 }
159 else {
160 0 return throw_error Forbidden => $permission->reason;
161 }
162  
163 }
164  
165 sub handle_delete {
166 1 ▶ my $self = shift;
Subroutine handle_delete: called CC=4 SCAR=14.5
Pod documented
167 1 my $request = shift;
168  
169 1 my $location = loc $request->data->{location};
170  
171 1 my $user = $request->user_id;
172 1 my $permission = $self->authorisation->permitted ( $user, write => $location );
173 1 ▶ if ( $permission ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($permission) { } 1 0 1
174 1 ▶ throw_error 'NotFound' unless $self->storage->item_exists($location);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->storage->item_exists($location) 0 1 1
175 1 ▶ $self->storage->delete_item ($location) or throw_error 'Internal'; # or throw
Branch: 1 branch
BranchTrueFalseTotal
$self->storage->delete_item($location) or throw_error("Internal") 0 1 1
176 1 return response 'success', { };
177 }
178 else {
179 0 return throw_error Forbidden => $permission->reason;
180 }
181  
182 }
183  
184 4 1;
« Articulate/Authentication/Internal.pm ↑ Summary Articulate/Flow/ContentType.pm »