DBIC/Resource/HAL/Role/SetWritable.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 65.0% 13 / 20 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 87.3% 83 / 95 CC 15 SCAR 21.0
CC
15
Cov
89%
_create_embedded_resources_from_hal
13.1
__ANON__
4.0
create_resources_from_hal
1.0
CRAP 8.1
« DBIC/Resource/ActiveModel/Role/SetWritable.pm ↑ Summary DBIC/Resource/HAL/Role/DBIC.pm »
1 package WebAPI::DBIC::Resource::HAL::Role::SetWritable;
2 25 $WebAPI::DBIC::Resource::HAL::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/hal+json' => 'from_hal_json' };
22 11 return $types;
23 25 };
24  
25  
26 5 ▶sub from_hal_json {
Subroutine from_hal_json: called CC=1 SCAR=0.0
Pod undocumented
27 5 my $self = shift;
28 5 my $item = $self->create_resources_from_hal( $self->decode_json($self->request->content) );
29 5 return $self->item($item);
30 }
31  
32  
33 5 ▶sub create_resources_from_hal { # XXX unify with create_resource in SetWritable, like ItemWritable?
Subroutine create_resources_from_hal: called CC=1 SCAR=0.0
Pod undocumented
34 5 my ($self, $hal) = @_;
35 5 my $item;
36  
37 5 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 5 ▶ $schema->txn_do(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
41  
42 5 $item = $self->_create_embedded_resources_from_hal($hal, $self->set->result_class);
43  
44 # resync with what's (now) in the db to pick up defaulted fields etc
45 5 $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 5 ▶ if grep {defined $_->{self}} @{$self->prefetch||[]};
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_->{"self"};} @{$self->prefetch or [];} 3 2 5
Condition50%1/2
$self->prefetch || []
l!l
50
MC/DC0%0/1
$self->prefetch || []
$self->prefetch
Truth table50%1/2
A
A= $self->prefetch
Aresult
11
00
51  
52 5 ▶ $schema->txn_rollback if $self->param('rollback'); # XXX
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("rollback") 3 2 5
53 5 });
54  
55 5 return $item;
56 }
57  
58  
59 # recurse to create resources in $hal->{_embedded}
60 # and update coresponding attributes in $hal
61 # then create $hal itself
62 8 ▶sub _create_embedded_resources_from_hal {
Subroutine _create_embedded_resources_from_hal: called CC=12 SCAR=25.7
63 8 my ($self, $hal, $result_class) = @_;
64  
65 8 my $links = delete $hal->{_links};
66 8 my $meta = delete $hal->{_meta};
67 8 ▶ my $embedded = delete $hal->{_embedded} || {};
Condition100%2/2
delete $hal->{'_embedded'} || {}
l!l
35
MC/DC100%1/1
delete $hal->{'_embedded'} || {}
delete $hal->{'_embedded'}
Truth table100%2/2
A
A= delete $hal->{'_embedded'}
Aresult
11
00
68  
69 8 for my $rel (keys %$embedded) {
70  
71 3 ▶ 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" 3 0 3
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 3 ▶ if $rel_info->{attrs}{accessor} ne 'single';
Branch: 1 branch
BranchTrueFalseTotal
if $rel_info->{"attrs"}{"accessor"} ne "single" 0 3 3
75  
76 3 my $rel_hal = $embedded->{$rel};
77 3 ▶ die "_embedded $rel data is not a hash\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $rel_hal ne "HASH" 0 3 3
78 if ref $rel_hal ne 'HASH';
79  
80 # work out what keys to copy from the subitem we're about to create
81 3 my %fk_map;
82 3 my $cond = $rel_info->{cond};
83 3 for my $sub_field (keys %$cond) {
84 3 my $our_field = $cond->{$sub_field};
85 3 ▶ $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") 3 0 3
86 3 ▶ $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") 3 0 3
87 3 $fk_map{$our_field} = $sub_field;
88  
89 die "$result_class already contains a value for '$our_field'\n"
90 3 ▶ if defined $hal->{$our_field}; # null is ok
Branch: 1 branch
BranchTrueFalseTotal
if defined $hal->{$our_field} 0 3 3
91 }
92  
93 # create this subitem (and any resources embedded in it)
94 3 my $subitem = $self->_create_embedded_resources_from_hal($rel_hal, $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 3 ▶ if $ENV{WEBAPI_DBIC_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"WEBAPI_DBIC_DEBUG"} 0 3 3
99 3 ▶ while ( my ($ourfield, $subfield) = each %fk_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %fk_map) 3 3 6
100 3 $hal->{$ourfield} = $subitem->$subfield();
101 }
102 }
103  
104 8 return $self->set->result_source->schema->resultset($result_class)->create($hal);
105 }
106  
107 25 1;
108  
109 __END__
« DBIC/Resource/ActiveModel/Role/SetWritable.pm ↑ Summary DBIC/Resource/HAL/Role/DBIC.pm »