| 1 | # ABSTRACT: DAO request class | ||||||||||||||||||||||||||||
| 2 | package PONAPI::DAO::Request; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use PONAPI::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 8 | has repository => ( | |||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||
| 11 | does => 'PONAPI::Repository', | ||||||||||||||||||||||||||||
| 12 | required => 1, | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | has document => ( | |||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||
| 17 | isa => 'PONAPI::Document', | ||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | has type => ( | |||||||||||||||||||||||||||
| 22 | is => 'ro', | ||||||||||||||||||||||||||||
| 23 | isa => 'Str', | ||||||||||||||||||||||||||||
| 24 | required => 1, | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has send_doc_self_link => ( | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 30 | 207 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 8 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | has is_valid => ( | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 36 | 241 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 8 | writer => '_set_is_valid', | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has json => ( | ||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | isa => JSON::MaybeXS::JSON(), | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | default => sub { JSON::MaybeXS->new->allow_nonref->utf8->canonical }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 8 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 243 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 243 | my $class = shift; | |||||||||||||||||||||||||||
| 48 | 243 | ▶ | my %args = @_ == 1 ? %{ $_[0] } : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | die "[__PACKAGE__] missing arg `version`" | ||||||||||||||||||||||||||||
| 51 | 243 | ▶ | unless defined $args{version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | $args{document} = PONAPI::Document->new( | ||||||||||||||||||||||||||||
| 54 | version => $args{version}, | ||||||||||||||||||||||||||||
| 55 | req_path => $args{req_path} // '/', | ||||||||||||||||||||||||||||
| 56 | 243 | ▶ | req_base => $args{req_base} // '/', | ||||||||||||||||||||||||||
|
Condition100%4/4
$args{"req_path"} // "/"
$args{"req_base"} // "/"
MC/DC100%1/1
$args{"req_path"} // "/"
$args{"req_path"}
MC/DC100%1/1
$args{"req_base"} // "/"
$args{"req_base"}
Truth table100%2/2
A
A= $args{"req_path"}
Truth table100%2/2
A
A= $args{"req_base"}
| |||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 243 | return \%args; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # These validation methods will be overwritten in the appropriate roles. | ||||||||||||||||||||||||||||
| 63 | # They cover the case where an attribute is NOT expected. | ||||||||||||||||||||||||||||
| 64 | 56 | ▶ | sub _validate_id { | ||||||||||||||||||||||||||
|
Subroutine
_validate_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 56 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 66 | 56 | ▶ | return unless defined $args->{id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 3 | $self->_bad_request( "`id` is not allowed for this request" ) | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 152 | ▶ | sub _validate_rel_type { | ||||||||||||||||||||||||||
|
Subroutine
_validate_rel_type: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 71 | 152 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 72 | 152 | ▶ | return unless defined $args->{rel_type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 11 | $self->_bad_request( "`relationship type` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 128 | ▶ | sub _validate_include { | ||||||||||||||||||||||||||
|
Subroutine
_validate_include: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 128 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 78 | 128 | ▶ | return unless defined $args->{include}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 7 | $self->_bad_request( "`include` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 128 | ▶ | sub _validate_fields { | ||||||||||||||||||||||||||
|
Subroutine
_validate_fields: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 128 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 84 | 128 | ▶ | return unless defined $args->{fields}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 7 | $self->_bad_request( "`fields` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 113 | ▶ | sub _validate_filter { | ||||||||||||||||||||||||||
|
Subroutine
_validate_filter: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 89 | 113 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 90 | 113 | ▶ | return unless defined $args->{filter}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->_bad_request( "`filter` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 113 | ▶ | sub _validate_sort { | ||||||||||||||||||||||||||
|
Subroutine
_validate_sort: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 95 | 113 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 96 | 113 | ▶ | return unless defined $args->{sort}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 6 | $self->_bad_request( "`sort` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 113 | ▶ | sub _validate_page { | ||||||||||||||||||||||||||
|
Subroutine
_validate_page: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 101 | 113 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 102 | 113 | ▶ | return unless defined $args->{page}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 6 | $self->_bad_request( "`page` is not allowed for this request" ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 235 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 235 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # `type` exists | ||||||||||||||||||||||||||||
| 110 | 235 | my $type = $self->type; | |||||||||||||||||||||||||||
| 111 | 235 | ▶ | return $self->_bad_request( "Type `$type` doesn't exist.", 404 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | unless $self->repository->has_type( $type ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 225 | $self->_validate_id($args); | |||||||||||||||||||||||||||
| 115 | 225 | $self->_validate_rel_type($args); | |||||||||||||||||||||||||||
| 116 | 225 | $self->_validate_include($args); | |||||||||||||||||||||||||||
| 117 | 225 | $self->_validate_fields($args); | |||||||||||||||||||||||||||
| 118 | 225 | $self->_validate_filter($args); | |||||||||||||||||||||||||||
| 119 | 225 | $self->_validate_sort($args); | |||||||||||||||||||||||||||
| 120 | 225 | $self->_validate_page($args); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # validate `data` | ||||||||||||||||||||||||||||
| 123 | 225 | ▶ | if ( exists $args->{data} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 124 | 97 | ▶ | if ( $self->can('data') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 87 | $self->_validate_data; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | else { | ||||||||||||||||||||||||||||
| 128 | 10 | $self->_bad_request( "request body is not allowed" ); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | elsif ( $self->can('has_data') ) { | ||||||||||||||||||||||||||||
| 132 | 7 | $self->_bad_request( "request body is missing `data`" ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 196 | ▶ | sub response { | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 196 | my ( $self, @headers ) = @_; | |||||||||||||||||||||||||||
| 138 | 196 | my $doc = $self->document; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 196 | ▶ | $doc->add_self_link | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->send_doc_self_link and not $doc->has_link("self")
MC/DC50%1/2
$self->send_doc_self_link and not $doc->has_link("self")
$self->send_doc_self_link
not $doc->has_link("self")
Truth table66%2/3
A and not(B)
A= $self->send_doc_self_linkB= not $doc->has_link("self")
| |||||||||||||||||||||||||||||
| 141 | if $self->send_doc_self_link && !$doc->has_link('self'); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | return ( | ||||||||||||||||||||||||||||
| 144 | 196 | ▶ | $doc->status, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | \@headers, | ||||||||||||||||||||||||||||
| 146 | ( | ||||||||||||||||||||||||||||
| 147 | $doc->status != 204 | ||||||||||||||||||||||||||||
| 148 | ? $doc->build | ||||||||||||||||||||||||||||
| 149 | : () | ||||||||||||||||||||||||||||
| 150 | ), | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 105 | ▶ | sub _bad_request { | ||||||||||||||||||||||||||
|
Subroutine
_bad_request: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 105 | my ( $self, $detail, $status ) = @_; | |||||||||||||||||||||||||||
| 156 | 105 | ▶ | $self->document->raise_error( $status||400, { detail => $detail } ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$status || 400
MC/DC100%1/1
$status || 400
$status
Truth table100%2/2
A
A= $status
| |||||||||||||||||||||||||||||
| 157 | 105 | $self->_set_is_valid(0); | |||||||||||||||||||||||||||
| 158 | 105 | return; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 8 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 162 | 8 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||