| 1 | package Froody::Invoker::Implementation; | ||||||||||||||||||||||||||||
| 2 | 24 | ▶ | use base qw(Froody::Invoker); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 24 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use Froody::Response::Terse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 24 | ▶ | use Froody::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 24 | ▶ | use Froody::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 24 | ▶ | use Params::Validate qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 24 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 24 | ▶ | use List::MoreUtils qw(any); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 24 | ▶ | use Froody::Upload; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 24 | my $logger = Froody::Logger->get_logger('froody.invoker.implementation'); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 24 | ▶ | use constant response_class => 'Froody::Response::Terse'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 32 | ▶ | sub get_invoke_function { | ||||||||||||||||||||||||||
|
Subroutine
get_invoke_function: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 32 | my ($self, $module, $method) = @_; | |||||||||||||||||||||||||||
| 23 | 32 | return $module->can($method->name); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 32 | ▶ | sub invoke { | ||||||||||||||||||||||||||
|
Subroutine
invoke: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 32 | my ($self, $method, $params, $metadata) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # load the module if we need to | ||||||||||||||||||||||||||||
| 30 | 32 | my $module = $self->module($method); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # get the perl code we're actually going to call | ||||||||||||||||||||||||||||
| 33 | 32 | ▶ | my $func = $self->get_invoke_function($module, $method) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | $logger->logdie("no such method: ".$method->name." in $module"); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # create the context object, and return the instance that you | ||||||||||||||||||||||||||||
| 37 | # can call the other methods on. By default, this simply returns | ||||||||||||||||||||||||||||
| 38 | # the current object (i.e. $invocation is the same as $self) | ||||||||||||||||||||||||||||
| 39 | 32 | my $invocation = $self->create_context($method->full_name, $params); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 32 | my $response; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 32 | eval { | |||||||||||||||||||||||||||
| 44 | # run the gauntlet | ||||||||||||||||||||||||||||
| 45 | # munge the arguments | ||||||||||||||||||||||||||||
| 46 | 32 | $invocation->pre_process($method, $params); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # call the perl code | ||||||||||||||||||||||||||||
| 49 | 27 | my $data = $invocation->$func($params, $metadata); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # convert return shape into the reponse | ||||||||||||||||||||||||||||
| 52 | 26 | $response = $invocation->post_process($method, $data, $metadata); | |||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 32 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 6 | $response = $invocation->error_handler($method, $@, $metadata); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # store extra stuff in the response (e.g. cookies) | ||||||||||||||||||||||||||||
| 60 | 32 | $invocation->store_context($response); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 32 | return $response; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | ############ | ||||||||||||||||||||||||||||
| 66 | # these are all helper methods for this particular implmentation | ||||||||||||||||||||||||||||
| 67 | # that are either called directly or indirectly from invoke | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 6 | ▶ | sub error_handler { | ||||||||||||||||||||||||||
|
Subroutine
error_handler: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 6 | my ($self, $method, $error, $metadata) = @_; | |||||||||||||||||||||||||||
| 71 | # TODO - error_class should be an implementation / invoker method, not | ||||||||||||||||||||||||||||
| 72 | # on the dispatcher!! | ||||||||||||||||||||||||||||
| 73 | 6 | my $error_class = $metadata->{dispatcher}->error_class; | |||||||||||||||||||||||||||
| 74 | 6 | return $error_class->from_exception( $error, $metadata->{repository} ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 31 | ▶ | sub create_context { | ||||||||||||||||||||||||||
|
Subroutine
create_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 31 | my ($self, $params) = @_; | |||||||||||||||||||||||||||
| 79 | 31 | return $self; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 32 | ▶ | sub store_context { | ||||||||||||||||||||||||||
|
Subroutine
store_context: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | return | ||||||||||||||||||||||||||||
| 84 | 32 | } | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 32 | ▶ | sub pre_process { | ||||||||||||||||||||||||||
|
Subroutine
pre_process: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 32 | my ($self, $method, $params) = @_; | |||||||||||||||||||||||||||
| 88 | 32 | my $spec = $method->arguments; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # you can't send 'undef' across HTTP, so all the param validators assume | ||||||||||||||||||||||||||||
| 91 | # that undef isn't a valid value for a param. This seems reasonable. For | ||||||||||||||||||||||||||||
| 92 | # maximum DWIM, remove undef vaules, so that the implementation thinks that | ||||||||||||||||||||||||||||
| 93 | # they weren't passed. | ||||||||||||||||||||||||||||
| 94 | 32 | for (keys %$params) { | |||||||||||||||||||||||||||
| 95 | 23 | ▶ | delete $params->{$_} unless defined($params->{$_}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # special case for remainder; | ||||||||||||||||||||||||||||
| 99 | 32 | my ($remainder) = grep { $spec->{$_}{type}[0] eq 'remaining' } keys %$spec; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 32 | my @errors; | |||||||||||||||||||||||||||
| 102 | 32 | our $argname; | |||||||||||||||||||||||||||
| 103 | 14 | ▶ | my $check = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 104 | 14 | my ($test, $message) = @_; | |||||||||||||||||||||||||||
| 105 | 14 | ▶ | return 1 if $test; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 5 | push @errors, { name => $argname, -text => $message}; | |||||||||||||||||||||||||||
| 107 | 5 | return; | |||||||||||||||||||||||||||
| 108 | 32 | }; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # it'll always be a hashref. | ||||||||||||||||||||||||||||
| 111 | 32 | ▶ | $params->{$remainder} = {} if $remainder; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 32 | for $argname (grep { !exists $spec->{$_} } keys %$params) { | |||||||||||||||||||||||||||
| 113 | 2 | ▶ | if ($remainder) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 2 | $params->{$remainder}{$argname} = delete $params->{$argname}; | |||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | 0 | $check->(!$remainder, "Unexpected argument."); | |||||||||||||||||||||||||||
| 117 | 0 | delete $params->{$argname}; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 32 | require Froody::Argument; | |||||||||||||||||||||||||||
| 122 | 32 | for $argname (keys %$spec) { | |||||||||||||||||||||||||||
| 123 | 30 | my $param = $params->{$argname}; | |||||||||||||||||||||||||||
| 124 | 30 | ▶ | if (!defined($param)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 8 | $check->( $spec->{$argname}{optional}, "Missing argument." ); | |||||||||||||||||||||||||||
| 126 | 8 | next; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 22 | for my $type (@{$spec->{$argname}{type}}) { | |||||||||||||||||||||||||||
| 129 | 28 | ▶ | next if $type eq 'remaining'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | # XXX: make the type plugin declare this | ||||||||||||||||||||||||||||
| 131 | 26 | ▶ | if (ref($param) eq 'ARRAY' && any { $type eq $_ } qw(text number)) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
ref $param eq "ARRAY" and &any(sub {
$type eq $_;
}
, "text", "number")
MC/DC100%2/2
ref $param eq "ARRAY" and &any(sub {
$type eq $_;
}
, "text", "number")
ref $param eq "ARRAY"
&any(sub {
$type eq $_;
}
, "text", "number")
Truth table100%3/3
A and B
A= ref $param eq "ARRAY"B= &any(sub {
$type eq $_;
}
, "text", "number")
| |||||||||||||||||||||||||||||
| 132 | 3 | for (0..$#{$param}) { | |||||||||||||||||||||||||||
| 133 | 9 | $param->[$_] = Froody::Argument->process($type, $param->[$_], $check); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 23 | $param = Froody::Argument->process($type, $param, $check); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 22 | $params->{$argname} = $param; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 32 | ▶ | if (@errors) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 5 | my $errdata = { error => [ @errors ] }; | |||||||||||||||||||||||||||
| 145 | 5 | Froody::Error->throw("perl.methodcall.param", | |||||||||||||||||||||||||||
| 146 | "Error validating incoming parameters", | ||||||||||||||||||||||||||||
| 147 | $errdata); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 27 | return; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 26 | ▶ | sub post_process { | ||||||||||||||||||||||||||
|
Subroutine
post_process: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | 26 | my ($self, $method, $data) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 26 | ▶ | $logger->logconfess("called with old style post_process") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | unless UNIVERSAL::isa($method,'Froody::Method'); | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # have a response already? Don't bother doing anything | ||||||||||||||||||||||||||||
| 160 | 26 | ▶ | return $data if blessed($data) && $data->isa("Froody::Response"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($data) and $data->isa("Froody::Response")
MC/DC50%1/2
builtin::blessed($data) and $data->isa("Froody::Response")
builtin::blessed($data)
$data->isa("Froody::Response")
Truth table66%2/3
A and B
A= builtin::blessed($data)B= $data->isa("Froody::Response")
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # build a response | ||||||||||||||||||||||||||||
| 163 | 24 | my $response = $self->response_class->new; | |||||||||||||||||||||||||||
| 164 | 24 | $response->content($data); | |||||||||||||||||||||||||||
| 165 | 24 | $response->structure($method); | |||||||||||||||||||||||||||
| 166 | 24 | return $response; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 31 | ▶ | sub module { | ||||||||||||||||||||||||||
|
Subroutine
module: called CC=3 SCAR=16.0
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 31 | my ($self, $method) = @_; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 31 | ▶ | unless ( $self->can($method->name) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my $module = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 175 | 0 | Froody::Error->throw("perl.use", "module $module cannot '" . $method->name . "'"); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 31 | return $self; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 24 | 1; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | __DATA__ | ||||||||||||||||||||||||||||