Total Complexity | 49 |
Total Lines | 338 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PaginationExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PaginationExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | final class PaginationExtension implements ContextAwareQueryResultCollectionExtensionInterface |
||
38 | { |
||
39 | private $managerRegistry; |
||
40 | private $requestStack; |
||
41 | private $resourceMetadataFactory; |
||
42 | private $enabled; |
||
43 | private $clientEnabled; |
||
44 | private $clientItemsPerPage; |
||
45 | private $itemsPerPage; |
||
46 | private $pageParameterName; |
||
47 | private $enabledParameterName; |
||
48 | private $itemsPerPageParameterName; |
||
49 | private $maximumItemPerPage; |
||
50 | private $partial; |
||
51 | private $clientPartial; |
||
52 | private $partialParameterName; |
||
53 | private $pagination; |
||
54 | |||
55 | /** |
||
56 | * @param ResourceMetadataFactoryInterface|RequestStack $resourceMetadataFactory |
||
57 | * @param Pagination|ResourceMetadataFactoryInterface $pagination |
||
58 | */ |
||
59 | public function __construct(ManagerRegistry $managerRegistry, /* ResourceMetadataFactoryInterface */ $resourceMetadataFactory, /* Pagination */ $pagination) |
||
60 | { |
||
61 | if ($resourceMetadataFactory instanceof RequestStack && $pagination instanceof ResourceMetadataFactoryInterface) { |
||
62 | @trigger_error(sprintf('Passing an instance of "%s" as second argument of "%s" is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "%s" instead.', RequestStack::class, self::class, ResourceMetadataFactoryInterface::class), E_USER_DEPRECATED); |
||
63 | @trigger_error(sprintf('Passing an instance of "%s" as third argument of "%s" is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "%s" instead.', ResourceMetadataFactoryInterface::class, self::class, Pagination::class), E_USER_DEPRECATED); |
||
64 | |||
65 | $this->requestStack = $resourceMetadataFactory; |
||
66 | $resourceMetadataFactory = $pagination; |
||
67 | $pagination = null; |
||
68 | |||
69 | $args = \array_slice(\func_get_args(), 3); |
||
70 | $legacyPaginationArgs = [ |
||
71 | ['arg_name' => 'enabled', 'type' => 'bool', 'default' => true], |
||
72 | ['arg_name' => 'clientEnabled', 'type' => 'bool', 'default' => false], |
||
73 | ['arg_name' => 'clientItemsPerPage', 'type' => 'bool', 'default' => false], |
||
74 | ['arg_name' => 'itemsPerPage', 'type' => 'int', 'default' => 30], |
||
75 | ['arg_name' => 'pageParameterName', 'type' => 'string', 'default' => 'page'], |
||
76 | ['arg_name' => 'enabledParameterName', 'type' => 'string', 'default' => 'pagination'], |
||
77 | ['arg_name' => 'itemsPerPageParameterName', 'type' => 'string', 'default' => 'itemsPerPage'], |
||
78 | ['arg_name' => 'maximumItemPerPage', 'type' => 'int', 'default' => null], |
||
79 | ['arg_name' => 'partial', 'type' => 'bool', 'default' => false], |
||
80 | ['arg_name' => 'clientPartial', 'type' => 'bool', 'default' => false], |
||
81 | ['arg_name' => 'partialParameterName', 'type' => 'string', 'default' => 'partial'], |
||
82 | ]; |
||
83 | |||
84 | foreach ($legacyPaginationArgs as $pos => $arg) { |
||
85 | if (\array_key_exists($pos, $args)) { |
||
86 | @trigger_error(sprintf('Passing "$%s" arguments is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Pass an instance of "%s" as third argument instead.', implode('", "$', array_column($legacyPaginationArgs, 'arg_name')), Paginator::class), E_USER_DEPRECATED); |
||
87 | |||
88 | if (!((null === $arg['default'] && null === $args[$pos]) || \call_user_func("is_{$arg['type']}", $args[$pos]))) { |
||
89 | throw new InvalidArgumentException(sprintf('The "$%s" argument is expected to be a %s%s.', $arg['arg_name'], $arg['type'], null === $arg['default'] ? ' or null' : '')); |
||
90 | } |
||
91 | |||
92 | $value = $args[$pos]; |
||
93 | } else { |
||
94 | $value = $arg['default']; |
||
95 | } |
||
96 | |||
97 | $this->{$arg['arg_name']} = $value; |
||
98 | } |
||
99 | } elseif (!$resourceMetadataFactory instanceof ResourceMetadataFactoryInterface) { |
||
100 | throw new InvalidArgumentException(sprintf('The "$resourceMetadataFactory" argument is expected to be an implementation of the "%s" interface.', ResourceMetadataFactoryInterface::class)); |
||
101 | } elseif (!$pagination instanceof Pagination) { |
||
102 | throw new InvalidArgumentException(sprintf('The "$pagination" argument is expected to be an instance of the "%s" class.', Pagination::class)); |
||
103 | } |
||
104 | |||
105 | $this->managerRegistry = $managerRegistry; |
||
106 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
107 | $this->pagination = $pagination; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) |
||
114 | { |
||
115 | if (null === $pagination = $this->getPagination($queryBuilder, $resourceClass, $operationName, $context)) { |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | [$offset, $limit] = $pagination; |
||
120 | |||
121 | $queryBuilder |
||
122 | ->setFirstResult($offset) |
||
123 | ->setMaxResults($limit); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool |
||
130 | { |
||
131 | if (null === $this->requestStack) { |
||
132 | return $this->pagination->isEnabled($resourceClass, $operationName, $context); |
||
133 | } |
||
134 | |||
135 | if (null === $request = $this->requestStack->getCurrentRequest()) { |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | return $this->isPaginationEnabled($request, $this->resourceMetadataFactory->create($resourceClass), $operationName); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function getResult(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null, array $context = []) |
||
146 | { |
||
147 | $query = $queryBuilder->getQuery(); |
||
148 | |||
149 | // Only one alias, without joins, disable the DISTINCT on the COUNT |
||
150 | if (1 === \count($queryBuilder->getAllAliases())) { |
||
151 | $query->setHint(CountWalker::HINT_DISTINCT, false); |
||
152 | } |
||
153 | |||
154 | $doctrineOrmPaginator = new DoctrineOrmPaginator($query, $this->shouldDoctrinePaginatorFetchJoinCollection($queryBuilder, $resourceClass, $operationName)); |
||
155 | $doctrineOrmPaginator->setUseOutputWalkers($this->shouldDoctrinePaginatorUseOutputWalkers($queryBuilder, $resourceClass, $operationName)); |
||
156 | |||
157 | if (null === $this->requestStack) { |
||
158 | $isPartialEnabled = $this->pagination->isPartialEnabled($resourceClass, $operationName, $context); |
||
159 | } else { |
||
160 | $isPartialEnabled = $this->isPartialPaginationEnabled( |
||
161 | $this->requestStack->getCurrentRequest(), |
||
162 | null === $resourceClass ? null : $this->resourceMetadataFactory->create($resourceClass), |
||
163 | $operationName |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | if ($isPartialEnabled) { |
||
168 | return new class($doctrineOrmPaginator) extends AbstractPaginator { |
||
169 | }; |
||
170 | } |
||
171 | |||
172 | return new Paginator($doctrineOrmPaginator); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @throws InvalidArgumentException |
||
177 | */ |
||
178 | private function getPagination(QueryBuilder $queryBuilder, string $resourceClass, ?string $operationName, array $context): ?array |
||
179 | { |
||
180 | $request = null; |
||
181 | if (null !== $this->requestStack && null === $request = $this->requestStack->getCurrentRequest()) { |
||
182 | return null; |
||
183 | } |
||
184 | |||
185 | if (null === $request) { |
||
186 | if (!$this->pagination->isEnabled($resourceClass, $operationName, $context)) { |
||
187 | return null; |
||
188 | } |
||
189 | |||
190 | $context = $this->addCountToContext($queryBuilder, $context); |
||
191 | |||
192 | return \array_slice($this->pagination->getPagination($resourceClass, $operationName, $context), 1); |
||
193 | } |
||
194 | |||
195 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
196 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
197 | return null; |
||
198 | } |
||
199 | |||
200 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
201 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
202 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
203 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
204 | } |
||
205 | |||
206 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
207 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
208 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
209 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
210 | } |
||
211 | |||
212 | if (0 > $itemsPerPage) { |
||
213 | throw new InvalidArgumentException('Item per page parameter should not be less than 0'); |
||
214 | } |
||
215 | |||
216 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
217 | |||
218 | if (1 > $page) { |
||
219 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
220 | } |
||
221 | |||
222 | if (0 === $itemsPerPage && 1 < $page) { |
||
223 | throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0'); |
||
224 | } |
||
225 | |||
226 | $firstResult = ($page - 1) * $itemsPerPage; |
||
227 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
228 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
229 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
230 | $after = base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
231 | $firstResult = (int) $after; |
||
232 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
233 | } |
||
234 | } |
||
235 | |||
236 | return [$firstResult, $itemsPerPage]; |
||
237 | } |
||
238 | |||
239 | private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool |
||
257 | } |
||
258 | |||
259 | private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool |
||
260 | { |
||
269 | } |
||
270 | |||
271 | private function getPaginationParameter(Request $request, string $parameterName, $default = null) |
||
278 | } |
||
279 | |||
280 | private function addCountToContext(QueryBuilder $queryBuilder, array $context): array |
||
281 | { |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Determines the value of the $fetchJoinCollection argument passed to the Doctrine ORM Paginator. |
||
295 | */ |
||
296 | private function shouldDoctrinePaginatorFetchJoinCollection(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Determines whether the Doctrine ORM Paginator should use output walkers. |
||
326 | */ |
||
327 | private function shouldDoctrinePaginatorUseOutputWalkers(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
375 | } |
||
376 | } |
||
377 |