| Total Complexity | 51 |
| Total Lines | 352 |
| 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 | /** |
||
| 42 | * @var ResourceMetadataFactoryInterface |
||
| 43 | */ |
||
| 44 | private $resourceMetadataFactory; |
||
| 45 | private $enabled; |
||
| 46 | private $clientEnabled; |
||
| 47 | private $clientItemsPerPage; |
||
| 48 | private $itemsPerPage; |
||
| 49 | private $pageParameterName; |
||
| 50 | private $enabledParameterName; |
||
| 51 | private $itemsPerPageParameterName; |
||
| 52 | private $maximumItemPerPage; |
||
| 53 | private $partial; |
||
| 54 | private $clientPartial; |
||
| 55 | private $partialParameterName; |
||
| 56 | /** |
||
| 57 | * @var Pagination|null |
||
| 58 | */ |
||
| 59 | private $pagination; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param ResourceMetadataFactoryInterface|RequestStack $resourceMetadataFactory |
||
| 63 | * @param Pagination|ResourceMetadataFactoryInterface $pagination |
||
| 64 | */ |
||
| 65 | public function __construct(ManagerRegistry $managerRegistry, /* ResourceMetadataFactoryInterface */ $resourceMetadataFactory, /* Pagination */ $pagination) |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool |
||
| 136 | { |
||
| 137 | if ($context['graphql_operation_name'] ?? false) { |
||
| 138 | return $this->pagination->isGraphQlEnabled($resourceClass, $operationName, $context); |
||
|
|
|||
| 139 | } |
||
| 140 | |||
| 141 | if (null === $this->requestStack) { |
||
| 142 | return $this->pagination->isEnabled($resourceClass, $operationName, $context); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (null === $request = $this->requestStack->getCurrentRequest()) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->isPaginationEnabled($request, $this->resourceMetadataFactory->create($resourceClass), $operationName); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function getResult(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null, array $context = []) |
||
| 156 | { |
||
| 157 | $query = $queryBuilder->getQuery(); |
||
| 158 | |||
| 159 | // Only one alias, without joins, disable the DISTINCT on the COUNT |
||
| 160 | if (1 === \count($queryBuilder->getAllAliases())) { |
||
| 161 | $query->setHint(CountWalker::HINT_DISTINCT, false); |
||
| 162 | } |
||
| 163 | |||
| 164 | $doctrineOrmPaginator = new DoctrineOrmPaginator($query, $this->shouldDoctrinePaginatorFetchJoinCollection($queryBuilder, $resourceClass, $operationName)); |
||
| 165 | $doctrineOrmPaginator->setUseOutputWalkers($this->shouldDoctrinePaginatorUseOutputWalkers($queryBuilder, $resourceClass, $operationName)); |
||
| 166 | |||
| 167 | if (null === $this->requestStack) { |
||
| 168 | $isPartialEnabled = $this->pagination->isPartialEnabled($resourceClass, $operationName, $context); |
||
| 169 | } else { |
||
| 170 | $isPartialEnabled = $this->isPartialPaginationEnabled( |
||
| 171 | $this->requestStack->getCurrentRequest(), |
||
| 172 | null === $resourceClass ? null : $this->resourceMetadataFactory->create($resourceClass), |
||
| 173 | $operationName |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | if ($isPartialEnabled) { |
||
| 178 | return new class($doctrineOrmPaginator) extends AbstractPaginator { |
||
| 179 | }; |
||
| 180 | } |
||
| 181 | |||
| 182 | return new Paginator($doctrineOrmPaginator); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @throws InvalidArgumentException |
||
| 187 | */ |
||
| 188 | private function getPagination(QueryBuilder $queryBuilder, string $resourceClass, ?string $operationName, array $context): ?array |
||
| 189 | { |
||
| 190 | $request = null; |
||
| 191 | if (null !== $this->requestStack && null === $request = $this->requestStack->getCurrentRequest()) { |
||
| 192 | return null; |
||
| 193 | } |
||
| 194 | |||
| 195 | if (null === $request) { |
||
| 196 | if (!$this->pagination->isEnabled($resourceClass, $operationName, $context)) { |
||
| 197 | return null; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (($context['graphql_operation_name'] ?? false) && !$this->pagination->isGraphQlEnabled($resourceClass, $operationName, $context)) { |
||
| 201 | return null; |
||
| 202 | } |
||
| 203 | |||
| 204 | $context = $this->addCountToContext($queryBuilder, $context); |
||
| 205 | |||
| 206 | return \array_slice($this->pagination->getPagination($resourceClass, $operationName, $context), 1); |
||
| 207 | } |
||
| 208 | |||
| 209 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 210 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | |||
| 214 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
| 215 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
| 216 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 217 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
| 221 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
| 222 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
| 223 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
| 224 | } |
||
| 225 | |||
| 226 | if (0 > $itemsPerPage) { |
||
| 227 | throw new InvalidArgumentException('Item per page parameter should not be less than 0'); |
||
| 228 | } |
||
| 229 | |||
| 230 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
| 231 | |||
| 232 | if (1 > $page) { |
||
| 233 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
| 234 | } |
||
| 235 | |||
| 236 | if (0 === $itemsPerPage && 1 < $page) { |
||
| 237 | throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0'); |
||
| 238 | } |
||
| 239 | |||
| 240 | $firstResult = ($page - 1) * $itemsPerPage; |
||
| 241 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
| 242 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 243 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
| 244 | $after = base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
| 245 | $firstResult = (int) $after; |
||
| 246 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | return [$firstResult, $itemsPerPage]; |
||
| 251 | } |
||
| 252 | |||
| 253 | private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool |
||
| 271 | } |
||
| 272 | |||
| 273 | private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool |
||
| 283 | } |
||
| 284 | |||
| 285 | private function getPaginationParameter(Request $request, string $parameterName, $default = null) |
||
| 286 | { |
||
| 287 | if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) { |
||
| 288 | return \array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default; |
||
| 289 | } |
||
| 290 | |||
| 291 | return $request->query->get($parameterName, $default); |
||
| 292 | } |
||
| 293 | |||
| 294 | private function addCountToContext(QueryBuilder $queryBuilder, array $context): array |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Determines the value of the $fetchJoinCollection argument passed to the Doctrine ORM Paginator. |
||
| 309 | */ |
||
| 310 | private function shouldDoctrinePaginatorFetchJoinCollection(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Determines whether the Doctrine ORM Paginator should use output walkers. |
||
| 340 | */ |
||
| 341 | private function shouldDoctrinePaginatorUseOutputWalkers(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 389 | } |
||
| 390 | } |
||
| 391 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.