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