| Total Complexity | 40 |
| Total Lines | 298 |
| 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(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 | return \array_slice($this->pagination->getPagination($resourceClass, $operationName, $context), 1); |
||
| 183 | } |
||
| 184 | |||
| 185 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 186 | if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
||
| 187 | return null; |
||
| 188 | } |
||
| 189 | |||
| 190 | $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
||
| 191 | if ($request->attributes->get('_graphql')) { |
||
| 192 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 193 | $itemsPerPage = $collectionArgs[$resourceClass]['first'] ?? $itemsPerPage; |
||
| 194 | } |
||
| 195 | |||
| 196 | if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
||
| 197 | $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $this->maximumItemPerPage, true); |
||
| 198 | $itemsPerPage = (int) $this->getPaginationParameter($request, $this->itemsPerPageParameterName, $itemsPerPage); |
||
| 199 | $itemsPerPage = (null !== $maxItemsPerPage && $itemsPerPage >= $maxItemsPerPage ? $maxItemsPerPage : $itemsPerPage); |
||
| 200 | } |
||
| 201 | |||
| 202 | if (0 > $itemsPerPage) { |
||
| 203 | throw new InvalidArgumentException('Item per page parameter should not be less than 0'); |
||
| 204 | } |
||
| 205 | |||
| 206 | $page = (int) $this->getPaginationParameter($request, $this->pageParameterName, 1); |
||
| 207 | |||
| 208 | if (1 > $page) { |
||
| 209 | throw new InvalidArgumentException('Page should not be less than 1'); |
||
| 210 | } |
||
| 211 | |||
| 212 | if (0 === $itemsPerPage && 1 < $page) { |
||
| 213 | throw new InvalidArgumentException('Page should not be greater than 1 if itemsPerPage is equal to 0'); |
||
| 214 | } |
||
| 215 | |||
| 216 | $firstResult = ($page - 1) * $itemsPerPage; |
||
| 217 | if ($request->attributes->get('_graphql')) { |
||
| 218 | $collectionArgs = $request->attributes->get('_graphql_collections_args', []); |
||
| 219 | if (isset($collectionArgs[$resourceClass]['after'])) { |
||
| 220 | $after = base64_decode($collectionArgs[$resourceClass]['after'], true); |
||
| 221 | $firstResult = (int) $after; |
||
| 222 | $firstResult = false === $after ? $firstResult : ++$firstResult; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | return [$firstResult, $itemsPerPage]; |
||
| 227 | } |
||
| 228 | |||
| 229 | private function isPartialPaginationEnabled(Request $request = null, ResourceMetadata $resourceMetadata = null, string $operationName = null): bool |
||
| 247 | } |
||
| 248 | |||
| 249 | private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool |
||
| 250 | { |
||
| 251 | $enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true); |
||
| 252 | $clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true); |
||
| 253 | |||
| 254 | if ($clientEnabled) { |
||
| 255 | $enabled = filter_var($this->getPaginationParameter($request, $this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $enabled; |
||
| 259 | } |
||
| 260 | |||
| 261 | private function getPaginationParameter(Request $request, string $parameterName, $default = null) |
||
| 262 | { |
||
| 263 | if (null !== $paginationAttribute = $request->attributes->get('_api_pagination')) { |
||
| 264 | return array_key_exists($parameterName, $paginationAttribute) ? $paginationAttribute[$parameterName] : $default; |
||
| 265 | } |
||
| 266 | |||
| 267 | return $request->query->get($parameterName, $default); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Determines whether the Paginator should fetch join collections, if the root entity uses composite identifiers it should not. |
||
| 272 | * |
||
| 273 | * @see https://github.com/doctrine/doctrine2/issues/2910 |
||
| 274 | */ |
||
| 275 | private function useFetchJoinCollection(QueryBuilder $queryBuilder, string $resourceClass = null, string $operationName = null): bool |
||
| 276 | { |
||
| 277 | if (QueryChecker::hasRootEntityWithCompositeIdentifier($queryBuilder, $this->managerRegistry)) { |
||
| 278 | return false; |
||
| 279 | } |
||
| 280 | |||
| 281 | if (null === $resourceClass) { |
||
| 282 | return true; |
||
| 283 | } |
||
| 284 | |||
| 285 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
||
| 286 | |||
| 287 | return $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_fetch_join_collection', true, true); |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Determines whether output walkers should be used. |
||
| 292 | */ |
||
| 293 | private function useOutputWalkers(QueryBuilder $queryBuilder): bool |
||
| 334 | } |
||
| 335 | } |
||
| 336 |