| Conditions | 9 |
| Paths | 8 |
| Total Lines | 67 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 44 | public function process($collectionEntity, array $context = array(), ?string $format = null) |
||
| 45 | { |
||
| 46 | $resourceMetadata = $this->resourceMetadataFactory->create($collectionEntity->getResource()); |
||
| 47 | $requestUri = null; |
||
| 48 | |||
| 49 | $collectionOperations = $resourceMetadata->getCollectionOperations(); |
||
| 50 | if ($collectionOperations) { |
||
| 51 | $collectionOperations = array_change_key_case($collectionOperations, CASE_LOWER); |
||
| 52 | $baseRoute = trim(trim($resourceMetadata->getAttribute('route_prefix', '')), '/'); |
||
| 53 | $methods = ['post', 'get']; |
||
| 54 | foreach ($methods as $method) { |
||
| 55 | if (array_key_exists($method, $collectionOperations)) { |
||
| 56 | $path = $baseRoute . $this->operationPathResolver->resolveOperationPath( |
||
| 57 | $resourceMetadata->getShortName(), |
||
|
|
|||
| 58 | $collectionOperations[$method], |
||
| 59 | OperationType::COLLECTION, |
||
| 60 | $method); |
||
| 61 | $finalPath = preg_replace('/{_format}$/', $format, $path); |
||
| 62 | $collectionEntity->addCollectionRoute( |
||
| 63 | $method, |
||
| 64 | $finalPath |
||
| 65 | ); |
||
| 66 | if ($method === 'get') { |
||
| 67 | $requestUri = $finalPath; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 | $collectionResourceAttributes = $resourceMetadata->getAttributes(); |
||
| 73 | |||
| 74 | /** @var ContextAwareCollectionDataProviderInterface $dataProvider */ |
||
| 75 | $dataProvider = $this->container->get(ContextAwareCollectionDataProviderInterface::class); |
||
| 76 | $dataProviderContext = [ |
||
| 77 | 'filters' => [ |
||
| 78 | 'pagination' => true, |
||
| 79 | '_page' => 1 |
||
| 80 | ]]; |
||
| 81 | $request = $apiPagination = null; |
||
| 82 | if ($collectionEntity->getPerPage() && ($request = $this->requestStack->getCurrentRequest())) { |
||
| 83 | $apiPagination = $request->attributes->get('_api_pagination'); |
||
| 84 | $originalPerPage = $apiPagination['itemsPerPage']; |
||
| 85 | $apiPagination['itemsPerPage'] = $collectionEntity->getPerPage(); |
||
| 86 | $request->attributes->set('_api_pagination', $apiPagination); |
||
| 87 | $apiPagination['itemsPerPage'] = $originalPerPage; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** @var Paginator $collection */ |
||
| 91 | $collection = $dataProvider->getCollection($collectionEntity->getResource(), Request::METHOD_GET, $dataProviderContext); |
||
| 92 | |||
| 93 | if ($request && $apiPagination) { |
||
| 94 | $request->attributes->set('_api_pagination', $apiPagination); |
||
| 95 | } |
||
| 96 | |||
| 97 | $forcedContext = [ |
||
| 98 | 'resource_class' => Collection::class, |
||
| 99 | 'request_uri' => $requestUri, |
||
| 100 | 'jsonld_has_context' => false, |
||
| 101 | 'api_sub_level' => null |
||
| 102 | ]; |
||
| 103 | $mergedContext = array_merge($context, $forcedContext); |
||
| 104 | $normalizedCollection = $this->itemNormalizer->normalize( |
||
| 105 | $collection, |
||
| 106 | $format, |
||
| 107 | $mergedContext |
||
| 108 | ); |
||
| 109 | |||
| 110 | $collectionEntity->setCollection($normalizedCollection); |
||
| 111 | } |
||
| 126 |