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