| Total Complexity | 87 |
| Total Lines | 619 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like UriProcessorNew 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 UriProcessorNew, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class UriProcessorNew implements IUriProcessor |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * Description of the OData request that a client has submitted. |
||
| 43 | * |
||
| 44 | * @var RequestDescription |
||
| 45 | */ |
||
| 46 | private $request; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Holds reference to the data service instance. |
||
| 50 | * |
||
| 51 | * @var IService |
||
| 52 | */ |
||
| 53 | private $service; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Holds reference to the wrapper over IDSMP and IDSQP implementation. |
||
| 57 | * |
||
| 58 | * @var ProvidersWrapper |
||
| 59 | */ |
||
| 60 | private $providers; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Holds reference to request expander. |
||
| 64 | * |
||
| 65 | * @var RequestExpander |
||
| 66 | */ |
||
| 67 | private $expander; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var ModelDeserialiser |
||
| 71 | */ |
||
| 72 | private $cereal; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var CynicDeserialiser |
||
| 76 | */ |
||
| 77 | private $cynicDeserialiser; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Constructs a new instance of UriProcessor. |
||
| 81 | * |
||
| 82 | * @param IService $service Reference to the data service instance |
||
| 83 | */ |
||
| 84 | private function __construct(IService $service) |
||
| 85 | { |
||
| 86 | $this->service = $service; |
||
| 87 | $this->providers = $service->getProvidersWrapper(); |
||
| 88 | $this->request = ResourcePathProcessor::process($service); |
||
| 89 | $this->expander = new RequestExpander( |
||
| 90 | $this->getRequest(), |
||
| 91 | $this->getService(), |
||
| 92 | $this->getProviders() |
||
| 93 | ); |
||
| 94 | $this->getRequest()->setUriProcessor($this); |
||
| 95 | $this->cereal = new ModelDeserialiser(); |
||
| 96 | $this->cynicDeserialiser = new CynicDeserialiser( |
||
| 97 | $service->getMetadataProvider(), |
||
| 98 | $service->getProvidersWrapper() |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Process the resource path and query options of client's request uri. |
||
| 104 | * |
||
| 105 | * @param IService $service Reference to the data service instance |
||
| 106 | * |
||
| 107 | * @throws ODataException |
||
| 108 | * |
||
| 109 | * @return IUriProcessor |
||
| 110 | */ |
||
| 111 | public static function process(IService $service) |
||
| 112 | { |
||
| 113 | $absRequestUri = $service->getHost()->getAbsoluteRequestUri(); |
||
| 114 | $absServiceUri = $service->getHost()->getAbsoluteServiceUri(); |
||
| 115 | |||
| 116 | if (!$absServiceUri->isBaseOf($absRequestUri)) { |
||
| 117 | throw ODataException::createInternalServerError( |
||
| 118 | Messages::uriProcessorRequestUriDoesNotHaveTheRightBaseUri( |
||
| 119 | $absRequestUri->getUrlAsString(), |
||
| 120 | $absServiceUri->getUrlAsString() |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | $processor = new self($service); |
||
| 126 | //Parse the query string options of the request Uri. |
||
| 127 | QueryProcessor::process($processor->request, $service); |
||
| 128 | return $processor; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Gets reference to the request submitted by client. |
||
| 133 | * |
||
| 134 | * @return RequestDescription |
||
| 135 | */ |
||
| 136 | public function getRequest() |
||
| 137 | { |
||
| 138 | return $this->request; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Gets reference to the request submitted by client. |
||
| 143 | * |
||
| 144 | * @return ProvidersWrapper |
||
| 145 | */ |
||
| 146 | public function getProviders() |
||
| 147 | { |
||
| 148 | return $this->providers; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the data service instance. |
||
| 153 | * |
||
| 154 | * @return IService |
||
| 155 | */ |
||
| 156 | public function getService() |
||
| 157 | { |
||
| 158 | return $this->service; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the request expander instance. |
||
| 163 | * |
||
| 164 | * @return RequestExpander |
||
| 165 | */ |
||
| 166 | public function getExpander() |
||
| 167 | { |
||
| 168 | return $this->expander; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return ModelDeserialiser |
||
| 173 | */ |
||
| 174 | public function getModelDeserialiser() |
||
| 175 | { |
||
| 176 | return $this->cereal; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function getCynicDeserialiser() |
||
| 180 | { |
||
| 181 | return $this->cynicDeserialiser; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Execute the client submitted request against the data source. |
||
| 186 | */ |
||
| 187 | public function execute() |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Execute the client submitted request against the data source (GET). |
||
| 221 | */ |
||
| 222 | protected function executeGet() |
||
| 223 | { |
||
| 224 | $segments = $this->getRequest()->getSegments(); |
||
| 225 | $root = $this->getRequest()->getRootProjectionNode(); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Execute the client submitted request against the data source (DELETE). |
||
| 278 | */ |
||
| 279 | protected function executeDelete() |
||
| 280 | { |
||
| 281 | $segment = $this->getFinalEffectiveSegment(); |
||
| 282 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 283 | $resourceSet = $segment->getTargetResourceSetWrapper(); |
||
| 284 | $keyDescriptor = $segment->getKeyDescriptor(); |
||
| 285 | |||
| 286 | $this->checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod); |
||
| 287 | assert($resourceSet instanceof ResourceSet); |
||
|
|
|||
| 288 | $this->getProviders()->deleteResource($resourceSet, $segment->getResult()); |
||
| 289 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_NOCONTENT); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Execute the client submitted request against the data source (PUT). |
||
| 294 | */ |
||
| 295 | protected function executePut() |
||
| 296 | { |
||
| 297 | $segment = $this->getFinalEffectiveSegment(); |
||
| 298 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 299 | $resourceSet = null !== $segment ? $segment->getTargetResourceSetWrapper() : null; |
||
| 300 | $keyDescriptor = null !== $segment ? $segment->getKeyDescriptor() : null; |
||
| 301 | |||
| 302 | $this->checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod); |
||
| 303 | assert($resourceSet instanceof ResourceSet); |
||
| 304 | assert($keyDescriptor instanceof KeyDescriptor); |
||
| 305 | |||
| 306 | $payload = $this->getRequest()->getData(); |
||
| 307 | assert($payload instanceof ODataEntry, get_class($payload)); |
||
| 308 | assert(!empty($payload->id), 'Payload ID must not be empty for PUT request'); |
||
| 309 | $data = $this->getModelDeserialiser()->bulkDeserialise($resourceSet->getResourceType(), $payload); |
||
| 310 | |||
| 311 | if (empty($data)) { |
||
| 312 | throw ODataException::createBadRequestError(Messages::noDataForThisVerb($requestMethod)); |
||
| 313 | } |
||
| 314 | |||
| 315 | $queryResult = $this->getCynicDeserialiser()->processPayload($payload); |
||
| 316 | $segment->setResult($queryResult); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Execute the client submitted request against the data source (POST). |
||
| 321 | */ |
||
| 322 | protected function executePost() |
||
| 323 | { |
||
| 324 | $segments = $this->getRequest()->getSegments(); |
||
| 325 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 326 | |||
| 327 | foreach ($segments as $segment) { |
||
| 328 | $requestTargetKind = $segment->getTargetKind(); |
||
| 329 | if ($requestTargetKind == TargetKind::RESOURCE()) { |
||
| 330 | $resourceSet = $segment->getTargetResourceSetWrapper(); |
||
| 331 | if (!$resourceSet) { |
||
| 332 | $url = $this->getService()->getHost()->getAbsoluteRequestUri()->getUrlAsString(); |
||
| 333 | $msg = Messages::badRequestInvalidUriForThisVerb($url, $requestMethod); |
||
| 334 | throw ODataException::createBadRequestError($msg); |
||
| 335 | } |
||
| 336 | |||
| 337 | $payload = $this->getRequest()->getData(); |
||
| 338 | if ($payload instanceof ODataURL) { |
||
| 339 | $this->executeGet(); |
||
| 340 | $masterModel = $this->getRequest()->getSegments()[0]->getResult(); |
||
| 341 | $masterResourceSet = $this->getRequest()->getSegments()[0]->getTargetResourceSetWrapper(); |
||
| 342 | $masterNavProperty = $this->getRequest()->getLastSegment()->getIdentifier(); |
||
| 343 | $slaveModelUri = new \POData\Common\Url($payload->url); |
||
| 344 | $host = $this->service->getHost(); |
||
| 345 | $absoluteServiceUri = $host->getAbsoluteServiceUri(); |
||
| 346 | $requestUriSegments = array_slice( |
||
| 347 | $slaveModelUri->getSegments(), |
||
| 348 | $absoluteServiceUri->getSegmentCount() |
||
| 349 | ); |
||
| 350 | $newSegments = SegmentParser::parseRequestUriSegments( |
||
| 351 | $requestUriSegments, |
||
| 352 | $this->service->getProvidersWrapper(), |
||
| 353 | true |
||
| 354 | ); |
||
| 355 | $this->executeGetResource($newSegments[0]); |
||
| 356 | $slaveModel = $newSegments[0]->getResult(); |
||
| 357 | $slaveResourceSet = $newSegments[0]->getTargetResourceSetWrapper(); |
||
| 358 | $linkAdded = $this->getProviders() |
||
| 359 | ->hookSingleModel( |
||
| 360 | $masterResourceSet, |
||
| 361 | $masterModel, |
||
| 362 | $slaveResourceSet, |
||
| 363 | $slaveModel, |
||
| 364 | $masterNavProperty |
||
| 365 | ); |
||
| 366 | if ($linkAdded) { |
||
| 367 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_NOCONTENT); |
||
| 368 | } else { |
||
| 369 | throw ODataException::createInternalServerError('AdapterIndicatedLinkNotAttached'); |
||
| 370 | } |
||
| 371 | foreach ($segments as $segment) { |
||
| 372 | $segment->setResult(null); |
||
| 373 | } |
||
| 374 | return; |
||
| 375 | } |
||
| 376 | assert($payload instanceof ODataEntry, get_class($payload)); |
||
| 377 | assert(empty($payload->id), 'Payload ID must be empty for POST request'); |
||
| 378 | $data = $this->getModelDeserialiser()->bulkDeserialise($resourceSet->getResourceType(), $payload); |
||
| 379 | |||
| 380 | if (empty($data)) { |
||
| 381 | throw ODataException::createBadRequestError(Messages::noDataForThisVerb($requestMethod)); |
||
| 382 | } |
||
| 383 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_CREATED); |
||
| 384 | $queryResult = $this->getCynicDeserialiser()->processPayload($payload); |
||
| 385 | $keyID = $payload->id; |
||
| 386 | assert($keyID instanceof KeyDescriptor, get_class($keyID)); |
||
| 387 | $locationUrl = $keyID->generateRelativeUri($resourceSet->getResourceSet()); |
||
| 388 | $absoluteServiceUri = $this->getService()->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
| 389 | $location = rtrim($absoluteServiceUri, '/') . '/' . $locationUrl; |
||
| 390 | $this->getService()->getHost()->setResponseLocation($location); |
||
| 391 | $segment->setResult($queryResult); |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return null|SegmentDescriptor |
||
| 398 | */ |
||
| 399 | protected function getFinalEffectiveSegment() |
||
| 400 | { |
||
| 401 | $segment = $this->getRequest()->getLastSegment(); |
||
| 402 | // if last segment is $count, back up one |
||
| 403 | if (null !== $segment && ODataConstants::URI_COUNT_SEGMENT == $segment->getIdentifier()) { |
||
| 404 | $segment = $segment->getPrevious(); |
||
| 405 | return $segment; |
||
| 406 | } |
||
| 407 | return $segment; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param $resourceSet |
||
| 412 | * @param $keyDescriptor |
||
| 413 | * @param $requestMethod |
||
| 414 | * @throws ODataException |
||
| 415 | */ |
||
| 416 | protected function checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod) |
||
| 422 | ); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param $segment |
||
| 428 | */ |
||
| 429 | private function executeGetSingleton($segment) |
||
| 430 | { |
||
| 431 | $segmentId = $segment->getIdentifier(); |
||
| 432 | $singleton = $this->getService()->getProvidersWrapper()->resolveSingleton($segmentId); |
||
| 433 | $segment->setResult($singleton->get()); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param $segment |
||
| 438 | */ |
||
| 439 | private function executeGetResource($segment, array $eagerList = []) |
||
| 440 | { |
||
| 441 | foreach ($eagerList as $eager) { |
||
| 442 | assert(is_string($eager) && 0 < strlen($eager), 'Eager-load list elements must be non-empty strings'); |
||
| 443 | } |
||
| 444 | $isRelated = TargetSource::ENTITY_SET == $segment->getTargetSource(); |
||
| 445 | if ($isRelated) { |
||
| 446 | $queryResult = $this->executeGetResourceDirect($segment, $eagerList); |
||
| 447 | } else { |
||
| 448 | $queryResult = $this->executeGetResourceRelated($segment, $eagerList); |
||
| 449 | } |
||
| 450 | $segment->setResult($queryResult); |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param $segment |
||
| 455 | */ |
||
| 456 | private function executeGetLink($segment) |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param $segment |
||
| 465 | * @return null|object|QueryResult |
||
| 466 | */ |
||
| 467 | private function executeGetResourceDirect($segment, array $eagerList) |
||
| 492 | } |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param $segment |
||
| 496 | * @return null|object|QueryResult |
||
| 497 | */ |
||
| 498 | private function executeGetResourceRelated($segment, $eagerList) |
||
| 499 | { |
||
| 500 | $projectedProperty = $segment->getProjectedProperty(); |
||
| 501 | $projectedPropertyKind = null !== $projectedProperty ? $projectedProperty->getKind() : 0; |
||
| 502 | $queryResult = null; |
||
| 503 | switch ($projectedPropertyKind) { |
||
| 504 | case ResourcePropertyKind::RESOURCE_REFERENCE: |
||
| 505 | $queryResult = $this->getProviders()->getRelatedResourceReference( |
||
| 506 | $segment->getPrevious()->getTargetResourceSetWrapper(), |
||
| 507 | $segment->getPrevious()->getResult(), |
||
| 508 | $segment->getTargetResourceSetWrapper(), |
||
| 509 | $projectedProperty |
||
| 510 | ); |
||
| 511 | break; |
||
| 512 | case ResourcePropertyKind::RESOURCESET_REFERENCE: |
||
| 513 | if ($segment->isSingleResult()) { |
||
| 514 | $queryResult = $this->getProviders()->getResourceFromRelatedResourceSet( |
||
| 515 | $segment->getPrevious()->getTargetResourceSetWrapper(), |
||
| 516 | $segment->getPrevious()->getResult(), |
||
| 517 | $segment->getTargetResourceSetWrapper(), |
||
| 518 | $projectedProperty, |
||
| 519 | $segment->getKeyDescriptor() |
||
| 520 | ); |
||
| 521 | } else { |
||
| 522 | $skipToken = $this->getRequest()->getInternalSkipTokenInfo(); |
||
| 523 | $skipToken = (null !== $skipToken) ? $skipToken->getSkipTokenInfo() : null; |
||
| 524 | $queryResult = $this->getProviders()->getRelatedResourceSet( |
||
| 525 | $this->getRequest()->queryType, |
||
| 526 | $segment->getPrevious()->getTargetResourceSetWrapper(), |
||
| 527 | $segment->getPrevious()->getResult(), |
||
| 528 | $segment->getTargetResourceSetWrapper(), |
||
| 529 | $projectedProperty, |
||
| 530 | $this->getRequest()->getFilterInfo(), |
||
| 531 | null, // $orderby |
||
| 532 | null, // $top |
||
| 533 | null, // $skip |
||
| 534 | $skipToken |
||
| 535 | ); |
||
| 536 | } |
||
| 537 | break; |
||
| 538 | default: |
||
| 539 | $this->checkResourceExistsByIdentifier($segment); |
||
| 540 | assert(false, 'Invalid property kind type for resource retrieval'); |
||
| 541 | } |
||
| 542 | return $queryResult; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Query for a resource set pointed by the given segment descriptor and update the descriptor with the result. |
||
| 547 | * |
||
| 548 | * @param SegmentDescriptor $segment Describes the resource set to query |
||
| 549 | */ |
||
| 550 | private function handleSegmentTargetsToResourceSet(SegmentDescriptor $segment) |
||
| 551 | { |
||
| 552 | if ($segment->isSingleResult()) { |
||
| 553 | $entityInstance = $this->getProviders()->getResourceFromResourceSet( |
||
| 554 | $segment->getTargetResourceSetWrapper(), |
||
| 555 | $segment->getKeyDescriptor() |
||
| 556 | ); |
||
| 557 | |||
| 558 | $segment->setResult($entityInstance); |
||
| 559 | } else { |
||
| 560 | $skip = (null == $this->getRequest()) ? 0 : $this->getRequest()->getSkipCount(); |
||
| 561 | $skip = (null === $skip) ? 0 : $skip; |
||
| 562 | $skipToken = $this->getRequest()->getInternalSkipTokenInfo(); |
||
| 563 | $skipToken = (null != $skipToken) ? $skipToken->getSkipTokenInfo() : null; |
||
| 564 | $queryResult = $this->getProviders()->getResourceSet( |
||
| 565 | $this->getRequest()->queryType, |
||
| 566 | $segment->getTargetResourceSetWrapper(), |
||
| 567 | $this->getRequest()->getFilterInfo(), |
||
| 568 | $this->getRequest()->getInternalOrderByInfo(), |
||
| 569 | $this->getRequest()->getTopCount(), |
||
| 570 | $skip, |
||
| 571 | $skipToken |
||
| 572 | ); |
||
| 573 | $segment->setResult($queryResult); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @param $segment |
||
| 579 | * @throws ODataException |
||
| 580 | */ |
||
| 581 | private function checkResourceExistsByIdentifier($segment) |
||
| 582 | { |
||
| 583 | if (null === $segment->getPrevious()->getResult()) { |
||
| 584 | throw ODataException::createResourceNotFoundError( |
||
| 585 | $segment->getPrevious()->getIdentifier() |
||
| 586 | ); |
||
| 587 | } |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Applies the query options to the resource(s) retrieved from the data source. |
||
| 592 | * |
||
| 593 | * @param SegmentDescriptor $segment The descriptor which holds resource(s) on which query options to be applied |
||
| 594 | */ |
||
| 595 | private function applyQueryOptions(SegmentDescriptor $segment) |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * If the provider does not perform the paging (ordering, top, skip) then this method does it. |
||
| 629 | * |
||
| 630 | * @param array $result |
||
| 631 | * |
||
| 632 | * @return array |
||
| 633 | */ |
||
| 634 | private function performPaging(array $result) |
||
| 658 | } |
||
| 659 | } |
||
| 660 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.