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