vendor/ibexa/graphql/src/lib/Resolver/LocationResolver.php line 112

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. namespace Ibexa\GraphQL\Resolver;
  7. use Ibexa\Contracts\Core\Repository\ContentService;
  8. use Ibexa\Contracts\Core\Repository\LocationService;
  9. use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
  10. use Ibexa\Contracts\Core\Repository\Values\Content\Query;
  11. use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
  12. use Ibexa\GraphQL\DataLoader\Exception\ArgumentsException;
  13. use Ibexa\GraphQL\DataLoader\LocationLoader;
  14. use Ibexa\GraphQL\InputMapper\SearchQuerySortByMapper;
  15. use Ibexa\GraphQL\Relay\PageAwareConnection;
  16. use Overblog\GraphQLBundle\Definition\Argument;
  17. use Overblog\GraphQLBundle\Relay\Connection\Paginator;
  18. /**
  19.  * @internal
  20.  */
  21. class LocationResolver
  22. {
  23.     public const DEFAULT_LIMIT 10;
  24.     /**
  25.      * @var \Ibexa\Contracts\Core\Repository\LocationService
  26.      */
  27.     private $locationService;
  28.     /**
  29.      * @var \Ibexa\Contracts\Core\Repository\ContentService
  30.      */
  31.     private $contentService;
  32.     /**
  33.      * @var \Ibexa\GraphQL\DataLoader\LocationLoader
  34.      */
  35.     private $locationLoader;
  36.     /**
  37.      * @var \Ibexa\GraphQL\InputMapper\SearchQuerySortByMapper
  38.      */
  39.     private $sortMapper;
  40.     public function __construct(
  41.         LocationService $locationService,
  42.         ContentService $contentService,
  43.         LocationLoader $locationLoader,
  44.         SearchQuerySortByMapper $sortMapper
  45.     ) {
  46.         $this->locationService $locationService;
  47.         $this->contentService $contentService;
  48.         $this->locationLoader $locationLoader;
  49.         $this->sortMapper $sortMapper;
  50.     }
  51.     public function resolveLocation($args)
  52.     {
  53.         if (isset($args['locationId'])) {
  54.             return $this->locationLoader->findById($args['locationId']);
  55.         } elseif (isset($args['remoteId'])) {
  56.             return $this->locationLoader->findByRemoteId($args['remoteId']);
  57.         } elseif (isset($args['urlAlias'])) {
  58.             return $this->locationLoader->findByUrlAlias($args['urlAlias']);
  59.         }
  60.         throw new ArgumentsException('Requires one and only one of remoteId or locationId');
  61.     }
  62.     public function resolveLocationsByContentId($contentId)
  63.     {
  64.         return $this->locationService->loadLocations(
  65.             $this->contentService->loadContentInfo($contentId)
  66.         );
  67.     }
  68.     public function resolveLocationById($locationId)
  69.     {
  70.         return $this->locationService->loadLocation($locationId);
  71.     }
  72.     /**
  73.      * @param int $locationId
  74.      *
  75.      * @return \Overblog\GraphQLBundle\Relay\Connection\Output\Connection
  76.      */
  77.     public function resolveLocationChildren($locationIdArgument $args): PageAwareConnection
  78.     {
  79.         $args['locationId'] = $locationId;
  80.         $sortClauses = isset($args['sortBy']) ? $this->sortMapper->mapInputToSortClauses($args['sortBy']) : [];
  81.         $query = new LocationQuery([
  82.             'filter' => $this->buildFilter($args),
  83.             'sortClauses' => $sortClauses,
  84.         ]);
  85.         $paginator = new Paginator(function ($offset$limit) use ($query) {
  86.             $query->offset $offset;
  87.             $query->limit $limit ?? self::DEFAULT_LIMIT;
  88.             return $this->locationLoader->find($query);
  89.         });
  90.         return PageAwareConnection::fromConnection(
  91.             $paginator->auto(
  92.                 $args,
  93.                 function () use ($query) {
  94.                     return $this->locationLoader->count($query);
  95.                 }
  96.             ),
  97.             $args
  98.         );
  99.     }
  100.     private function buildFilter(Argument $args): Criterion
  101.     {
  102.         return new Query\Criterion\ParentLocationId($args['locationId']);
  103.     }
  104. }
  105. class_alias(LocationResolver::class, 'EzSystems\EzPlatformGraphQL\GraphQL\Resolver\LocationResolver');