PONAPI/DAO/Request/Role/UpdateLike.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 90.0% 9 / 10 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 85.7% 60 / 70 CC 8 SCAR 11.6
CC
8
Cov
89%
_verify_update_response
6.1
_get_resource_for_meta
2.3
_add_success_meta
2.0
CRAP 3.2
« PONAPI/Utils/Names.pm ↑ Summary PONAPI/DAO/Request/Create.pm »
1 # ABSTRACT: DAO request role - `update` & like related functionality
2 package PONAPI::DAO::Request::Role::UpdateLike;
3  
4 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use PONAPI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use PONAPI::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 has respond_to_updates_with_200 => (
10 is => 'ro',
11 isa => 'Bool',
12 );
13  
14 has update_nothing_status => (
15 is => 'ro',
16 isa => 'Int',
17 # http://jsonapi.org/format/#crud-updating-relationship-responses-204
18 # Basically, we return a 204, UNLESS it's an ->update() call, in which
19 # case it has to return a 404:
20 # http://jsonapi.org/format/#crud-updating-responses-404
21 50 ▶ default => sub { 204 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 8 );
23  
24 sub _verify_update_response {
25 29 ▶ my ( $self, $ret, @extra ) = @_;
Subroutine _verify_update_response: called CC=6 SCAR=18.1
26  
27 PONAPI::Exception->throw(
28 internal => 1,
29 message => "update-like operation returned an unexpected value"
30 29 ▶ ) unless exists $PONAPI_UPDATE_RETURN_VALUES{$ret};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PONAPI::DAO::Request::Role::UpdateLike::PONAPI_UPDATE_RETURN_VALUES{$ret} 4 25 29
31  
32 25 my $doc = $self->document;
33 25 ▶ return if $doc->has_errors or $doc->has_status;
Branch: 1 branch
BranchTrueFalseTotal
if $doc->has_errors or $doc->has_status 0 25 25
Condition33%1/3
$doc->has_errors or $doc->has_status
l!l&&r!l&&!r
0025
MC/DC0%0/2
$doc->has_errors or $doc->has_status
$doc->has_errors $doc->has_status
Truth table33%1/3
A or B
A= $doc->has_errorsB= $doc->has_status
ABresult
1X1
011
000
34  
35 25 ▶ if ( $self->respond_to_updates_with_200 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->respond_to_updates_with_200) { } 2 23 25
36 2 $doc->set_status(200);
37 2 ▶ $self->repository->retrieve(
Branch: 1 branch
BranchTrueFalseTotal
if $ret == 100 1 1 2
38 type => $self->type,
39 id => $self->id,
40 document => $doc,
41 ) if $ret == PONAPI_UPDATED_EXTENDED;
42 2 return 1;
43 }
44 else {
45 23 $doc->set_status(202);
46 }
47  
48 23 return 1;
49 }
50  
51 sub _add_success_meta {
52 25 ▶ my ($self, $return_status) = @_;
Subroutine _add_success_meta: called CC=2 SCAR=6.9
53  
54 25 my $resource = $self->_get_resource_for_meta;
55  
56 25 my $detail = "successfully modified $resource";
57 25 ▶ if ( $return_status == PONAPI_UPDATED_NOTHING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return_status == 102) 5 20 25
58 5 my $status = $self->update_nothing_status;
59 5 $self->document->set_status($status);
60 # This should NEVER show up in a response, because it would
61 # mean a 204 with a body; I'm still leaving it here because
62 # a 204+body is preferable to a server error due to an empty
63 # document.
64 5 $detail = "modified nothing for $resource"
65 }
66  
67 25 $self->document->add_meta( detail => $detail );
68 }
69  
70 sub _get_resource_for_meta {
71 25 ▶ my $self = shift;
Subroutine _get_resource_for_meta: called CC=2 SCAR=8.4
72  
73 my $link = $self->document->req_path
74 25 ▶ // ( join "/" => grep defined, '', @{$self}{qw/type id rel_type/} );
Condition33%1/3
$self->document->req_path // join("/", grep(defined($_), "", @{$self;}{"type", "id", "rel_type"}))
l!l&&r!l&&!r
2500
MC/DC0%0/2
$self->document->req_path // join("/", grep(defined($_), "", @{$self;}{"type", "id", "rel_type"}))
$self->document->req_path join("/", grep(defined($_), "", @{$self;}{"type", "id", "rel_type"}))
Truth table33%1/3
A // B
A= $self->document->req_pathB= join("/", grep(defined($_), "", @{$self;}{"type", "id", "rel_type"}))
ABresult
1X1
011
000
75  
76 25 return $link . " => " . $self->json->encode( $self->data );
77 }
78  
79 8 ▶no PONAPI::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 8 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 __END__
83  
84 =pod
85  
86 =encoding UTF-8
87  
88 =head1 NAME
89  
90 PONAPI::DAO::Request::Role::UpdateLike - DAO request role - `update` & like related functionality
91  
92 =head1 VERSION
93  
94 version 0.002012
95  
96 =head1 AUTHORS
97  
98 =over 4
99  
100 =item *
101  
102 Mickey Nasriachi <mickey@cpan.org>
103  
104 =item *
105  
106 Stevan Little <stevan@cpan.org>
107  
108 =item *
109  
110 Brian Fraser <hugmeir@cpan.org>
111  
112 =back
113  
114 =head1 COPYRIGHT AND LICENSE
115  
116 This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
117  
118 This is free software; you can redistribute it and/or modify it under
119 the same terms as the Perl 5 programming language system itself.
120  
121 =cut
« PONAPI/Utils/Names.pm ↑ Summary PONAPI/DAO/Request/Create.pm »