Total Complexity | 41 |
Total Lines | 397 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like RequestExpander often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RequestExpander, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class RequestExpander |
||
17 | { |
||
18 | /** |
||
19 | * Description of the OData request that a client has submitted. |
||
20 | * |
||
21 | * @var RequestDescription |
||
22 | */ |
||
23 | private $request; |
||
24 | |||
25 | /** |
||
26 | * Holds reference to the data service instance. |
||
27 | * |
||
28 | * @var IService |
||
29 | */ |
||
30 | private $service; |
||
31 | |||
32 | /** |
||
33 | * Holds reference to the wrapper over IDSMP and IDSQP implementation. |
||
34 | * |
||
35 | * @var ProvidersWrapper |
||
36 | */ |
||
37 | private $providers; |
||
38 | |||
39 | /** |
||
40 | * Holds reference to segment stack being processed. |
||
41 | * |
||
42 | * @var SegmentStack |
||
43 | */ |
||
44 | private $stack; |
||
45 | |||
46 | public function __construct(RequestDescription $request, IService $service, ProvidersWrapper $wrapper) |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Gets reference to the request submitted by client. |
||
56 | * |
||
57 | * @return RequestDescription |
||
58 | */ |
||
59 | public function getRequest() |
||
60 | { |
||
61 | return $this->request; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Gets reference to the request submitted by client. |
||
66 | * |
||
67 | * @return ProvidersWrapper |
||
68 | */ |
||
69 | public function getProviders() |
||
70 | { |
||
71 | return $this->providers; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Gets the data service instance. |
||
76 | * |
||
77 | * @return IService |
||
78 | */ |
||
79 | public function getService() |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Gets the segment stack instance. |
||
86 | * |
||
87 | * @return SegmentStack |
||
88 | */ |
||
89 | public function getStack() |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Perform expansion. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function handleExpansion() |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Execute queries for expansion. |
||
114 | * |
||
115 | * @param array|mixed $result Resource(s) whose navigation properties needs to be expanded |
||
116 | */ |
||
117 | private function executeExpansion($result) |
||
118 | { |
||
119 | if ($result instanceof QueryResult) { |
||
120 | $result = $result->results; |
||
121 | } |
||
122 | |||
123 | $originalIsArray = is_array($result); |
||
124 | |||
125 | if (!$originalIsArray) { |
||
126 | $result = [$result]; |
||
127 | } |
||
128 | |||
129 | $expandedProjectionNodes = $this->getExpandedProjectionNodes(); |
||
130 | foreach ($expandedProjectionNodes as $expandedProjectionNode) { |
||
131 | $resourceType = $expandedProjectionNode->getResourceType(); |
||
132 | $isCollection = ResourcePropertyKind::RESOURCESET_REFERENCE |
||
133 | == $expandedProjectionNode->getResourceProperty()->getKind(); |
||
134 | $expandedPropertyName = $expandedProjectionNode->getResourceProperty()->getName(); |
||
135 | |||
136 | foreach ($result as $entry) { |
||
137 | if ($isCollection) { |
||
138 | $result1 = $this->executeCollectionExpansionGetRelated($expandedProjectionNode, $entry); |
||
139 | if (!empty($result1)) { |
||
140 | $this->executeCollectionExpansionProcessExpansion( |
||
141 | $entry, |
||
142 | $result1, |
||
143 | $expandedProjectionNode, |
||
144 | $resourceType, |
||
145 | $expandedPropertyName |
||
146 | ); |
||
147 | } else { |
||
148 | $resultSet = $originalIsArray ? [] : $result1; |
||
149 | $resourceType->setPropertyValue($entry, $expandedPropertyName, $resultSet); |
||
150 | } |
||
151 | } else { |
||
152 | $this->executeSingleExpansionGetRelated( |
||
153 | $expandedProjectionNode, |
||
154 | $entry, |
||
155 | $resourceType, |
||
156 | $expandedPropertyName |
||
157 | ); |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Resource set wrapper for the resource being retrieved. |
||
165 | * |
||
166 | * @return ResourceSetWrapper |
||
167 | */ |
||
168 | private function getCurrentResourceSetWrapper() |
||
169 | { |
||
170 | $wraps = $this->getStack()->getSegmentWrappers(); |
||
171 | $count = count($wraps); |
||
172 | |||
173 | return 0 == $count ? $this->getRequest()->getTargetResourceSetWrapper() : $wraps[$count - 1]; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Pushes a segment for the root of the tree |
||
178 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
179 | * |
||
180 | * @return bool true if the segment was pushed, false otherwise |
||
181 | */ |
||
182 | private function pushSegmentForRoot() |
||
183 | { |
||
184 | $segmentName = $this->getRequest()->getContainerName(); |
||
185 | $segmentResourceSetWrapper = $this->getRequest()->getTargetResourceSetWrapper(); |
||
186 | |||
187 | return $this->pushSegment($segmentName, $segmentResourceSetWrapper); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Pushes a segment for the current navigation property being written out. |
||
192 | * Note: Refer 'ObjectModelSerializerNotes.txt' for more details about |
||
193 | * 'Segment Stack' and this method. |
||
194 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
195 | * |
||
196 | * @param ResourceProperty &$resourceProperty Current navigation property |
||
197 | * being written out |
||
198 | * |
||
199 | * @throws InvalidOperationException If this function invoked with non-navigation |
||
200 | * property instance |
||
201 | * |
||
202 | * @return bool true if a segment was pushed, false otherwise |
||
203 | */ |
||
204 | private function pushSegmentForNavigationProperty(ResourceProperty &$resourceProperty) |
||
205 | { |
||
206 | if ($resourceProperty->getTypeKind() == ResourceTypeKind::ENTITY()) { |
||
207 | assert(!empty($this->getStack()->getSegmentNames()), '!is_empty($this->getStack()->getSegmentNames())'); |
||
208 | $currentResourceSetWrapper = $this->getCurrentResourceSetWrapper(); |
||
209 | $currentResourceType = $currentResourceSetWrapper->getResourceType(); |
||
210 | $currentResourceSetWrapper = $this->getService() |
||
211 | ->getProvidersWrapper() |
||
212 | ->getResourceSetWrapperForNavigationProperty( |
||
213 | $currentResourceSetWrapper, |
||
214 | $currentResourceType, |
||
215 | $resourceProperty |
||
216 | ); |
||
217 | |||
218 | assert(null !== $currentResourceSetWrapper, '!null($currentResourceSetWrapper)'); |
||
219 | |||
220 | return $this->pushSegment( |
||
221 | $resourceProperty->getName(), |
||
222 | $currentResourceSetWrapper |
||
223 | ); |
||
224 | } else { |
||
225 | throw new InvalidOperationException( |
||
226 | 'pushSegmentForNavigationProperty should not be called with non-entity type' |
||
227 | ); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Gets collection of expanded projection nodes under the current node. |
||
233 | * |
||
234 | * @return ExpandedProjectionNode[] List of nodes describing expansions for the current segment |
||
235 | */ |
||
236 | protected function getExpandedProjectionNodes() |
||
237 | { |
||
238 | $expandedProjectionNode = $this->getCurrentExpandedProjectionNode(); |
||
239 | $expandedProjectionNodes = []; |
||
240 | if (null !== $expandedProjectionNode) { |
||
241 | foreach ($expandedProjectionNode->getChildNodes() as $node) { |
||
242 | if ($node instanceof ExpandedProjectionNode) { |
||
243 | $expandedProjectionNodes[] = $node; |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | return $expandedProjectionNodes; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Find a 'ExpandedProjectionNode' instance in the projection tree |
||
253 | * which describes the current segment. |
||
254 | * |
||
255 | * @return ExpandedProjectionNode|null |
||
256 | */ |
||
257 | private function getCurrentExpandedProjectionNode() |
||
258 | { |
||
259 | $expandedProjectionNode = $this->getRequest()->getRootProjectionNode(); |
||
260 | if (null !== $expandedProjectionNode) { |
||
261 | $names = $this->getStack()->getSegmentNames(); |
||
262 | $depth = count($names); |
||
263 | if (0 != $depth) { |
||
264 | for ($i = 1; $i < $depth; ++$i) { |
||
265 | $expandedProjectionNode = $expandedProjectionNode->findNode($names[$i]); |
||
|
|||
266 | assert(null !== $expandedProjectionNode, '!is_null($expandedProjectionNode)'); |
||
267 | assert( |
||
268 | $expandedProjectionNode instanceof ExpandedProjectionNode, |
||
269 | '$expandedProjectionNode instanceof ExpandedProjectionNode' |
||
270 | ); |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | return $expandedProjectionNode; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Pushes information about the segment whose instance is going to be |
||
280 | * retrieved from the IDSQP implementation |
||
281 | * Note: Calls to this method should be balanced with calls to popSegment. |
||
282 | * |
||
283 | * @param string $segmentName Name of segment to push |
||
284 | * @param ResourceSetWrapper &$resourceSetWrapper The resource set wrapper |
||
285 | * to push |
||
286 | * |
||
287 | * @return bool true if the segment was push, false otherwise |
||
288 | */ |
||
289 | private function pushSegment($segmentName, ResourceSetWrapper &$resourceSetWrapper) |
||
290 | { |
||
291 | return $this->getStack()->pushSegment($segmentName, $resourceSetWrapper); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Pops segment information from the 'Segment Stack' |
||
296 | * Note: Calls to this method should be balanced with previous calls |
||
297 | * to _pushSegment. |
||
298 | * |
||
299 | * @param bool $needPop Is a pop required. Only true if last push |
||
300 | * was successful |
||
301 | * |
||
302 | * @throws InvalidOperationException If found un-balanced call |
||
303 | * with _pushSegment |
||
304 | */ |
||
305 | private function popSegment($needPop) |
||
306 | { |
||
307 | $this->getStack()->popSegment($needPop); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @param ExpandedProjectionNode $expandedProjectionNode |
||
312 | * @param $entry |
||
313 | * |
||
314 | * @return object[]|null |
||
315 | */ |
||
316 | private function executeCollectionExpansionGetRelated($expandedProjectionNode, $entry) |
||
317 | { |
||
318 | $currentResourceSet = $this->getCurrentResourceSetWrapper()->getResourceSet(); |
||
319 | $resourceSetOfProjectedProperty = $expandedProjectionNode |
||
320 | ->getResourceSetWrapper() |
||
321 | ->getResourceSet(); |
||
322 | $projectedProperty = $expandedProjectionNode->getResourceProperty(); |
||
323 | $result = $this->getProviders()->getRelatedResourceSet( |
||
324 | QueryType::ENTITIES(), //it's always entities for an expansion |
||
325 | $currentResourceSet, |
||
326 | $entry, |
||
327 | $resourceSetOfProjectedProperty, |
||
328 | $projectedProperty, |
||
329 | null, // $filter |
||
330 | null, // $orderby |
||
331 | null, // $top |
||
332 | null // $skip |
||
333 | )->results; |
||
334 | |||
335 | return $result; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @param ExpandedProjectionNode $expandedProjectionNode |
||
340 | * @param $entry |
||
341 | * @param \POData\Providers\Metadata\ResourceType $resourceType |
||
342 | * @param string $expandedPropertyName |
||
343 | * |
||
344 | * @throws InvalidOperationException |
||
345 | * @throws \POData\Common\ODataException |
||
346 | */ |
||
347 | private function executeSingleExpansionGetRelated( |
||
367 | } |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param $entry |
||
372 | * @param $result |
||
373 | * @param ExpandedProjectionNode $expandedProjectionNode |
||
374 | * @param \POData\Providers\Metadata\ResourceType $resourceType |
||
375 | * @param string $expandedPropertyName |
||
376 | * |
||
377 | * @throws InvalidOperationException |
||
378 | */ |
||
379 | private function executeCollectionExpansionProcessExpansion( |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param $result |
||
403 | * @param ExpandedProjectionNode $expandedProjectionNode |
||
404 | * |
||
405 | * @throws InvalidOperationException |
||
406 | */ |
||
407 | private function pushPropertyToNavigation($result, $expandedProjectionNode) |
||
413 | } |
||
414 | } |
||
415 |