| Total Complexity | 45 |
| Total Lines | 323 |
| 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) |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool |
||
| 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(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 | return \array_slice($this->pagination->getPagination($resourceClass, $operationName, $context), 1); |
||
| 191 | } |
||
| 192 | |||
| 193 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 194 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
| 195 | return null; |
||
| 196 | } |
||
| 197 | |||
| 198 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
| 199 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
| 200 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 201 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
| 205 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
| 206 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
| 207 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (0 > $itemsPerPage) { |
||
| 211 | throw new InvalidArgumentException('Item per page parameter should not be less than 0'); |
||
| 212 | } |
||
| 213 | |||
| 214 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
| 215 | |||
| 216 | if (1 > $page) { |
||
| 217 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
| 218 | } |
||
| 219 | |||
| 220 | if (0 === $itemsPerPage && 1 < $page) { |
||
| 221 | throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0'); |
||
| 222 | } |
||
| 223 | |||
| 224 | $firstResult = ($page - 1) * $itemsPerPage; |
||
| 225 | if ($request->attributes->getBoolean('_graphql', false)) { |
||
| 226 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 227 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
| 228 | $after = base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
| 229 | $firstResult = (int) $after; |
||
| 230 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | return [$firstResult, $itemsPerPage]; |
||
| 235 | } |
||
| 236 | |||
| 237 | private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool |
||
| 255 | } |
||
| 256 | |||
| 257 | private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool |
||
| 267 | } |
||
| 268 | |||
| 269 | private function getPaginationParameter(Request $request, string $parameterName, $default = null) |
||
| 270 | { |
||
| 271 | if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) { |
||
| 272 | return \array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Determines the value of the $fetchJoinCollection argument passed to the Doctrine ORM Paginator. |
||
| 280 | */ |
||
| 281 | private function shouldDoctrinePaginatorFetchJoinCollection(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Determines whether the Doctrine ORM Paginator should use output walkers. |
||
| 311 | */ |
||
| 312 | private function shouldDoctrinePaginatorUseOutputWalkers(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 360 | } |
||
| 361 | } |
||
| 362 |