DBIC/Resource/JSONAPI/Role/SetWritable.pm

filter: Statementstmt 37.2% 22 / 59 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 4 / 8 Podpod 0.0% 0 / 2 totaltotal 27.3% 26 / 95 CC 15 SCAR 44.9
CC
15
Cov
27%
_create_embedded_resources_from_jsonapi
156.0
__ANON__
20.0
from_jsonapi_json
2.0
CRAP 89.1
« DBIC/Resource/ActiveModel/Role/ItemWritable.pm ↑ Summary DBIC/Resource/HAL/Role/ItemWritable.pm »
1 package WebAPI::DBIC::Resource::JSONAPI::Role::SetWritable;
2 25 $WebAPI::DBIC::Resource::JSONAPI::Role::SetWritable::VERSION = '0.004002';
3  
4 25 ▶use Devel::Dwarn;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 25 requires '_build_content_types_accepted';
11 25 requires 'render_item_into_body';
12 25 requires 'decode_json';
13 25 requires 'set';
14 25 requires 'prefetch';
15  
16  
17 11 ▶around '_build_content_types_accepted' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 11 my $orig = shift;
19 11 my $self = shift;
20 11 my $types = $self->$orig();
21 11 unshift @$types, { 'application/vnd.api+json' => 'from_jsonapi_json' };
22 11 return $types;
23 25 };
24  
25  
26 0 ▶sub from_jsonapi_json {
Subroutine from_jsonapi_json: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my $self = shift;
28 0 my $item = $self->create_resources_from_jsonapi( $self->decode_json($self->request->content) );
29 0 return $self->item($item);
30 }
31  
32  
33 0 ▶sub create_resources_from_jsonapi { # XXX unify with create_resource in SetWritable, like ItemWritable?
Subroutine create_resources_from_jsonapi: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my ($self, $jsonapi) = @_;
35 0 my $item;
36  
37 0 my $schema = $self->set->result_source->schema;
38 # XXX perhaps the transaction wrapper belongs higher in the stack
39 # but it has to be below the auth layer which switches schemas
40 0 ▶ $schema->txn_do(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
41  
42 0 $item = $self->_create_embedded_resources_from_jsonapi($jsonapi, $self->set->result_class);
43  
44 # resync with what's (now) in the db to pick up defaulted fields etc
45 0 $item->discard_changes();
46  
47 # called here because create_path() is too late for Web::Machine
48 # and we need it to happen inside the transaction for rollback=1 to work
49 $self->render_item_into_body(item => $item, prefetch => $self->prefetch)
50 0 ▶ if grep {defined $_->{self}} @{$self->prefetch||[]};
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_->{"self"};} @{$self->prefetch or [];} 0 0 0
Condition0%0/2
$self->prefetch || []
l!l
00
MC/DC0%0/1
$self->prefetch || []
$self->prefetch
Truth table0%0/2
A
A= $self->prefetch
Aresult
11
00
51  
52 0 ▶ $schema->txn_rollback if $self->param('rollback'); # XXX
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("rollback") 0 0 0
53 0 });
54  
55 0 return $item;
56 }
57  
58  
59 # recurse to create resources in $jsonapi->{_embedded}
60 # and update coresponding attributes in $jsonapi
61 # then create $jsonapi itself
62 0 ▶sub _create_embedded_resources_from_jsonapi {
Subroutine _create_embedded_resources_from_jsonapi: not called CC=12 SCAR=50.5
63 0 my ($self, $jsonapi, $result_class) = @_;
64  
65 0 my $links = delete $jsonapi->{_links};
66 0 my $meta = delete $jsonapi->{_meta};
67 0 ▶ my $embedded = delete $jsonapi->{_embedded} || {};
Condition0%0/2
delete $jsonapi->{'_embedded'} || {}
l!l
00
MC/DC0%0/1
delete $jsonapi->{'_embedded'} || {}
delete $jsonapi->{'_embedded'}
Truth table0%0/2
A
A= delete $jsonapi->{'_embedded'}
Aresult
11
00
68  
69 0 for my $rel (keys %$embedded) {
70  
71 0 ▶ my $rel_info = $result_class->relationship_info($rel)
Branch: 1 branch
BranchTrueFalseTotal
my $rel_info = $result_class->relationship_info($rel) or die "$result_class doesn't have a '${rel}' relation\n" 0 0 0
72 or die "$result_class doesn't have a '$rel' relation\n";
73 die "$result_class _embedded $rel isn't a 'single' relationship\n"
74 0 ▶ if $rel_info->{attrs}{accessor} ne 'single';
Branch: 1 branch
BranchTrueFalseTotal
if $rel_info->{"attrs"}{"accessor"} ne "single" 0 0 0
75  
76 0 my $rel_jsonapi = $embedded->{$rel};
77 0 ▶ die "_embedded $rel data is not a hash\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $rel_jsonapi ne "HASH" 0 0 0
78 if ref $rel_jsonapi ne 'HASH';
79  
80 # work out what keys to copy from the subitem we're about to create
81 0 my %fk_map;
82 0 my $cond = $rel_info->{cond};
83 0 for my $sub_field (keys %$cond) {
84 0 my $our_field = $cond->{$sub_field};
85 0 ▶ $our_field =~ s/^self\.//x or confess "panic $rel $our_field";
Branch: 1 branch
BranchTrueFalseTotal
$our_field =~ s/^self\.//x or confess("panic $rel $our_field") 0 0 0
86 0 ▶ $sub_field =~ s/^foreign\.//x or confess "panic $rel $sub_field";
Branch: 1 branch
BranchTrueFalseTotal
$sub_field =~ s/^foreign\.//x or confess("panic $rel $sub_field") 0 0 0
87 0 $fk_map{$our_field} = $sub_field;
88  
89 die "$result_class already contains a value for '$our_field'\n"
90 0 ▶ if defined $jsonapi->{$our_field}; # null is ok
Branch: 1 branch
BranchTrueFalseTotal
if defined $jsonapi->{$our_field} 0 0 0
91 }
92  
93 # create this subitem (and any resources embedded in it)
94 0 my $subitem = $self->_create_embedded_resources_from_jsonapi($rel_jsonapi, $rel_info->{source});
95  
96 # copy the keys of the subitem up to the item we're about to create
97 0 warn "$result_class $rel: propagating keys: @{[ %fk_map ]}\n"
98 0 ▶ if $ENV{WEBAPI_DBIC_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"WEBAPI_DBIC_DEBUG"} 0 0 0
99 0 ▶ while ( my ($ourfield, $subfield) = each %fk_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %fk_map) 0 0 0
100 0 $jsonapi->{$ourfield} = $subitem->$subfield();
101 }
102 }
103  
104 0 return $self->set->result_source->schema->resultset($result_class)->create($jsonapi);
105 }
106  
107 25 1;
108  
109 __END__
« DBIC/Resource/ActiveModel/Role/ItemWritable.pm ↑ Summary DBIC/Resource/HAL/Role/ItemWritable.pm »