| Total Complexity | 95 |
| Total Lines | 687 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 40 | class UriProcessorNew implements IUriProcessor |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Description of the OData request that a client has submitted. |
||
| 44 | * |
||
| 45 | * @var RequestDescription |
||
| 46 | */ |
||
| 47 | private $request; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Holds reference to the data service instance. |
||
| 51 | * |
||
| 52 | * @var IService |
||
| 53 | */ |
||
| 54 | private $service; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Holds reference to the wrapper over IDSMP and IDSQP implementation. |
||
| 58 | * |
||
| 59 | * @var ProvidersWrapper |
||
| 60 | */ |
||
| 61 | private $providers; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Holds reference to request expander. |
||
| 65 | * |
||
| 66 | * @var RequestExpander |
||
| 67 | */ |
||
| 68 | private $expander; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var ModelDeserialiser |
||
| 72 | */ |
||
| 73 | private $cereal; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var CynicDeserialiser |
||
| 77 | */ |
||
| 78 | private $cynicDeserialiser; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructs a new instance of UriProcessor. |
||
| 82 | * |
||
| 83 | * @param IService $service Reference to the data service instance |
||
| 84 | * @throws InvalidOperationException |
||
| 85 | * @throws ODataException |
||
| 86 | * @throws \Doctrine\Common\Annotations\AnnotationException |
||
| 87 | * @throws \POData\Common\UrlFormatException |
||
| 88 | * @throws \ReflectionException |
||
| 89 | */ |
||
| 90 | private function __construct(IService $service) |
||
| 91 | { |
||
| 92 | $this->service = $service; |
||
| 93 | $this->providers = $service->getProvidersWrapper(); |
||
| 94 | $this->request = ResourcePathProcessor::process($service); |
||
| 95 | $this->expander = new RequestExpander( |
||
| 96 | $this->getRequest(), |
||
| 97 | $this->getService(), |
||
| 98 | $this->getProviders() |
||
| 99 | ); |
||
| 100 | $this->getRequest()->setUriProcessor($this); |
||
| 101 | $this->cereal = new ModelDeserialiser(); |
||
| 102 | $this->cynicDeserialiser = new CynicDeserialiser( |
||
| 103 | $service->getMetadataProvider(), |
||
| 104 | $service->getProvidersWrapper() |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Process the resource path and query options of client's request uri. |
||
| 110 | * |
||
| 111 | * @param IService $service Reference to the data service instance |
||
| 112 | * |
||
| 113 | * @return IUriProcessor |
||
| 114 | * @throws InvalidOperationException |
||
| 115 | * @throws ODataException |
||
| 116 | * @throws \Doctrine\Common\Annotations\AnnotationException |
||
| 117 | * @throws \POData\Common\NotImplementedException |
||
| 118 | * @throws \ReflectionException |
||
| 119 | * @throws \POData\Common\UrlFormatException |
||
| 120 | */ |
||
| 121 | public static function process(IService $service) |
||
| 122 | { |
||
| 123 | $absRequestUri = $service->getHost()->getAbsoluteRequestUri(); |
||
| 124 | $absServiceUri = $service->getHost()->getAbsoluteServiceUri(); |
||
| 125 | |||
| 126 | if (!$absServiceUri->isBaseOf($absRequestUri)) { |
||
| 127 | throw ODataException::createInternalServerError( |
||
| 128 | Messages::uriProcessorRequestUriDoesNotHaveTheRightBaseUri( |
||
| 129 | $absRequestUri->getUrlAsString(), |
||
| 130 | $absServiceUri->getUrlAsString() |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $processor = new self($service); |
||
| 136 | //Parse the query string options of the request Uri. |
||
| 137 | QueryProcessor::process($processor->request, $service); |
||
| 138 | return $processor; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Gets reference to the request submitted by client. |
||
| 143 | * |
||
| 144 | * @return RequestDescription |
||
| 145 | */ |
||
| 146 | public function getRequest() |
||
| 147 | { |
||
| 148 | return $this->request; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets reference to the request submitted by client. |
||
| 153 | * |
||
| 154 | * @return ProvidersWrapper |
||
| 155 | */ |
||
| 156 | public function getProviders() |
||
| 157 | { |
||
| 158 | return $this->providers; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the data service instance. |
||
| 163 | * |
||
| 164 | * @return IService |
||
| 165 | */ |
||
| 166 | public function getService() |
||
| 167 | { |
||
| 168 | return $this->service; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Gets the request expander instance. |
||
| 173 | * |
||
| 174 | * @return RequestExpander |
||
| 175 | */ |
||
| 176 | public function getExpander() |
||
| 177 | { |
||
| 178 | return $this->expander; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return ModelDeserialiser |
||
| 183 | */ |
||
| 184 | public function getModelDeserialiser() |
||
| 185 | { |
||
| 186 | return $this->cereal; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return CynicDeserialiser |
||
| 191 | */ |
||
| 192 | public function getCynicDeserialiser() |
||
| 193 | { |
||
| 194 | return $this->cynicDeserialiser; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Execute the client submitted request against the data source. |
||
| 199 | * @throws InvalidOperationException |
||
| 200 | * @throws ODataException |
||
| 201 | * @throws \POData\Common\UrlFormatException |
||
| 202 | * @throws \ReflectionException |
||
| 203 | */ |
||
| 204 | public function execute() |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Execute the client submitted request against the data source (GET). |
||
| 240 | * @throws ODataException |
||
| 241 | * @throws InvalidOperationException |
||
| 242 | * @throws \ReflectionException |
||
| 243 | */ |
||
| 244 | protected function executeGet() |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Execute the client submitted request against the data source (DELETE). |
||
| 300 | * @throws ODataException |
||
| 301 | * @throws \POData\Common\UrlFormatException |
||
| 302 | */ |
||
| 303 | protected function executeDelete() |
||
| 304 | { |
||
| 305 | $segment = $this->getFinalEffectiveSegment(); |
||
| 306 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 307 | $resourceSet = $segment->getTargetResourceSetWrapper(); |
||
| 308 | $keyDescriptor = $segment->getKeyDescriptor(); |
||
| 309 | |||
| 310 | $this->checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod); |
||
| 311 | assert($resourceSet instanceof ResourceSet); |
||
| 312 | $this->getProviders()->deleteResource($resourceSet, $segment->getResult()); |
||
| 313 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_NOCONTENT); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Execute the client submitted request against the data source (PUT). |
||
| 318 | * @throws InvalidOperationException |
||
| 319 | * @throws ODataException |
||
| 320 | * @throws \ReflectionException |
||
| 321 | * @throws \Exception |
||
| 322 | */ |
||
| 323 | protected function executePut() |
||
| 324 | { |
||
| 325 | $segment = $this->getFinalEffectiveSegment(); |
||
| 326 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 327 | $resourceSet = null !== $segment ? $segment->getTargetResourceSetWrapper() : null; |
||
| 328 | $keyDescriptor = null !== $segment ? $segment->getKeyDescriptor() : null; |
||
| 329 | |||
| 330 | $this->checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod); |
||
| 331 | assert($resourceSet instanceof ResourceSet); |
||
| 332 | assert($keyDescriptor instanceof KeyDescriptor); |
||
| 333 | |||
| 334 | $payload = $this->getRequest()->getData(); |
||
| 335 | if (!$payload instanceof ODataEntry) { |
||
| 336 | throw new InvalidOperationException(get_class($payload)); |
||
| 337 | } |
||
| 338 | if (empty($payload->id)) { |
||
| 339 | throw new InvalidOperationException('Payload ID must not be empty for PUT request'); |
||
| 340 | } |
||
| 341 | $data = $this->getModelDeserialiser()->bulkDeserialise($resourceSet->getResourceType(), $payload); |
||
| 342 | |||
| 343 | if (empty($data)) { |
||
| 344 | throw ODataException::createBadRequestError(Messages::noDataForThisVerb($requestMethod)); |
||
| 345 | } |
||
| 346 | |||
| 347 | $queryResult = $this->getCynicDeserialiser()->processPayload($payload); |
||
| 348 | $segment->setResult($queryResult); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Execute the client submitted request against the data source (POST). |
||
| 353 | * @throws InvalidOperationException |
||
| 354 | * @throws ODataException |
||
| 355 | * @throws \POData\Common\UrlFormatException |
||
| 356 | * @throws \ReflectionException |
||
| 357 | * @throws \Exception |
||
| 358 | */ |
||
| 359 | protected function executePost() |
||
| 360 | { |
||
| 361 | $segments = $this->getRequest()->getSegments(); |
||
| 362 | $requestMethod = $this->getService()->getOperationContext()->incomingRequest()->getMethod(); |
||
| 363 | |||
| 364 | foreach ($segments as $segment) { |
||
| 365 | $requestTargetKind = $segment->getTargetKind(); |
||
| 366 | if ($requestTargetKind == TargetKind::RESOURCE()) { |
||
| 367 | $resourceSet = $segment->getTargetResourceSetWrapper(); |
||
| 368 | if (!$resourceSet) { |
||
| 369 | $url = $this->getService()->getHost()->getAbsoluteRequestUri()->getUrlAsString(); |
||
| 370 | $msg = Messages::badRequestInvalidUriForThisVerb($url, $requestMethod); |
||
| 371 | throw ODataException::createBadRequestError($msg); |
||
| 372 | } |
||
| 373 | |||
| 374 | $payload = $this->getRequest()->getData(); |
||
| 375 | if ($payload instanceof ODataURL) { |
||
| 376 | $this->executeGet(); |
||
| 377 | $masterModel = $this->getRequest()->getSegments()[0]->getResult(); |
||
| 378 | $masterResourceSet = $this->getRequest()->getSegments()[0]->getTargetResourceSetWrapper(); |
||
| 379 | $masterNavProperty = $this->getRequest()->getLastSegment()->getIdentifier(); |
||
| 380 | $slaveModelUri = new \POData\Common\Url($payload->url); |
||
| 381 | $host = $this->getService()->getHost(); |
||
| 382 | $absoluteServiceUri = $host->getAbsoluteServiceUri(); |
||
| 383 | $requestUriSegments = array_slice( |
||
| 384 | $slaveModelUri->getSegments(), |
||
| 385 | $absoluteServiceUri->getSegmentCount() |
||
| 386 | ); |
||
| 387 | $newSegments = SegmentParser::parseRequestUriSegments( |
||
| 388 | $requestUriSegments, |
||
| 389 | $this->getService()->getProvidersWrapper(), |
||
| 390 | true |
||
| 391 | ); |
||
| 392 | $this->executeGetResource($newSegments[0]); |
||
| 393 | $slaveModel = $newSegments[0]->getResult(); |
||
| 394 | $slaveResourceSet = $newSegments[0]->getTargetResourceSetWrapper(); |
||
| 395 | $linkAdded = $this->getProviders() |
||
| 396 | ->hookSingleModel( |
||
| 397 | $masterResourceSet, |
||
| 398 | $masterModel, |
||
| 399 | $slaveResourceSet, |
||
| 400 | $slaveModel, |
||
| 401 | $masterNavProperty |
||
| 402 | ); |
||
| 403 | if ($linkAdded) { |
||
| 404 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_NOCONTENT); |
||
| 405 | } else { |
||
| 406 | throw ODataException::createInternalServerError('AdapterIndicatedLinkNotAttached'); |
||
| 407 | } |
||
| 408 | foreach ($segments as $segment) { |
||
| 409 | $segment->setResult(null); |
||
| 410 | } |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | if (!$payload instanceof ODataEntry) { |
||
| 414 | throw new InvalidOperationException(get_class($payload)); |
||
| 415 | } |
||
| 416 | if (!empty($payload->id)) { |
||
| 417 | throw new InvalidOperationException('Payload ID must be empty for POST request'); |
||
| 418 | } |
||
| 419 | |||
| 420 | $data = $this->getModelDeserialiser()->bulkDeserialise($resourceSet->getResourceType(), $payload); |
||
| 421 | |||
| 422 | if (empty($data)) { |
||
| 423 | throw ODataException::createBadRequestError(Messages::noDataForThisVerb($requestMethod)); |
||
| 424 | } |
||
| 425 | $this->getService()->getHost()->setResponseStatusCode(HttpStatus::CODE_CREATED); |
||
| 426 | $queryResult = $this->getCynicDeserialiser()->processPayload($payload); |
||
| 427 | $keyID = $payload->id; |
||
| 428 | if (!$keyID instanceof KeyDescriptor) { |
||
| 429 | throw new InvalidOperationException(get_class($keyID)); |
||
| 430 | } |
||
| 431 | |||
| 432 | $locationUrl = $keyID->generateRelativeUri($resourceSet->getResourceSet()); |
||
| 433 | $absoluteServiceUri = $this->getService()->getHost()->getAbsoluteServiceUri()->getUrlAsString(); |
||
| 434 | $location = rtrim($absoluteServiceUri, '/') . '/' . $locationUrl; |
||
| 435 | $this->getService()->getHost()->setResponseLocation($location); |
||
| 436 | $segment->setResult($queryResult); |
||
| 437 | } |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return null|SegmentDescriptor |
||
| 443 | */ |
||
| 444 | protected function getFinalEffectiveSegment() |
||
| 445 | { |
||
| 446 | $segment = $this->getRequest()->getLastSegment(); |
||
| 447 | // if last segment is $count, back up one |
||
| 448 | if (null !== $segment && ODataConstants::URI_COUNT_SEGMENT == $segment->getIdentifier()) { |
||
| 449 | $segment = $segment->getPrevious(); |
||
| 450 | return $segment; |
||
| 451 | } |
||
| 452 | return $segment; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param $resourceSet |
||
| 457 | * @param $keyDescriptor |
||
| 458 | * @param $requestMethod |
||
| 459 | * @throws ODataException |
||
| 460 | * @throws \POData\Common\UrlFormatException |
||
| 461 | */ |
||
| 462 | protected function checkUriValidForSuppliedVerb($resourceSet, $keyDescriptor, $requestMethod) |
||
| 468 | ); |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param SegmentDescriptor $segment |
||
| 474 | */ |
||
| 475 | private function executeGetSingleton(SegmentDescriptor $segment) |
||
| 476 | { |
||
| 477 | $segmentId = $segment->getIdentifier(); |
||
| 478 | $singleton = $this->getService()->getProvidersWrapper()->resolveSingleton($segmentId); |
||
| 479 | $segment->setResult($singleton->get()); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param SegmentDescriptor $segment |
||
| 484 | * @param array $eagerList |
||
| 485 | * @throws InvalidOperationException |
||
| 486 | * @throws ODataException |
||
| 487 | * @throws \ReflectionException |
||
| 488 | */ |
||
| 489 | private function executeGetResource(SegmentDescriptor $segment, array $eagerList = []) |
||
| 490 | { |
||
| 491 | foreach ($eagerList as $eager) { |
||
| 492 | $nonEmpty = is_string($eager) && 0 < strlen($eager); |
||
| 493 | if (!$nonEmpty) { |
||
| 494 | throw new InvalidOperationException('Eager-load list elements must be non-empty strings'); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | $isRelated = TargetSource::ENTITY_SET() == $segment->getTargetSource(); |
||
| 498 | if ($isRelated) { |
||
| 499 | $queryResult = $this->executeGetResourceDirect($segment, $eagerList); |
||
| 500 | } else { |
||
| 501 | $queryResult = $this->executeGetResourceRelated($segment, $eagerList); |
||
| 502 | } |
||
| 503 | $segment->setResult($queryResult); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param SegmentDescriptor $segment |
||
| 508 | */ |
||
| 509 | private function executeGetLink(SegmentDescriptor $segment) |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param SegmentDescriptor $segment |
||
| 518 | * @param array $eagerList |
||
| 519 | * @return null|object|QueryResult |
||
| 520 | * @throws InvalidOperationException |
||
| 521 | * @throws ODataException |
||
| 522 | * @throws \ReflectionException |
||
| 523 | */ |
||
| 524 | private function executeGetResourceDirect(SegmentDescriptor $segment, array $eagerList) |
||
| 549 | } |
||
| 550 | |||
| 551 | /** |
||
| 552 | * @param SegmentDescriptor $segment |
||
| 553 | * @param $eagerList |
||
| 554 | * @return null|object|QueryResult |
||
| 555 | * @throws InvalidOperationException |
||
| 556 | * @throws ODataException |
||
| 557 | * @throws \ReflectionException |
||
| 558 | */ |
||
| 559 | private function executeGetResourceRelated(SegmentDescriptor $segment, $eagerList) |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Query for a resource set pointed by the given segment descriptor and update the descriptor with the result. |
||
| 608 | * |
||
| 609 | * @param SegmentDescriptor $segment Describes the resource set to query |
||
| 610 | * @param array|null $eagerLoad |
||
| 611 | * @throws InvalidOperationException |
||
| 612 | * @throws ODataException |
||
| 613 | * @throws \ReflectionException |
||
| 614 | */ |
||
| 615 | private function handleSegmentTargetsToResourceSet(SegmentDescriptor $segment, $eagerLoad) |
||
| 616 | { |
||
| 617 | if ($segment->isSingleResult()) { |
||
| 618 | $entityInstance = $this->getProviders()->getResourceFromResourceSet( |
||
| 619 | $segment->getTargetResourceSetWrapper(), |
||
| 620 | $segment->getKeyDescriptor() |
||
| 621 | ); |
||
| 622 | |||
| 623 | $segment->setResult($entityInstance); |
||
| 624 | } else { |
||
| 625 | $eagerLoad = (null !== $eagerLoad) ? $eagerLoad : []; |
||
| 626 | $skip = (null == $this->getRequest()) ? 0 : $this->getRequest()->getSkipCount(); |
||
| 627 | $skip = (null === $skip) ? 0 : $skip; |
||
| 628 | $skipToken = $this->getRequest()->getInternalSkipTokenInfo(); |
||
| 629 | $skipToken = (null != $skipToken) ? $skipToken->getSkipTokenInfo() : null; |
||
| 630 | $queryResult = $this->getProviders()->getResourceSet( |
||
| 631 | $this->getRequest()->queryType, |
||
| 632 | $segment->getTargetResourceSetWrapper(), |
||
| 633 | $this->getRequest()->getFilterInfo(), |
||
| 634 | $this->getRequest()->getInternalOrderByInfo(), |
||
| 635 | $this->getRequest()->getTopCount(), |
||
| 636 | $skip, |
||
| 637 | $skipToken, |
||
| 638 | $eagerLoad |
||
| 639 | ); |
||
| 640 | $segment->setResult($queryResult); |
||
| 641 | } |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param SegmentDescriptor $segment |
||
| 646 | * @throws ODataException |
||
| 647 | */ |
||
| 648 | private function checkResourceExistsByIdentifier(SegmentDescriptor $segment) |
||
| 649 | { |
||
| 650 | if (null === $segment->getPrevious()->getResult()) { |
||
| 651 | throw ODataException::createResourceNotFoundError( |
||
| 652 | $segment->getPrevious()->getIdentifier() |
||
| 653 | ); |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Applies the query options to the resource(s) retrieved from the data source. |
||
| 659 | * |
||
| 660 | * @param SegmentDescriptor $segment The descriptor which holds resource(s) on which query options to be applied |
||
| 661 | * @throws ODataException |
||
| 662 | */ |
||
| 663 | private function applyQueryOptions(SegmentDescriptor $segment) |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * If the provider does not perform the paging (ordering, top, skip) then this method does it. |
||
| 697 | * |
||
| 698 | * @param array $result |
||
| 699 | * |
||
| 700 | * @return array |
||
| 701 | * @throws ODataException |
||
| 702 | */ |
||
| 703 | private function performPaging(array $result) |
||
| 727 | } |
||
| 728 | } |
||
| 729 |