DBIC/Resource/JSONAPI/Role/DBIC.pm

filter: Statementstmt 77.2% 95 / 123 Branchbran 45.0% 27 / 60 Conditioncond 50.0% 9 / 18 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 6 totaltotal 62.3% 144 / 231 CC 41 SCAR 31.8
CC
41
Cov
65%
_jsonapi_page_links
90.0
_render_prefetch_jsonapi
42.0
render_jsonapi_response
11.5
CRAP 24.1
« DBIC/Resource/Role/DBICAuth.pm ↑ Summary DBIC/Resource/Role/Relationship.pm »
1 package WebAPI::DBIC::Resource::JSONAPI::Role::DBIC;
2 25 $WebAPI::DBIC::Resource::JSONAPI::Role::DBIC::VERSION = '0.004002';
3  
4 25 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use Devel::Dwarn;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 25 requires 'get_url_for_item_relationship';
12 25 requires 'render_item_as_plain_hash';
13 25 requires 'path_for_item';
14 25 requires 'add_params_to_url';
15 25 requires 'prefetch';
16 25 requires 'type_namer';
17  
18  
19  
20 34 ▶sub jsonapi_type {
Subroutine jsonapi_type: called CC=1 SCAR=0.0
Pod undocumented
21 34 my ($self) = @_;
22 34 return $self->type_namer->type_name_for_resultset($self->set);
23 }
24  
25  
26 15 ▶sub top_link_for_relname { # XXX cacheable
Subroutine top_link_for_relname: called CC=3 SCAR=11.1
Pod undocumented
27 15 my ($self, $relname) = @_;
28  
29 15 my $link_url_templated = $self->get_url_template_for_set_relationship($self->set, $relname);
30 15 ▶ return if not defined $link_url_templated;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $link_url_templated 0 15 15
31  
32 # XXX a hack to keep the template urls readable!
33 15 $link_url_templated =~ s/%7B/{/g;
34 15 $link_url_templated =~ s/%7D/}/g;
35  
36 15 my $rel_info = $self->set->result_class->relationship_info($relname);
37 15 ▶ my $result_class = $rel_info->{class}||die "panic";
Condition50%1/2
$rel_info->{'class'} || die('panic')
l!l
150
MC/DC0%0/1
$rel_info->{'class'} || die('panic')
$rel_info->{'class'}
Truth table50%1/2
A
A= $rel_info->{'class'}
Aresult
11
00
38  
39 15 my $rel_jsonapi_type = $self->type_namer->type_name_for_result_class($result_class);
40  
41 15 my $path = $self->jsonapi_type .".". $relname;
42 15 return $path => {
43 href => "$link_url_templated", # XXX stringify the URL object
44 type => $rel_jsonapi_type,
45 };
46 }
47  
48  
49 17 ▶sub render_jsonapi_prefetch_rel {
Subroutine render_jsonapi_prefetch_rel: called CC=6 SCAR=18.1
Pod undocumented
50 17 my ($self, $set, $relname, $sub_rel, $top_links, $compound_links, $item_edit_rel_hooks) = @_;
51  
52 17 my $rel_info = $set->result_class->relationship_info($relname);
53 17 ▶ my $result_class = $rel_info->{class}||die "panic";
Condition50%1/2
$rel_info->{'class'} || die('panic')
l!l
170
MC/DC0%0/1
$rel_info->{'class'} || die('panic')
$rel_info->{'class'}
Truth table50%1/2
A
A= $rel_info->{'class'}
Aresult
11
00
54  
55 17 my @idcolumns = $result_class->unique_constraint_columns('primary'); # XXX wrong
56 17 ▶ if (@idcolumns > 1) { # eg many-to-many that doesn't have a separate id
Branch: 1 branch
BranchTrueFalseTotal
if (@idcolumns > 1) 2 15 17
57 warn "Result class $result_class has multiple keys (@idcolumns) so relations like $relname won't have links generated.\n"
58 2 ▶ unless our $warn_once->{"$result_class $relname"}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $our $warn_once{"$result_class $relname"}++ 1 1 2
59 2 return;
60 }
61  
62 15 ▶ my ($top_link_key, $top_link_value) = $self->top_link_for_relname($relname)
Branch: 1 branch
BranchTrueFalseTotal
() = $self->top_link_for_relname($relname) or return 15 0 15
63 or return;
64 15 $top_links->{$top_link_key} = $top_link_value;
65  
66 15 my $rel_typename = $self->type_namer->type_name_for_result_class($rel_info->{class});
67  
68 die "panic: item_edit_rel_hooks for $relname already defined"
69 15 ▶ if $item_edit_rel_hooks->{$relname};
Branch: 1 branch
BranchTrueFalseTotal
if $item_edit_rel_hooks->{$relname} 0 15 15
70 22 ▶ $item_edit_rel_hooks->{$relname} = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
71 22 my ($jsonapi_obj, $row) = @_;
72  
73 22 my $subitem = $row->$relname();
74  
75 22 ▶ my $compound_links_for_rel = $compound_links->{$rel_typename} ||= {};
Condition100%2/2
$compound_links->{$rel_typename} ||= {}
l!l
715
MC/DC100%1/1
$compound_links->{$rel_typename} ||= {}
$compound_links->{$rel_typename}
Truth table100%2/2
A
A= $compound_links->{$rel_typename}
Aresult
11
00
76  
77 22 my $link_keys;
78 22 ▶ if (not defined $subitem) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $subitem) { } 0 22 22
elsif ($subitem->isa('DBIx::Class::ResultSet')) { } 5 17 22
elsif ($subitem->isa('DBIx::Class::Row')) { } 17 0 17
79 0 $link_keys = undef;
80 }
81 elsif ($subitem->isa('DBIx::Class::ResultSet')) { # one-to-many rel
82 5 $link_keys = [];
83 5 ▶ while (my $subrow = $subitem->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subrow = $subitem->next) 9 5 14
84 9 my $id = $subrow->id;
85 9 push @$link_keys, $id;
86 9 $compound_links_for_rel->{$id} = $self->render_item_as_jsonapi_hash($subrow); # XXX typename
87 }
88 }
89 elsif ($subitem->isa('DBIx::Class::Row')) { # one-to-many rel
90 17 $link_keys = $subitem->id;
91 17 $compound_links_for_rel->{$subitem->id} = $self->render_item_as_jsonapi_hash($subitem); # XXX typename
92 }
93 else {
94 0 die "panic: don't know how to handle $row $relname value $subitem";
95 }
96  
97 22 $jsonapi_obj->{links}{$rel_typename} = $link_keys;
98 }
99 15 }
100  
101  
102 19 ▶sub render_jsonapi_response { # return top-level document hashref
Subroutine render_jsonapi_response: called CC=11 SCAR=24.4
Pod undocumented
103 19 my ($self) = @_;
104  
105 19 my $set = $self->set;
106  
107 19 my $top_links = {};
108 19 my $compound_links = {};
109 19 my $item_edit_rel_hooks = {};
110  
111 19 ▶ for my $prefetch (@{$self->prefetch||[]}) {
Condition50%1/2
$self->prefetch || []
l!l
190
MC/DC0%0/1
$self->prefetch || []
$self->prefetch
Truth table50%1/2
A
A= $self->prefetch
Aresult
11
00
112 #warn "prefetch $prefetch";
113 17 ▶ next if $self->param('distinct');
Branch: 1 branch
BranchTrueFalseTotal
if $self->param("distinct") 0 17 17
114  
115 17 ▶ while (my ($relname, $sub_rel) = each %{$prefetch}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$prefetch;}) 17 17 34
116 #warn "prefetch $prefetch - $relname, $sub_rel";
117 17 $self->render_jsonapi_prefetch_rel($set, $relname, $sub_rel, $top_links, $compound_links, $item_edit_rel_hooks);
118 }
119 }
120  
121 31 ▶ my $set_data = $self->render_set_as_array_of_jsonapi_resource_objects($set, undef, sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
122 31 my ($jsonapi_obj, $row) = @_;
123 31 $_->($jsonapi_obj, $row) for values %$item_edit_rel_hooks;
124 19 });
125  
126 # construct top document to return
127 19 ▶ my $top_set_key = ($self->param('distinct')) ? 'data' : $self->jsonapi_type;
Branch: 1 branch
BranchTrueFalseTotal
$self->param('distinct') ? : 0 19 19
128 19 my $top_doc = { # http://jsonapi.org/format/#document-structure-top-level
129 $top_set_key => $set_data,
130 };
131  
132 19 ▶ if (keys %$top_links) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$top_links) 11 8 19
133 11 $top_doc->{links} = $top_links
134 }
135  
136 19 ▶ if (keys %$compound_links) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$compound_links) 11 8 19
137 #Dwarn $compound_links;
138 11 ▶ while ( my ($k, $v) = each %$compound_links) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$compound_links) 15 11 26
139 # sort just for test stability,
140 15 $top_doc->{linked}{$k} = [ @{$v}{ sort keys %$v } ];
141 }
142 }
143  
144 19 my $total_items;
145 19 ▶ if (($self->param('with')||'') =~ /count/) { # XXX
Branch: 1 branch
BranchTrueFalseTotal
if (($self->param("with") || "") =~ /count/) 0 19 19
Condition50%1/2
$self->param('with') || ''
l!l
019
MC/DC0%0/1
$self->param('with') || ''
$self->param('with')
Truth table50%1/2
A
A= $self->param('with')
Aresult
11
00
146 0 $total_items = $set->pager->total_entries;
147 0 $top_doc->{meta}{count} = $total_items; # XXX detail not in spec
148 }
149  
150 19 return $top_doc;
151 }
152  
153  
154  
155 57 ▶sub render_item_as_jsonapi_hash {
Subroutine render_item_as_jsonapi_hash: called CC=2 SCAR=7.0
Pod undocumented
156 57 my ($self, $item) = @_;
157  
158 57 my $data = $self->render_item_as_plain_hash($item);
159  
160 57 ▶ $data->{id} //= $item->id;
Condition66%2/3
$data->{"id"} //= $item->id
l!l&&r!l&&!r
2550
MC/DC0%0/2
$data->{"id"} //= $item->id
$data->{"id"} $item->id
Truth table66%2/3
A //= B
A= $data->{"id"}B= $item->id
ABresult
1X1
011
000
161 57 $data->{type} = $self->type_namer->type_name_for_result_class($item->result_source->result_class);
162 57 $data->{href} = $self->path_for_item($item);
163  
164 #$self->_render_prefetch_jsonapi($item, $data, $_) for @{$self->prefetch||[]};
165  
166 # add links for relationships
167  
168 57 return $data;
169 }
170  
171  
172 0 ▶sub _render_prefetch_jsonapi {
Subroutine _render_prefetch_jsonapi: not called CC=6 SCAR=37.4
173 0 my ($self, $item, $data, $prefetch) = @_;
174  
175 0 ▶ while (my ($rel, $sub_rel) = each %{$prefetch}){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$prefetch;}) 0 0 0
176 0 ▶ next if $rel eq 'self';
Branch: 1 branch
BranchTrueFalseTotal
if $rel eq "self" 0 0 0
177  
178 0 my $subitem = $item->$rel();
179  
180 0 ▶ if (not defined $subitem) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $subitem) { } 0 0 0
elsif ($subitem->isa('DBIx::Class::ResultSet')) { } 0 0 0
181 0 $data->{_embedded}{$rel} = undef; # show an explicit null from a prefetch
182 }
183 elsif ($subitem->isa('DBIx::Class::ResultSet')) { # one-to-many rel
184 0 ▶ my $rel_set_resource = $self->web_machine_resource(
Branch: 1 branch
BranchTrueFalseTotal
ref $sub_rel eq 'ARRAY' ? : 0 0 0
185 set => $subitem,
186 item => undef,
187 prefetch => ref $sub_rel eq 'ARRAY' ? $sub_rel : [$sub_rel],
188 );
189 0 $data->{_embedded}{$rel} = $rel_set_resource->render_set_as_array_of_jsonapi_resource_objects($subitem, undef);
190 }
191 else {
192 0 $data->{_embedded}{$rel} = $self->render_item_as_plain_hash($subitem);
193 }
194 }
195 }
196  
197 19 ▶sub render_set_as_array_of_jsonapi_resource_objects {
Subroutine render_set_as_array_of_jsonapi_resource_objects: called CC=4 SCAR=14.0
Pod undocumented
198 19 my ($self, $set, $render_method, $edit_hook) = @_;
199 19 ▶ $render_method ||= 'render_item_as_jsonapi_hash';
Condition50%1/2
$render_method ||= "render_item_as_jsonapi_hash"
l!l
019
MC/DC0%0/1
$render_method ||= "render_item_as_jsonapi_hash"
$render_method
Truth table50%1/2
A
A= $render_method
Aresult
11
00
200  
201 19 my @jsonapi_objs;
202 19 ▶ while (my $row = $set->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $set->next) 31 19 50
203 31 push @jsonapi_objs, $self->$render_method($row);
204 31 ▶ $edit_hook->($jsonapi_objs[-1], $row) if $edit_hook;
Branch: 1 branch
BranchTrueFalseTotal
if $edit_hook 31 0 31
205 }
206  
207 19 return \@jsonapi_objs;
208 }
209  
210  
211  
212  
213 0 ▶sub _jsonapi_page_links {
Subroutine _jsonapi_page_links: not called CC=9 SCAR=45.0
214 0 my ($self, $set, $base, $page_items, $total_items) = @_;
215  
216 # XXX we ought to allow at least the self link when not pages
217 0 ▶ return () unless $set->is_paged;
Branch: 1 branch
BranchTrueFalseTotal
unless $set->is_paged 0 0 0
218  
219 # XXX we break encapsulation here, sadly, because calling
220 # $set->pager->current_page triggers a "select count(*)".
221 # XXX When we're using a later version of DBIx::Class we can use this:
222 # https://metacpan.org/source/RIBASUSHI/DBIx-Class-0.08208/lib/DBIx/Class/ResultSet/Pager.pm
223 # and do something like $rs->pager->total_entries(sub { 99999999 })
224 0 ▶ my $rows = $set->{attrs}{rows} or confess "panic: rows not set";
Branch: 1 branch
BranchTrueFalseTotal
my $rows = $set->{"attrs"}{"rows"} or confess("panic: rows not set") 0 0 0
225 0 ▶ my $page = $set->{attrs}{page} or confess "panic: page not set";
Branch: 1 branch
BranchTrueFalseTotal
my $page = $set->{"attrs"}{"page"} or confess("panic: page not set") 0 0 0
226  
227 # XXX this self link this should probably be subtractive, ie include all
228 # params by default except any known to cause problems
229 0 my $url = $self->add_params_to_url($base, { distinct=>1, with=>1, me=>1 }, { rows => $rows });
230 0 my $linkurl = $url->as_string;
231 0 $linkurl .= "&page="; # hack to optimize appending page 5 times below
232  
233 0 my @link_kvs;
234 0 push @link_kvs, self => {
235 href => $linkurl.($page),
236 title => $set->result_class,
237 };
238 0 ▶ push @link_kvs, next => { href => $linkurl.($page+1) }
Branch: 1 branch
BranchTrueFalseTotal
if $page_items == $rows 0 0 0
239 if $page_items == $rows;
240 0 ▶ push @link_kvs, prev => { href => $linkurl.($page-1) }
Branch: 1 branch
BranchTrueFalseTotal
if $page > 1 0 0 0
241 if $page > 1;
242 0 ▶ push @link_kvs, first => { href => $linkurl.1 }
Branch: 1 branch
BranchTrueFalseTotal
if $page > 1 0 0 0
243 if $page > 1;
244 0 ▶ push @link_kvs, last => { href => $linkurl.$set->pager->last_page }
Branch: 1 branch
BranchTrueFalseTotal
if $total_items and $page != $set->pager->last_page 0 0 0
Condition0%0/3
$total_items and $page != $set->pager->last_page
!ll&&!rl&&r
000
MC/DC0%0/2
$total_items and $page != $set->pager->last_page
$total_items $page != $set->pager->last_page
Truth table0%0/3
A and B
A= $total_itemsB= $page != $set->pager->last_page
ABresult
0X0
100
111
245 if $total_items and $page != $set->pager->last_page;
246  
247 0 return @link_kvs;
248 }
249  
250  
251 25 1;
252  
253 __END__
« DBIC/Resource/Role/DBICAuth.pm ↑ Summary DBIC/Resource/Role/Relationship.pm »