ExtDirect/EventProvider.pm

filter: Statementstmt 100% 82 / 82 Branchbran 80.0% 8 / 10 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 88.6% 109 / 123 CC 11 SCAR 9.3
CC
11
Cov
94%
poll
5.0
new
3.2
_get_poll_requests
3.0
CRAP 2.5
« ExtDirect/Demo/Profile.pm ↑ Summary ExtDirect/Serializer.pm »
1 package RPC::ExtDirect::EventProvider;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use RPC::ExtDirect::Util::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use RPC::ExtDirect::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use RPC::ExtDirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use RPC::ExtDirect::NoEvents;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ### PACKAGE GLOBAL VARIABLE ###
13 #
14 # Turn this on for debugging.
15 #
16 # DEPRECATED. Use `debug_eventprovider` Config option instead.
17 # See RPC::ExtDirect::Config.
18 #
19  
20 4 our $DEBUG;
21  
22 ### PACKAGE GLOBAL VARIABLE ###
23 #
24 # Set Serializer class name so it could be configured
25 #
26 # DEPRECATED. Use `serializer_class_eventprovider` or `serializer_class`
27 # Config options instead.
28 #
29  
30 4 our $SERIALIZER_CLASS;
31  
32 ### PACKAGE GLOBAL VARIABLE ###
33 #
34 # DEPRECATED. This option did nothing in previous versions of
35 # RPC::ExtDirect library, and is ignored in 3.x+
36 #
37  
38 4 our $EVENT_CLASS;
39  
40 ### PACKAGE GLOBAL VARIABLE ###
41 #
42 # Set Request class name so it could be configured
43 #
44 # DEPRECATED. Use `request_class_eventprovider` Config option instead.
45 #
46  
47 4 our $REQUEST_CLASS;
48  
49 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
50 #
51 # Create a new EventProvider object with default API and Config
52 #
53  
54 sub new {
55 9 ▶ my ($class, %arg) = @_;
Subroutine new: called CC=3 SCAR=11.8
Pod undocumented
56
57 9 ▶ $arg{config} ||= RPC::ExtDirect::Config->new();
Condition66%2/3
$arg{"config"} ||= "RPC::ExtDirect::Config"->new
l!l&&r!l&&!r
540
MC/DC0%0/2
$arg{"config"} ||= "RPC::ExtDirect::Config"->new
$arg{"config"} "RPC::ExtDirect::Config"->new
Truth table66%2/3
A ||= B
A= $arg{"config"}B= "RPC::ExtDirect::Config"->new
ABresult
1X1
011
000
58 9 ▶ $arg{api} ||= RPC::ExtDirect->get_api();
Condition33%1/3
$arg{"api"} ||= "RPC::ExtDirect"->get_api
l!l&&r!l&&!r
090
MC/DC0%0/2
$arg{"api"} ||= "RPC::ExtDirect"->get_api
$arg{"api"} "RPC::ExtDirect"->get_api
Truth table33%1/3
A ||= B
A= $arg{"api"}B= "RPC::ExtDirect"->get_api
ABresult
1X1
011
000
59
60 9 return bless { %arg }, $class;
61 }
62  
63 ### PUBLIC CLASS/INSTANCE METHOD ###
64 #
65 # Run all poll handlers in succession, collect the Events returned
66 # by them and return serialized representation suitable for passing
67 # on to client side.
68 #
69 # Note that the preferred way to call this method is on the EventProvider
70 # object instance, but we support the class-based way for backwards
71 # compatibility.
72 #
73 # Be aware that the only supported way to configure the EventProvider
74 # is to pass a Config object to the constructor and then call poll()
75 # on the instance.
76 #
77  
78 sub poll {
79 9 ▶ my ($class, $env) = @_;
Subroutine poll: called CC=5 SCAR=16.2
Pod undocumented
80
81 9 ▶ my $self = ref($class) ? $class : $class->new();
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 5 4 9
82
83 9 my @poll_requests = $self->_get_poll_requests();
84  
85 # Even if we have nothing to poll, we must return a stub Event
86 # or client side will throw an unhandled JavaScript exception
87 9 ▶ return $self->_no_events unless @poll_requests;
Branch: 1 branch
BranchTrueFalseTotal
unless @poll_requests 0 9 9
88  
89 # Run all the requests and collect their results
90 9 my @results = $self->_run_requests($env, \@poll_requests);
91  
92 # No events returned by the handlers? We still gotta return something.
93 9 ▶ return $self->_no_events unless @results;
Branch: 1 branch
BranchTrueFalseTotal
unless @results 4 5 9
94  
95 # Polling results are always JSON; no content type needed
96 5 my $serialized = $self->_serialize_results(@results);
97  
98 # And if serialization fails we have to return something positive
99 5 ▶ return $serialized || $self->_no_events;
Condition66%2/3
$serialized || $self->_no_events
l!l&&r!l&&!r
410
MC/DC0%0/2
$serialized || $self->_no_events
$serialized $self->_no_events
Truth table66%2/3
A || B
A= $serializedB= $self->_no_events
ABresult
1X1
011
000
100 }
101  
102 ### PUBLIC INSTANCE METHODS ###
103 #
104 # Simple read-write accessors
105 #
106  
107 4 RPC::ExtDirect::Util::Accessor::mk_accessors(
108 simple => [qw/ api config /],
109 );
110  
111 ############## PRIVATE METHODS BELOW ##############
112  
113 ### PRIVATE INSTANCE METHOD ###
114 #
115 # Return a list of Request::PollHandler objects
116 #
117  
118 sub _get_poll_requests {
119 9 ▶ my ($self) = @_;
Subroutine _get_poll_requests: called CC=3 SCAR=11.0
120  
121 # Compile the list of poll handler Methods
122 9 my @handlers = $self->api->get_poll_handlers();
123  
124 # Now create the corresponding Request objects
125 9 my @poll_requests;
126 9 for my $handler ( @handlers ) {
127 9 my $req = $self->_create_request($handler);
128  
129 9 ▶ push @poll_requests, $req if $req;
Branch: 1 branch
BranchTrueFalseTotal
if $req 9 0 9
130 };
131
132 9 return @poll_requests;
133 }
134  
135 ### PRIVATE INSTANCE METHOD ###
136 #
137 # Create Request off a poll handler
138 #
139  
140 sub _create_request {
141 9 ▶ my ($self, $handler) = @_;
Subroutine _create_request: called CC=1 SCAR=0.0
142
143 9 my $config = $self->config;
144 9 my $api = $self->api;
145 9 my $action_name = $handler->action;
146 9 my $method_name = $handler->name;
147
148 9 my $request_class = $config->request_class_eventprovider;
149
150 9 eval "require $request_class";
151
152 9 my $req = $request_class->new({
153 config => $config,
154 api => $api,
155 action => $action_name,
156 method => $method_name,
157 });
158
159 9 return $req;
160 }
161  
162 ### PRIVATE INSTANCE METHOD ###
163 #
164 # Run poll requests and collect results
165 #
166  
167 sub _run_requests {
168 9 ▶ my ($self, $env, $requests) = @_;
Subroutine _run_requests: called CC=2 SCAR=6.9
169
170 # Run the requests
171 9 $_->run($env) for @$requests;
172  
173 # Collect responses
174 9 my @results = map { $_->result } @$requests;
175
176 9 return @results;
177 }
178  
179 ### PRIVATE CLASS METHOD ###
180 #
181 # Serialize results
182 #
183  
184 sub _serialize_results {
185 5 ▶ my ($self, @results) = @_;
Subroutine _serialize_results: called CC=2 SCAR=6.9
186
187 # Fortunately, client side does understand more than on event
188 # batched as array
189 5 ▶ my $final_result = @results > 1 ? [ @results ]
Branch: 1 branch
BranchTrueFalseTotal
@results > 1 ? : 3 2 5
190 : $results[0]
191 ;
192
193 5 my $config = $self->config;
194 5 my $api = $self->api;
195 5 my $debug = $config->debug_eventprovider;
196
197 5 my $serializer_class = $config->serializer_class_eventprovider;
198
199 5 eval "require $serializer_class";
200
201 5 my $serializer = $serializer_class->new(
202 config => $config,
203 api => $api,
204 );
205  
206 5 my $json = eval {
207 5 $serializer->serialize(
208 mute_exceptions => 1,
209 debug => $debug,
210 data => [$final_result],
211 )
212 };
213  
214 5 return $json;
215 }
216  
217 ### PRIVATE CLASS METHOD ###
218 #
219 # Serializes and returns a NoEvents object.
220 #
221  
222 sub _no_events {
223 5 ▶ my ($self) = @_;
Subroutine _no_events: called CC=1 SCAR=0.0
224
225 5 my $config = $self->config;
226 5 my $api = $self->api;
227 5 my $debug = $config->debug_eventprovider;
228
229 5 my $serializer_class = $config->serializer_class_eventprovider;
230
231 5 eval "require $serializer_class";
232
233 5 my $serializer = $serializer_class->new(
234 config => $config,
235 api => $api,
236 );
237  
238 5 my $no_events = RPC::ExtDirect::NoEvents->new();
239 5 my $result = $no_events->result();
240
241 # NoEvents result can't blow up, hence no eval
242 5 my $serialized = $serializer->serialize(
243 mute_exceptions => !1,
244 debug => $debug,
245 data => [$result],
246 );
247  
248 5 return $serialized;
249 }
250  
251 4 1;
« ExtDirect/Demo/Profile.pm ↑ Summary ExtDirect/Serializer.pm »