vendor/sylius/resource-bundle/src/Bundle/Controller/ResourcesCollectionProvider.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Controller;
  12. use Hateoas\Configuration\Route;
  13. use Hateoas\Representation\Factory\PagerfantaFactory;
  14. use Pagerfanta\Pagerfanta;
  15. use Sylius\Bundle\ResourceBundle\Grid\View\ResourceGridView;
  16. use Sylius\Component\Resource\Repository\RepositoryInterface;
  17. final class ResourcesCollectionProvider implements ResourcesCollectionProviderInterface
  18. {
  19. private ResourcesResolverInterface $resourcesResolver;
  20. private PagerfantaFactory $pagerfantaRepresentationFactory;
  21. public function __construct(ResourcesResolverInterface $resourcesResolver, PagerfantaFactory $pagerfantaRepresentationFactory)
  22. {
  23. $this->resourcesResolver = $resourcesResolver;
  24. $this->pagerfantaRepresentationFactory = $pagerfantaRepresentationFactory;
  25. }
  26. /**
  27. * @psalm-suppress MissingReturnType
  28. */
  29. public function get(RequestConfiguration $requestConfiguration, RepositoryInterface $repository)
  30. {
  31. $resources = $this->resourcesResolver->getResources($requestConfiguration, $repository);
  32. $paginationLimits = [];
  33. if ($resources instanceof ResourceGridView) {
  34. $paginator = $resources->getData();
  35. $paginationLimits = $resources->getDefinition()->getLimits();
  36. } else {
  37. $paginator = $resources;
  38. }
  39. if ($paginator instanceof Pagerfanta) {
  40. $request = $requestConfiguration->getRequest();
  41. $paginator->setMaxPerPage($this->resolveMaxPerPage(
  42. $request->query->has('limit') ? $request->query->getInt('limit') : null,
  43. $requestConfiguration->getPaginationMaxPerPage(),
  44. $paginationLimits,
  45. ));
  46. $currentPage = (int) $request->query->get('page', '1');
  47. $paginator->setCurrentPage($currentPage);
  48. // This prevents Pagerfanta from querying database from a template
  49. $paginator->getCurrentPageResults();
  50. if (!$requestConfiguration->isHtmlRequest()) {
  51. $route = new Route($request->attributes->get('_route'), array_merge($request->attributes->get('_route_params'), $request->query->all()));
  52. return $this->pagerfantaRepresentationFactory->createRepresentation($paginator, $route);
  53. }
  54. }
  55. return $resources;
  56. }
  57. /**
  58. * @param int[] $gridLimits
  59. */
  60. private function resolveMaxPerPage(?int $requestLimit, int $configurationLimit, array $gridLimits = []): int
  61. {
  62. if (null === $requestLimit) {
  63. return reset($gridLimits) ?: $configurationLimit;
  64. }
  65. if (!empty($gridLimits)) {
  66. $maxGridLimit = max($gridLimits);
  67. return $requestLimit > $maxGridLimit ? $maxGridLimit : $requestLimit;
  68. }
  69. return $requestLimit;
  70. }
  71. }