Conditions | 12 |
Paths | 62 |
Total Lines | 56 |
Code Lines | 36 |
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 |
||
74 | public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = []) |
||
75 | { |
||
76 | $request = $this->requestStack->getCurrentRequest(); |
||
77 | if (null === $request) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
82 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
87 | if ($request->attributes->get('_graphql')) { |
||
88 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
89 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
90 | } |
||
91 | |||
92 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
93 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
94 | |||
95 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
96 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
97 | } |
||
98 | |||
99 | if (0 >= $itemsPerPage) { |
||
100 | throw new InvalidArgumentException('Item per page parameter should not be less than 1'); |
||
101 | } |
||
102 | |||
103 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
104 | |||
105 | if (1 > $page) { |
||
106 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
107 | } |
||
108 | |||
109 | $firstResult = ($page - 1) * $itemsPerPage; |
||
110 | if ($request->attributes->get('_graphql')) { |
||
111 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
112 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
113 | $after = \base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
114 | $firstResult = (int) $after; |
||
115 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | $repository = $this->managerRegistry->getManagerForClass($resourceClass)->getRepository($resourceClass); |
||
120 | $aggregationBuilder |
||
121 | ->facet() |
||
122 | ->field('results')->pipeline( |
||
123 | $repository->createAggregationBuilder() |
||
|
|||
124 | ->skip($firstResult) |
||
125 | ->limit($itemsPerPage) |
||
126 | ) |
||
127 | ->field('count')->pipeline( |
||
128 | $repository->createAggregationBuilder() |
||
129 | ->count('count') |
||
130 | ); |
||
177 |