API.pm

filter: Statementstmt 89.6% 285 / 318 Branchbran 73.0% 76 / 104 Conditioncond 57.6% 34 / 59 MC/DCmcdc 25.7% 9 / 35 Subroutinesub 100% 72 / 72 Podpod 100% 31 / 31 totaltotal 81.9% 507 / 619 CC 85 SCAR 26.8
CC
85
Cov
82%
validate_object
58.4
insert_object_from_params
16.0
end
13.0
CRAP 14.6
« API/RequestArguments.pm ↑ Summary API/StoredResultSource.pm »
1 package Catalyst::Controller::DBIC::API;
2 16 $Catalyst::Controller::DBIC::API::VERSION = '2.009000';
3 #ABSTRACT: Provides a DBIx::Class web service automagically
4 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶BEGIN { extends 'Catalyst::Controller'; }
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 ▶use CGI::Expand ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use DBIx::Class::ResultClass::HashRefInflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Test::Deep::NoTest('eq_deeply');
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use MooseX::Types::Moose(':all');
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Scalar::Util( 'blessed', 'reftype' );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use Catalyst::Controller::DBIC::API::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use DBIx::Class::ResultSet::RecursiveUpdate;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 16 has '_json' => (
20 is => 'ro',
21 isa => JSON::MaybeXS::JSON(),
22 lazy_build => 1,
23 );
24  
25 11 ▶sub _build__json {
Subroutine _build__json: called CC=1 SCAR=0.0
26  
27 # no ->utf8 here because the request params get decoded by Catalyst
28 11 return JSON::MaybeXS->new;
29 }
30  
31 16 with 'Catalyst::Controller::DBIC::API::StoredResultSource',
32 'Catalyst::Controller::DBIC::API::StaticArguments';
33  
34 16 with 'Catalyst::Controller::DBIC::API::RequestArguments' => { static => 1 };
35  
36 16 __PACKAGE__->config();
37  
38  
39  
40 106 ▶sub begin : Private {
Subroutine begin: called CC=1 SCAR=0.0
Pod documented
41 106 my ( $self, $c ) = @_;
42  
43 106 Moose::Util::ensure_all_roles( $c->req,
44 'Catalyst::Controller::DBIC::API::Request' );
45 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47  
48 103 ▶sub setup : Chained('specify.in.subclass.config') : CaptureArgs(0) :
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
49 16 ▶ PathPart('specify.in.subclass.config') { }
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52 106 ▶sub deserialize : Chained('setup') : CaptureArgs(0) : PathPart('') :
Subroutine deserialize: called CC=7 SCAR=19.5
Pod documented
53 ActionClass('Deserialize') {
54 106 my ( $self, $c ) = @_;
55 106 my $req_params;
56  
57 106 ▶ if ( $c->req->data && scalar( keys %{ $c->req->data } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->data and scalar keys %{$c->req->data;}) { } 16 90 106
Condition100%2/2
$c->req->data and scalar keys %{$c->req->data;}
!ll
8917
MC/DC100%1/1
$c->req->data and scalar keys %{$c->req->data;}
$c->req->data
Truth table100%2/2
A
A= $c->req->data
Aresult
00
11
58 16 $req_params = $c->req->data;
59 }
60 else {
61 90 $req_params = CGI::Expand->expand_hash( $c->req->params );
62  
63 90 foreach my $param (
64 90 @{ [ $self->search_arg, $self->count_arg,
65 $self->page_arg, $self->offset_arg,
66 $self->ordered_by_arg, $self->grouped_by_arg,
67 $self->prefetch_arg
68 ]
69 }
70 )
71 {
72 # these params can also be composed of JSON
73 # but skip if the parameter is not provided
74 630 ▶ next if not exists $req_params->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $req_params->{$param} 564 66 630
75  
76 # find out if CGI::Expand was involved
77 66 ▶ if ( ref $req_params->{$param} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $req_params->{$param} eq 'HASH') { } 25 41 66
78 25 for my $key ( keys %{ $req_params->{$param} } ) {
79  
80 # copy the value because JSON::XS will alter it
81 # even if decoding failed
82 27 my $value = $req_params->{$param}->{$key};
83 27 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 27 my $deserialized = $self->_json->decode($value);
85 6 $req_params->{$param}->{$key} = $deserialized;
86 }
87 21 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.6
88 21 ▶ $c->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 21 21
89 "Param '$param.$key' did not deserialize appropriately: $_"
90 ) if $c->debug;
91 }
92 27 }
93 }
94 else {
95 41 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
96 41 my $value = $req_params->{$param};
97 41 my $deserialized = $self->_json->decode($value);
98 24 $req_params->{$param} = $deserialized;
99 }
100 17 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=7.6
101 17 ▶ $c->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 17 17
102 "Param '$param' did not deserialize appropriately: $_"
103 ) if $c->debug;
104 }
105 41 }
106 }
107 }
108  
109 106 $self->inflate_request( $c, $req_params );
110 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112  
113 97 ▶sub generate_rs {
Subroutine generate_rs: called CC=2 SCAR=7.1
Pod documented
114 97 my ( $self, $c ) = @_;
115  
116 97 ▶ return $c->model( $c->stash->{class} || $self->class );
Condition66%2/3
$c->stash->{'class'} || $self->class
l!l&&r!l&&!r
7900
MC/DC0%0/2
$c->stash->{'class'} || $self->class
$c->stash->{'class'} $self->class
Truth table66%2/3
A || B
A= $c->stash->{'class'}B= $self->class
ABresult
1X1
011
000
117 }
118  
119  
120 106 ▶sub inflate_request {
Subroutine inflate_request: called CC=1 SCAR=0.0
Pod documented
121 106 my ( $self, $c, $params ) = @_;
122  
123 106 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 # set static arguments
125 106 $c->req->_set_controller($self);
126  
127 # set request arguments
128 106 $c->req->_set_request_data($params);
129  
130 # set the current resultset
131 97 $c->req->_set_current_result_set( $self->generate_rs($c) );
132  
133 }
134 10 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 10 $c->log->error($_);
136 10 $self->push_error( $c, { message => $_ } );
137 10 $c->detach();
138 }
139 106 }
140  
141  
142 23 ▶sub object_with_id : Chained('deserialize') : CaptureArgs(1) : PathPart('') {
Subroutine object_with_id: called CC=2 SCAR=6.9
Pod documented
143 23 my ( $self, $c, $id ) = @_;
144  
145 23 my $vals = $c->req->request_data->{ $self->data_root };
146 23 ▶ unless ( defined($vals) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vals) 23 0 23
147  
148 # no data root, assume the request_data itself is the payload
149 23 $vals = $c->req->request_data;
150 }
151  
152 23 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 # there can be only one set of data
154 23 $c->req->add_object( [ $self->object_lookup( $c, $id ), $vals ] );
155 }
156 8 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 8 $c->log->error($_);
158 8 $self->push_error( $c, { message => $_ } );
159 8 $c->detach();
160 }
161 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163  
164 43 ▶sub objects_no_id : Chained('deserialize') : CaptureArgs(0) : PathPart('') {
Subroutine objects_no_id: called CC=8 SCAR=22.1
Pod documented
165 43 my ( $self, $c ) = @_;
166  
167 43 ▶ if ( $c->req->has_request_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->has_request_data) 43 0 43
168 43 my $data = $c->req->request_data;
169 43 my $vals;
170  
171 43 ▶ if ( exists( $data->{ $self->data_root } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$data{$self->data_root} and defined $$data{$self->data_root}) { } 9 34 43
Condition66%2/3
exists $$data{$self->data_root} and defined $$data{$self->data_root}
!ll&&!rl&&r
3409
MC/DC50%1/2
exists $$data{$self->data_root} and defined $$data{$self->data_root}
exists $$data{$self->data_root} defined $$data{$self->data_root}
Truth table66%2/3
A and B
A= exists $$data{$self->data_root}B= defined $$data{$self->data_root}
ABresult
0X0
100
111
172 && defined( $data->{ $self->data_root } ) )
173 {
174 9 my $root = $data->{ $self->data_root };
175 9 ▶ if ( reftype($root) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($root) eq 'ARRAY') { } 9 0 9
elsif (builtin::reftype($root) eq 'HASH') { } 0 0 0
176 9 $vals = $root;
177 }
178 elsif ( reftype($root) eq 'HASH' ) {
179 0 $vals = [$root];
180 }
181 else {
182 0 $c->log->error('Invalid request data');
183 0 $self->push_error( $c,
184 { message => 'Invalid request data' } );
185 0 $c->detach();
186 }
187 }
188 else {
189 # no data root, assume the request_data itself is the payload
190 34 $vals = [ $c->req->request_data ];
191 }
192  
193 43 foreach my $val (@$vals) {
194 55 ▶ unless ( exists( $val->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $val->{"id"}) 38 17 55
195 38 $c->req->add_object(
196 [ $c->req->current_result_set->new_result( {} ), $val ] );
197 38 next;
198 }
199  
200 17 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
201 $c->req->add_object(
202 17 [ $self->object_lookup( $c, $val->{id} ), $val ] );
203 }
204 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 2 $c->log->error($_);
206 2 $self->push_error( $c, { message => $_ } );
207 2 $c->detach();
208 }
209 17 }
210 }
211 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213  
214 40 ▶sub object_lookup {
Subroutine object_lookup: called CC=4 SCAR=14.5
Pod documented
215 40 my ( $self, $c, $id ) = @_;
216  
217 40 ▶ die 'No valid ID provided for look up' unless defined $id and length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 40 40
Condition33%1/3
defined $id and length $id
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table33%1/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
218 40 my $object = $c->req->current_result_set->find($id);
219 40 ▶ die "No object found for id '$id'" unless defined $object;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object 10 30 40
220 30 return $object;
221 }
222  
223  
224 54 ▶sub list {
Subroutine list: called CC=1 SCAR=0.0
Pod documented
225 54 my ( $self, $c ) = @_;
226  
227 54 $self->list_munge_parameters($c);
228 54 $self->list_perform_search($c);
229 53 $self->list_format_output($c);
230  
231 # make sure there are no objects lingering
232 49 $c->req->clear_objects();
233 }
234  
235  
236 52 ▶sub list_munge_parameters { } # noop by default
Subroutine list_munge_parameters: called CC=1 SCAR=0.0
Pod documented
237  
238  
239 54 ▶sub list_perform_search {
Subroutine list_perform_search: called CC=1 SCAR=0.0
Pod documented
240 54 my ( $self, $c ) = @_;
241  
242 54 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.5
243 54 my $req = $c->req;
244  
245 54 my $rs =
246 $req->current_result_set->search( $req->search_parameters,
247 $req->search_attributes );
248  
249 53 $req->_set_current_result_set($rs);
250  
251 $req->_set_search_total_entries(
252 $req->current_result_set->pager->total_entries )
253 if $req->has_search_attributes
254 && ( exists( $req->search_attributes->{page} )
255 && defined( $req->search_attributes->{page} )
256 53 ▶ && length( $req->search_attributes->{page} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $req->has_search_attributes and (exists $req->search_attributes->{"page"} and defined $req->search_attributes->{"page"} and length $req->search_attributes->{"page"}) 13 40 53
Condition66%6/9
$req->has_search_attributes and (exists $req->search_attributes->{"page"} and defined $req->search_attributes->{"page"} and length $req->search_attributes->{"page"})
!ll&&!rl&&r
04013
exists $req->search_attributes->{"page"} and defined $req->search_attributes->{"page"} and length $req->search_attributes->{"page"}
!ll&&!rl&&r
40013
exists $req->search_attributes->{"page"} and defined $req->search_attributes->{"page"}
!ll&&!rl&&r
40013
MC/DC25%1/4
$req->has_search_attributes and (exists $req->search_attributes->{"page"} and defined $req->search_attributes->{"page"} and length $req->search_attributes->{"page"})
$req->has_search_attributes exists $req->search_attributes->{"page"} defined $req->search_attributes->{"page"} length $req->search_attributes->{"page"}
Truth table40%2/5
A and B and C and D
A= $req->has_search_attributesB= exists $req->search_attributes->{"page"}C= defined $req->search_attributes->{"page"}D= length $req->search_attributes->{"page"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
257 }
258 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
259 1 $c->log->error($_);
260 1 $self->push_error( $c,
261 { message => 'a database error has occured.' } );
262 1 $c->detach();
263 }
264 54 }
265  
266  
267 53 ▶sub list_format_output {
Subroutine list_format_output: called CC=2 SCAR=6.9
Pod documented
268 53 my ( $self, $c ) = @_;
269  
270 53 my $rs = $c->req->current_result_set->search;
271 53 ▶ $rs->result_class( $self->result_class ) if $self->result_class;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result_class 53 0 53
272  
273 53 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
274 53 my $output = {};
275 53 my $formatted = [];
276  
277 53 foreach my $row ( $rs->all ) {
278 147 push( @$formatted, $self->row_format_output( $c, $row ) );
279 }
280  
281 49 $output->{ $self->data_root } = $formatted;
282  
283 49 ▶ if ( $c->req->has_search_total_entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->has_search_total_entries) 11 38 49
284 11 $output->{ $self->total_entries_arg } =
285 $c->req->search_total_entries + 0;
286 }
287  
288 49 $c->stash->{ $self->stash_key } = $output;
289 }
290 4 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
291 4 $c->log->error($_);
292 4 $self->push_error( $c,
293 { message => 'a database error has occured.' } );
294 4 $c->detach();
295 }
296 53 }
297  
298  
299 147 ▶sub row_format_output {
Subroutine row_format_output: called CC=1 SCAR=0.0
Pod documented
300  
301 #my ($self, $c, $row) = @_;
302 147 my ( $self, undef, $row ) = @_;
303 147 return $row; # passthrough by default
304 }
305  
306  
307 3 ▶sub item {
Subroutine item: called CC=2 SCAR=9.2
Pod documented
308 3 my ( $self, $c ) = @_;
309  
310 3 ▶ if ( $c->req->count_objects != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->count_objects != 1) { } 0 3 3
311 0 $c->log->error($_);
312 0 $self->push_error( $c,
313 { message => 'No objects on which to operate' } );
314 0 $c->detach();
315 }
316 else {
317 3 $c->stash->{ $self->stash_key }->{ $self->item_root } =
318 $self->each_object_inflate( $c, $c->req->get_object(0)->[0] );
319 }
320 }
321  
322  
323 24 ▶sub update_or_create {
Subroutine update_or_create: called CC=2 SCAR=8.1
Pod documented
324 24 my ( $self, $c ) = @_;
325  
326 24 ▶ if ( $c->req->has_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->has_objects) { } 24 0 24
327 24 $self->validate_objects($c);
328 19 ▶ $self->transact_objects( $c, sub { $self->save_objects( $c, @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
329 }
330 else {
331 0 $c->log->error($_);
332 0 $self->push_error( $c,
333 { message => 'No objects on which to operate' } );
334 0 $c->detach();
335 }
336 }
337  
338  
339 23 ▶sub transact_objects {
Subroutine transact_objects: called CC=1 SCAR=0.0
Pod documented
340 23 my ( $self, $c, $coderef ) = @_;
341  
342 23 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
343 23 $self->stored_result_source->schema->txn_do( $coderef,
344 $c->req->objects );
345 }
346 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
347 3 $c->log->error($_);
348 3 $self->push_error( $c,
349 { message => 'a database error has occured.' } );
350 3 $c->detach();
351 }
352 23 }
353  
354  
355 24 ▶sub validate_objects {
Subroutine validate_objects: called CC=1 SCAR=0.0
Pod documented
356 24 my ( $self, $c ) = @_;
357  
358 24 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
359 24 foreach my $obj ( $c->req->all_objects ) {
360 32 $obj->[1] = $self->validate_object( $c, $obj );
361 }
362 }
363 5 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
364 5 my $err = $_;
365 5 $c->log->error($err);
366 5 $err =~ s/\s+at\s+.+\n$//g;
367 5 $self->push_error( $c, { message => $err } );
368 5 $c->detach();
369 }
370 24 }
371  
372  
373 32 ▶sub validate_object {
Subroutine validate_object: called CC=29 SCAR=40.7
Pod documented
374 32 my ( $self, $c, $obj ) = @_;
375 32 my ( $object, $params ) = @$obj;
376  
377 32 my %values;
378 15 my %requires_map = map { $_ => 1 } @{
379 32 ( $object->in_storage )
380 ? []
381 32 ▶ : $c->stash->{create_requires} || $self->create_requires
Branch: 1 branch
BranchTrueFalseTotal
$object->in_storage ? : 19 13 32
Condition66%2/3
$c->stash->{"create_requires"} or $self->create_requires
l!l&&r!l&&!r
1120
MC/DC0%0/2
$c->stash->{"create_requires"} or $self->create_requires
$c->stash->{"create_requires"} $self->create_requires
Truth table66%2/3
A or B
A= $c->stash->{"create_requires"}B= $self->create_requires
ABresult
1X1
011
000
382 };
383  
384 82 ▶ my %allows_map = map { ( ref $_ ) ? %{$_} : ( $_ => 1 ) } (
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 17 65 82
385 keys %requires_map,
386 32 @{ ( $object->in_storage )
387 ? ( $c->stash->{update_allows} || $self->update_allows )
388 32 ▶ : ( $c->stash->{create_allows} || $self->create_allows )
Branch: 1 branch
BranchTrueFalseTotal
$object->in_storage ? : 19 13 32
Condition50%3/6
$c->stash->{"update_allows"} or $self->update_allows
l!l&&r!l&&!r
2170
$c->stash->{"create_allows"} or $self->create_allows
l!l&&r!l&&!r
0130
MC/DC0%0/2
$c->stash->{"update_allows"} or $self->update_allows
$c->stash->{"update_allows"} $self->update_allows
MC/DC0%0/2
$c->stash->{"create_allows"} or $self->create_allows
$c->stash->{"create_allows"} $self->create_allows
Truth table66%2/3
A or B
A= $c->stash->{"update_allows"}B= $self->update_allows
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $c->stash->{"create_allows"}B= $self->create_allows
ABresult
1X1
011
000
389 }
390 );
391  
392 32 foreach my $key ( keys %allows_map ) {
393  
394 # check value defined if key required
395 76 my $allowed_fields = $allows_map{$key};
396  
397 76 ▶ if ( ref $allowed_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $allowed_fields) { } 17 59 76
398 17 my $related_source = $object->result_source->related_source($key);
399 17 my $related_params = $params->{$key};
400 17 my %allowed_related_map = map { $_ => 1 } @$allowed_fields;
401 my $allowed_related_cols =
402 17 ▶ ( $allowed_related_map{'*'} )
Branch: 1 branch
BranchTrueFalseTotal
$allowed_related_map{'*'} ? : 17 0 17
403 ? [ $related_source->columns ]
404 : $allowed_fields;
405  
406 17 ▶ if (ref($related_params) && reftype($related_params) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $related_params and builtin::reftype($related_params) eq 'ARRAY') { } 0 17 17
Condition66%2/3
ref $related_params and builtin::reftype($related_params) eq "ARRAY"
!ll&&!rl&&r
1520
MC/DC0%0/2
ref $related_params and builtin::reftype($related_params) eq "ARRAY"
ref $related_params builtin::reftype($related_params) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $related_paramsB= builtin::reftype($related_params) eq "ARRAY"
ABresult
0X0
100
111
407 0 my @related_data;
408 0 for my $related_param (@$related_params) {
409 0 my %data;
410 0 foreach my $related_col ( @{$allowed_related_cols} ) {
411 0 ▶ if (defined(
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $related_col_value = $related_param->{$related_col})) 0 0 0
412 my $related_col_value =
413 $related_param->{$related_col}
414 )
415 ) {
416 0 $data{$related_col} = $related_col_value;
417 }
418 }
419 0 push @related_data, \%data;
420 }
421 0 $values{$key} = \@related_data;
422 }
423 else {
424 17 foreach my $related_col ( @{$allowed_related_cols} ) {
425 68 ▶ if (defined(
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $related_col_value = $related_params->{$related_col})) 2 66 68
426 my $related_col_value =
427 $related_params->{$related_col}
428 )
429 ) {
430 2 $values{$key}{$related_col} = $related_col_value;
431 }
432 }
433 }
434 }
435 else {
436 59 my $value = $params->{$key};
437  
438 59 ▶ if ( $requires_map{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires_map{$key}) 15 44 59
439 15 ▶ unless ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 4 11 15
440  
441 # if not defined look for default
442 $value = $object->result_source->column_info($key)
443 4 ->{default_value};
444 4 ▶ unless ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 2 4
445 2 die "No value supplied for ${key} and no default";
446 }
447 }
448 }
449  
450 # check for multiple values
451 57 ▶ if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and not builtin::reftype($value) eq "SCALAR") 0 57 57
Condition33%1/3
ref $value and not builtin::reftype($value) eq "SCALAR"
!ll&&!rl&&r
5700
MC/DC0%0/2
ref $value and not builtin::reftype($value) eq "SCALAR"
ref $value not builtin::reftype($value) eq "SCALAR"
Truth table33%1/3
A and not(B)
A= ref $valueB= not builtin::reftype($value) eq "SCALAR"
ABresult
0X0
100
111
452 # PostgreSQL supports arrays
453 0 ▶ unless (ref($value) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY" and $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL" and $object->result_source->column_info($key)->{"data_type"} =~ /\[\]$/) 0 0 0
Condition0%0/6
ref $value eq "ARRAY" and $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL" and $object->result_source->column_info($key)->{"data_type"} =~ /\[\]$/
!ll&&!rl&&r
000
ref $value eq "ARRAY" and $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL"
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value eq "ARRAY" and $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL" and $object->result_source->column_info($key)->{"data_type"} =~ /\[\]$/
ref $value eq "ARRAY" $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL" $object->result_source->column_info($key)->{"data_type"} =~ /\[\]$/
Truth table0%0/4
A and B and C
A= ref $value eq "ARRAY"B= $self->stored_result_source->schema->storage->sqlt_type eq "PostgreSQL"C= $object->result_source->column_info($key)->{"data_type"} =~ /\[\]$/
ABCresult
0XX0
10X0
1100
1111
454 && $self->stored_result_source->schema->storage->sqlt_type eq 'PostgreSQL'
455 && $object->result_source->column_info($key)->{data_type} =~ /\[\]$/) {
456 0 require Data::Dumper;
457 0 die
458 0 "Multiple values for '${key}': ${\Data::Dumper::Dumper($value)}";
459 }
460 }
461  
462 # check exists so we don't just end up with hash of undefs
463 # check defined to account for default values being used
464 $values{$key} = $value
465 57 ▶ if exists $params->{$key} || defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{$key} or defined $value 34 23 57
Condition100%3/3
exists $params->{$key} or defined $value
l!l&&r!l&&!r
32223
MC/DC100%2/2
exists $params->{$key} or defined $value
exists $params->{$key} defined $value
Truth table100%3/3
A or B
A= exists $params->{$key}B= defined $value
ABresult
1X1
011
000
466 }
467 }
468  
469 30 ▶ unless ( keys %values || !$object->in_storage ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %values or not $object->in_storage) 3 27 30
Condition66%2/3
keys %values or not $object->in_storage
l!l&&r!l&&!r
2703
MC/DC50%1/2
keys %values or not $object->in_storage
keys %values not $object->in_storage
Truth table66%2/3
A or not(B)
A= keys %valuesB= not $object->in_storage
ABresult
1X1
011
000
470 3 die 'No valid keys passed';
471 }
472  
473 27 return \%values;
474 }
475  
476  
477 4 ▶sub delete {
Subroutine delete: called CC=2 SCAR=8.1
Pod documented
478 4 my ( $self, $c ) = @_;
479  
480 4 ▶ if ( $c->req->has_objects ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->has_objects) { } 4 0 4
481 $self->transact_objects( $c,
482 4 ▶ sub { $self->delete_objects( $c, @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
483 4 $c->req->clear_objects;
484 }
485 else {
486 0 $c->log->error($_);
487 0 $self->push_error( $c,
488 { message => 'No objects on which to operate' } );
489 0 $c->detach();
490 }
491 }
492  
493  
494 19 ▶sub save_objects {
Subroutine save_objects: called CC=2 SCAR=6.9
Pod documented
495 19 my ( $self, $c, $objects ) = @_;
496  
497 19 foreach my $obj (@$objects) {
498 25 $self->save_object( $c, $obj );
499 }
500 }
501  
502  
503 25 ▶sub save_object {
Subroutine save_object: called CC=2 SCAR=6.9
Pod documented
504 25 my ( $self, $c, $obj ) = @_;
505  
506 25 my ( $object, $params ) = @$obj;
507  
508 25 ▶ if ( $object->in_storage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->in_storage) { } 14 11 25
509 14 $self->update_object_from_params( $c, $object, $params );
510 }
511 else {
512 11 $self->insert_object_from_params( $c, $object, $params );
513 }
514  
515 }
516  
517  
518 14 ▶sub update_object_from_params {
Subroutine update_object_from_params: called CC=1 SCAR=0.0
Pod documented
519 14 my ( $self, $c, $object, $params ) = @_;
520  
521 14 $params = {%$params, %{$object->ident_condition}};
522  
523 14 my $updated_object =
524 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
525 resultset => $c->req->current_result_set,
526 # unknown_params_ok => 1,
527 updates => $params,
528 );
529  
530 # replace request object with updated one for response
531 13 my $vals = $c->req->get_object(0)->[1];
532 13 $c->req->clear_objects;
533 13 $c->req->add_object( [ $updated_object, $vals ] );
534 }
535  
536  
537 11 ▶sub insert_object_from_params {
Subroutine insert_object_from_params: called CC=8 SCAR=27.7
Pod documented
538  
539 #my ($self, $c, $object, $params) = @_;
540 11 my ( $self, undef, $object, $params ) = @_;
541  
542 11 my %rels;
543 11 ▶ while ( my ( $key, $value ) = each %{$params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 17 11 28
544 17 ▶ if ( ref($value) && !( reftype($value) eq reftype(JSON::MaybeXS::true) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value and not builtin::reftype($value) eq 'SCALAR') { } 0 17 17
elsif ($object->can($key)) { } 17 0 17
Condition33%1/3
ref $value and not builtin::reftype($value) eq "SCALAR"
!ll&&!rl&&r
1700
MC/DC0%0/2
ref $value and not builtin::reftype($value) eq "SCALAR"
ref $value not builtin::reftype($value) eq "SCALAR"
Truth table33%1/3
A and not(B)
A= ref $valueB= not builtin::reftype($value) eq "SCALAR"
ABresult
0X0
100
111
545 0 $rels{$key} = $value;
546 }
547  
548 # accessor = colname
549 elsif ( $object->can($key) ) {
550 17 $object->$key($value);
551 }
552  
553 # accessor != colname
554 else {
555 my $accessor =
556 0 $object->result_source->column_info($key)->{accessor};
557 0 $object->$accessor($value);
558 }
559 }
560  
561 11 $object->insert;
562  
563 9 ▶ while ( my ( $k, $v ) = each %rels ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %rels) 0 9 9
564 0 ▶ if (reftype($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($v) eq 'ARRAY') { } 0 0 0
565 0 foreach my $next_v ( @$v ) {
566 0 $object->create_related($k, $next_v);
567 }
568 }
569 else {
570 0 $object->create_related($k => $v);
571 }
572 }
573 }
574  
575  
576 4 ▶sub delete_objects {
Subroutine delete_objects: called CC=1 SCAR=0.0
Pod documented
577 4 my ( $self, $c, $objects ) = @_;
578  
579 4 map { $self->delete_object( $c, $_->[0] ) } @$objects;
580 }
581  
582  
583 8 ▶sub delete_object {
Subroutine delete_object: called CC=1 SCAR=0.0
Pod documented
584  
585 #my ($self, $c, $object) = @_;
586 8 my ( $self, undef, $object ) = @_;
587  
588 8 $object->delete;
589 }
590  
591  
592 106 ▶sub end : Private {
Subroutine end: called CC=13 SCAR=25.7
Pod documented
593 106 my ( $self, $c ) = @_;
594  
595 # don't change the http status code if already set elsewhere
596 106 ▶ unless ( $c->res->status && $c->res->status != 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->res->status and $c->res->status != 200) 105 1 106
Condition66%2/3
$c->res->status and $c->res->status != 200
!ll&&!rl&&r
01051
MC/DC50%1/2
$c->res->status and $c->res->status != 200
$c->res->status $c->res->status != 200
Truth table66%2/3
A and B
A= $c->res->statusB= $c->res->status != 200
ABresult
0X0
100
111
597 105 ▶ if ( $self->has_errors($c) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_errors($c)) { } 33 72 105
598 33 $c->res->status(400);
599 }
600 else {
601 72 $c->res->status(200);
602 }
603 }
604  
605 106 ▶ if ( $c->res->status == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->res->status == 200) { } 72 34 106
606 $c->stash->{ $self->stash_key }->{success} =
607 72 ▶ $self->use_json_boolean ? JSON::MaybeXS::true : 'true';
Branch: 1 branch
BranchTrueFalseTotal
$self->use_json_boolean ? : 16 56 72
608 72 ▶ if ( $self->return_object
Branch: 1 branch
BranchTrueFalseTotal
if ($self->return_object and $c->req->has_objects and not exists $c->stash->{$self->stash_key}{$self->data_root}) 6 66 72
Condition83%5/6
$self->return_object and $c->req->has_objects and not exists $c->stash->{$self->stash_key}{$self->data_root}
!ll&&!rl&&r
6606
$self->return_object and $c->req->has_objects
!ll&&!rl&&r
6426
MC/DC66%2/3
$self->return_object and $c->req->has_objects and not exists $c->stash->{$self->stash_key}{$self->data_root}
$self->return_object $c->req->has_objects not exists $c->stash->{$self->stash_key}{$self->data_root}
Truth table75%3/4
A and B and not(C)
A= $self->return_objectB= $c->req->has_objectsC= not exists $c->stash->{$self->stash_key}{$self->data_root}
ABCresult
0XX0
10X0
1100
1111
609 && $c->req->has_objects
610 && ! exists $c->stash->{ $self->stash_key }->{ $self->data_root } ) {
611 6 my $returned_objects = [];
612 push( @$returned_objects, $self->each_object_inflate( $c, $_ ) )
613 6 for map { $_->[0] } $c->req->all_objects;
614 6 ▶ $c->stash->{ $self->stash_key }->{ $self->data_root } =
Branch: 1 branch
BranchTrueFalseTotal
scalar @$returned_objects > 1 ? : 1 5 6
615 scalar(@$returned_objects) > 1
616 ? $returned_objects
617 : $returned_objects->[0];
618 }
619 }
620 else {
621 $c->stash->{ $self->stash_key }->{success} =
622 34 ▶ $self->use_json_boolean ? JSON::MaybeXS::false : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$self->use_json_boolean ? : 7 27 34
623 34 ▶ $c->stash->{ $self->stash_key }->{messages} = $self->get_errors($c)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_errors($c) 33 1 34
624 if $self->has_errors($c);
625  
626 # don't return data for error responses
627 34 delete $c->stash->{ $self->stash_key }->{ $self->data_root };
628 }
629  
630 106 $c->forward('serialize');
631 16 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
632  
633  
634 10 ▶sub each_object_inflate {
Subroutine each_object_inflate: called CC=1 SCAR=0.0
Pod documented
635  
636 #my ($self, $c, $object) = @_;
637 10 my ( $self, undef, $object ) = @_;
638  
639 10 return { $object->get_columns };
640 }
641  
642  
643 # from Catalyst::Action::Serialize
644 16 ▶sub serialize : ActionClass('Serialize') { }
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
645  
646  
647 33 ▶sub push_error {
Subroutine push_error: called CC=3 SCAR=11.1
Pod documented
648 33 my ( $self, $c, $params ) = @_;
649 33 ▶ die 'Catalyst app object missing'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 33 33
650 unless defined $c;
651 33 my $error = 'unknown error';
652 33 ▶ if ( exists $params->{message} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{"message"}) 33 0 33
653 33 $error = $params->{message};
654  
655 # remove newline from die "error message\n" which is required to not
656 # have the filename and line number in the error text
657 33 $error =~ s/\n$//;
658 }
659 33 push( @{ $c->stash->{_dbic_crud_errors} }, $error );
660 }
661  
662  
663 33 ▶sub get_errors {
Subroutine get_errors: called CC=2 SCAR=7.1
Pod documented
664 33 my ( $self, $c ) = @_;
665 33 ▶ die 'Catalyst app object missing'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 33 33
666 unless defined $c;
667 33 return $c->stash->{_dbic_crud_errors};
668 }
669  
670  
671 139 ▶sub has_errors {
Subroutine has_errors: called CC=2 SCAR=7.1
Pod documented
672 139 my ( $self, $c ) = @_;
673 139 ▶ die 'Catalyst app object missing'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 139 139
674 unless defined $c;
675 139 return exists $c->stash->{_dbic_crud_errors};
676 }
677  
678  
679 16 1;
680  
681 __END__
« API/RequestArguments.pm ↑ Summary API/StoredResultSource.pm »