| Conditions | 25 |
| Paths | 473 |
| Total Lines | 69 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 53 | public function normalize($object, $format = null, array $context = []) |
||
| 54 | { |
||
| 55 | $data = $this->collectionNormalizer->normalize($object, $format, $context); |
||
| 56 | if (!\is_array($data)) { |
||
| 57 | throw new UnexpectedValueException('Expected data to be an array'); |
||
| 58 | } |
||
| 59 | |||
| 60 | if (isset($context['api_sub_level'])) { |
||
| 61 | return $data; |
||
| 62 | } |
||
| 63 | |||
| 64 | $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = null; |
||
| 65 | if ($paginated = $object instanceof PartialPaginatorInterface) { |
||
| 66 | if ($object instanceof PaginatorInterface) { |
||
| 67 | $paginated = 1. !== $lastPage = $object->getLastPage(); |
||
| 68 | } else { |
||
| 69 | $itemsPerPage = $object->getItemsPerPage(); |
||
| 70 | $pageTotalItems = (float) \count($object); |
||
| 71 | } |
||
| 72 | |||
| 73 | $currentPage = $object->getCurrentPage(); |
||
| 74 | } |
||
| 75 | |||
| 76 | $parsed = IriHelper::parseIri($context['request_uri'] ?? '/', $this->pageParameterName); |
||
| 77 | $appliedFilters = $parsed['parameters']; |
||
| 78 | unset($appliedFilters[$this->enabledParameterName]); |
||
| 79 | |||
| 80 | if (!$appliedFilters && !$paginated) { |
||
| 81 | return $data; |
||
| 82 | } |
||
| 83 | |||
| 84 | $metadata = isset($context['resource_class']) && null !== $this->resourceMetadataFactory ? $this->resourceMetadataFactory->create($context['resource_class']) : null; |
||
| 85 | $isPaginatedWithCursor = $paginated && null !== $metadata && null !== $cursorPaginationAttribute = $metadata->getCollectionOperationAttribute($context['collection_operation_name'] ?? $context['subresource_operation_name'], 'pagination_via_cursor', null, true); |
||
| 86 | |||
| 87 | $data['hydra:view'] = [ |
||
| 88 | '@id' => IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $paginated && !$isPaginatedWithCursor ? $currentPage : null), |
||
| 89 | '@type' => 'hydra:PartialCollectionView', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | if ($isPaginatedWithCursor) { |
||
| 93 | $objects = iterator_to_array($object); |
||
| 94 | $firstObject = current($objects); |
||
| 95 | $lastObject = end($objects); |
||
| 96 | |||
| 97 | $data['hydra:view']['@id'] = IriHelper::createIri($parsed['parts'], $parsed['parameters']); |
||
| 98 | |||
| 99 | if (false !== $lastObject && isset($cursorPaginationAttribute)) { |
||
| 100 | $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, 1, $lastObject))); |
||
| 101 | } |
||
| 102 | |||
| 103 | if (false !== $firstObject && isset($cursorPaginationAttribute)) { |
||
| 104 | $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], array_merge($parsed['parameters'], $this->cursorPaginationFields($cursorPaginationAttribute, -1, $firstObject))); |
||
| 105 | } |
||
| 106 | } elseif ($paginated) { |
||
| 107 | if (null !== $lastPage) { |
||
| 108 | $data['hydra:view']['hydra:first'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, 1.); |
||
| 109 | $data['hydra:view']['hydra:last'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $lastPage); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (1. !== $currentPage) { |
||
| 113 | $data['hydra:view']['hydra:previous'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage - 1.); |
||
| 114 | } |
||
| 115 | |||
| 116 | if (null !== $lastPage && $currentPage !== $lastPage || null === $lastPage && $pageTotalItems >= $itemsPerPage) { |
||
|
|
|||
| 117 | $data['hydra:view']['hydra:next'] = IriHelper::createIri($parsed['parts'], $parsed['parameters'], $this->pageParameterName, $currentPage + 1.); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | return $data; |
||
| 122 | } |
||
| 168 |