blib/lib/Froody/Invoker/Implementation.pm

filter: Statementstmt 96.4% 110 / 114 Branchbran 84.6% 22 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 23 / 23 Podpod 100% 8 / 8 totaltotal 91.9% 171 / 186 CC 22 SCAR 17.7
CC
22
Cov
92%
pre_process
14.0
module
4.9
post_process
4.0
CRAP 5.9
« blib/lib/Froody/Request.pm ↑ Summary blib/lib/Froody/Argument/Multipart.pm »
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
BranchTrueFalseTotal
my $func = $self->get_invoke_function($module, $method) or $logger->logdie('no such method: ' . $method->name . " in $module") 32 0 32
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
BranchTrueFalseTotal
if ($@) 6 26 32
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
BranchTrueFalseTotal
unless defined $params->{$_} 1 22 23
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
BranchTrueFalseTotal
if $test 9 5 14
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
BranchTrueFalseTotal
if $remainder 2 30 32
112 32 for $argname (grep { !exists $spec->{$_} } keys %$params) {
113 2 ▶ if ($remainder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder) { } 2 0 2
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
BranchTrueFalseTotal
unless (defined $param) 8 22 30
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
BranchTrueFalseTotal
if $type eq "remaining" 2 26 28
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
BranchTrueFalseTotal
if (ref $param eq 'ARRAY' and &any(sub { $type eq $_; } , 'text', 'number')) { } 3 23 26
Condition100%3/3
ref $param eq "ARRAY" and &any(sub { $type eq $_; } , "text", "number")
!ll&&!rl&&r
2123
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (@errors) 5 27 32
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
BranchTrueFalseTotal
unless UNIVERSAL::isa($method, "Froody::Method") 0 26 26
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
BranchTrueFalseTotal
if builtin::blessed($data) and $data->isa("Froody::Response") 2 24 26
Condition66%2/3
builtin::blessed($data) and $data->isa("Froody::Response")
!ll&&!rl&&r
2402
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")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($self->can($method->name)) 0 31 31
174 0 ▶ my $module = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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__
« blib/lib/Froody/Request.pm ↑ Summary blib/lib/Froody/Argument/Multipart.pm »