| Total Complexity | 44 |
| Total Lines | 313 |
| 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 |
||
| 36 | final class PaginationExtension implements ContextAwareQueryResultCollectionExtensionInterface |
||
| 37 | { |
||
| 38 | private $managerRegistry; |
||
| 39 | private $requestStack; |
||
| 40 | private $resourceMetadataFactory; |
||
| 41 | private $enabled; |
||
| 42 | private $clientEnabled; |
||
| 43 | private $clientItemsPerPage; |
||
| 44 | private $itemsPerPage; |
||
| 45 | private $pageParameterName; |
||
| 46 | private $enabledParameterName; |
||
| 47 | private $itemsPerPageParameterName; |
||
| 48 | private $maximumItemPerPage; |
||
| 49 | private $partial; |
||
| 50 | private $clientPartial; |
||
| 51 | private $partialParameterName; |
||
| 52 | private $pagination; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param ResourceMetadataFactoryInterface|RequestStack $resourceMetadataFactory |
||
| 56 | * @param Pagination|ResourceMetadataFactoryInterface $pagination |
||
| 57 | */ |
||
| 58 | public function __construct(ManagerRegistry $managerRegistry, /* ResourceMetadataFactoryInterface */ $resourceMetadataFactory, /* Pagination */ $pagination) |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * {@inheritdoc} |
||
| 111 | */ |
||
| 112 | public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null, array $context = []) |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool |
||
| 129 | { |
||
| 130 | if (null === $this->requestStack) { |
||
| 131 | return $this->pagination->isEnabled($resourceClass, $operationName, $context); |
||
| 132 | } |
||
| 133 | |||
| 134 | if (null === $request = $this->requestStack->getCurrentRequest()) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | return $this->isPaginationEnabled($request, $this->resourceMetadataFactory->create($resourceClass), $operationName); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function getResult(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null, array $context = []) |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @throws InvalidArgumentException |
||
| 169 | */ |
||
| 170 | private function getPagination(QueryBuilder $queryBuilder, string $resourceClass, ?string $operationName, array $context): ?array |
||
| 171 | { |
||
| 172 | $request = null; |
||
| 173 | if (null !== $this->requestStack && null === $request = $this->requestStack->getCurrentRequest()) { |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (null === $request) { |
||
| 178 | if (!$this->pagination->isEnabled($resourceClass, $operationName, $context)) { |
||
| 179 | return null; |
||
| 180 | } |
||
| 181 | |||
| 182 | $context = $this->addCountToContext($queryBuilder, $context); |
||
| 183 | |||
| 184 | return \array_slice($this->pagination->getPagination($resourceClass, $operationName, $context), 1); |
||
| 185 | } |
||
| 186 | |||
| 187 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 188 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
| 189 | return null; |
||
| 190 | } |
||
| 191 | |||
| 192 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
| 193 | if ($request->attributes->get('_graphql')) { |
||
| 194 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 195 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
| 199 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
| 200 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
| 201 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
| 202 | } |
||
| 203 | |||
| 204 | if (0 > $itemsPerPage) { |
||
| 205 | throw new InvalidArgumentException('Item per page parameter should not be less than 0'); |
||
| 206 | } |
||
| 207 | |||
| 208 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
| 209 | |||
| 210 | if (1 > $page) { |
||
| 211 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
| 212 | } |
||
| 213 | |||
| 214 | if (0 === $itemsPerPage && 1 < $page) { |
||
| 215 | throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0'); |
||
| 216 | } |
||
| 217 | |||
| 218 | $firstResult = ($page - 1) * $itemsPerPage; |
||
| 219 | if ($request->attributes->get('_graphql')) { |
||
| 220 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 221 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
| 222 | $after = base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
| 223 | $firstResult = (int) $after; |
||
| 224 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | return [$firstResult, $itemsPerPage]; |
||
| 229 | } |
||
| 230 | |||
| 231 | private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool |
||
| 249 | } |
||
| 250 | |||
| 251 | private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool |
||
| 252 | { |
||
| 253 | $enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true); |
||
| 254 | $clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true); |
||
| 255 | |||
| 256 | if ($clientEnabled) { |
||
| 257 | $enabled = filter_var($this->getPaginationParameter($request, $this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN); |
||
| 258 | } |
||
| 259 | |||
| 260 | return $enabled; |
||
| 261 | } |
||
| 262 | |||
| 263 | private function getPaginationParameter(Request $request, string $parameterName, $default = null) |
||
| 264 | { |
||
| 265 | if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) { |
||
| 266 | return \array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default; |
||
| 267 | } |
||
| 268 | |||
| 269 | return $request->query->get($parameterName, $default); |
||
| 270 | } |
||
| 271 | |||
| 272 | private function addCountToContext(QueryBuilder $queryBuilder, array $context): array |
||
| 273 | { |
||
| 274 | if (!($context['graphql'] ?? false)) { |
||
| 275 | return $context; |
||
| 276 | } |
||
| 277 | |||
| 278 | if (isset($context['filters']['last']) && !isset($context['filters']['before'])) { |
||
| 279 | $context['count'] = (new DoctrineOrmPaginator($queryBuilder))->count(); |
||
| 280 | } |
||
| 281 | |||
| 282 | return $context; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Determines whether the Paginator should fetch join collections, if the root entity uses composite identifiers it should not. |
||
| 287 | * |
||
| 288 | * @see https://github.com/doctrine/doctrine2/issues/2910 |
||
| 289 | */ |
||
| 290 | private function useFetchJoinCollection(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 291 | { |
||
| 292 | if (QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder, $this->managerRegistry)) { |
||
| 293 | return false; |
||
| 294 | } |
||
| 295 | |||
| 296 | if (null === $resourceClass) { |
||
| 297 | return true; |
||
| 298 | } |
||
| 299 | |||
| 300 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 301 | |||
| 302 | return $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_fetch_join_collection', true, true); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Determines whether output walkers should be used. |
||
| 307 | */ |
||
| 308 | private function useOutputWalkers(QueryBuilder $queryBuilder): bool |
||
| 349 | } |
||
| 350 | } |
||
| 351 |