vendor/ibexa/graphql/src/lib/DataLoader/SearchLocationLoader.php line 66

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\DataLoader;
  7. use Ibexa\Contracts\Core\Repository\Exceptions as ApiException;
  8. use Ibexa\Contracts\Core\Repository\LocationService;
  9. use Ibexa\Contracts\Core\Repository\SearchService;
  10. use Ibexa\Contracts\Core\Repository\URLAliasService;
  11. use Ibexa\Contracts\Core\Repository\Values\Content\Location;
  12. use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
  13. use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchHit;
  14. use Ibexa\Contracts\Core\Repository\Values\Content\URLAlias;
  15. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  16. use Ibexa\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator;
  17. use Ibexa\GraphQL\DataLoader\Exception\ArgumentsException;
  18. /**
  19.  * @internal
  20.  */
  21. class SearchLocationLoader implements LocationLoader
  22. {
  23.     /**
  24.      * @var \Ibexa\Contracts\Core\Repository\SearchService
  25.      */
  26.     private $searchService;
  27.     /**
  28.      * @var \Ibexa\Contracts\Core\Repository\LocationService
  29.      */
  30.     private $locationService;
  31.     /**
  32.      * @var \Ibexa\Contracts\Core\Repository\URLAliasService
  33.      */
  34.     private $urlAliasService;
  35.     /**
  36.      * @var \Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface
  37.      */
  38.     private $configResolver;
  39.     /**
  40.      * @var \Ibexa\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator
  41.      */
  42.     private $urlAliasGenerator;
  43.     public function __construct(SearchService $searchServiceLocationService $locationServiceURLAliasService $urlAliasServiceConfigResolverInterface $configResolverUrlAliasGenerator $urlAliasGenerator)
  44.     {
  45.         $this->searchService $searchService;
  46.         $this->locationService $locationService;
  47.         $this->urlAliasService $urlAliasService;
  48.         $this->configResolver $configResolver;
  49.         $this->urlAliasGenerator $urlAliasGenerator;
  50.     }
  51.     public function find(LocationQuery $query): array
  52.     {
  53.         return array_map(
  54.             static function (SearchHit $searchHit) {
  55.                 return $searchHit->valueObject;
  56.             },
  57.             $this->searchService->findLocations($query)->searchHits
  58.         );
  59.     }
  60.     public function findById($id): Location
  61.     {
  62.         try {
  63.             return $this->locationService->loadLocation($id);
  64.         } catch (ApiException\InvalidArgumentException $e) {
  65.         } catch (ApiException\NotFoundException $e) {
  66.             throw new ArgumentsException($e->getMessage(), $e->getCode(), $e);
  67.         }
  68.     }
  69.     public function findByRemoteId($id): Location
  70.     {
  71.         try {
  72.             return $this->locationService->loadLocationByRemoteId($id);
  73.         } catch (ApiException\InvalidArgumentException $e) {
  74.         } catch (ApiException\NotFoundException $e) {
  75.             throw new ArgumentsException($e->getMessage(), $e->getCode(), $e);
  76.         }
  77.     }
  78.     public function findByUrlAlias(string $urlAlias): Location
  79.     {
  80.         $alias $this->getUrlAlias($urlAlias);
  81.         return ($alias->type == URLAlias::LOCATION)
  82.             ? $this->locationService->loadLocation($alias->destination)
  83.             : null;
  84.     }
  85.     /**
  86.      * Counts the results of a query.
  87.      *
  88.      * @param \Ibexa\Contracts\Core\Repository\Values\Content\Query $query
  89.      *
  90.      * @return int
  91.      *
  92.      * @throws \Ibexa\GraphQL\DataLoader\Exception\ArgumentsException
  93.      */
  94.     public function count(LocationQuery $query)
  95.     {
  96.         $countQuery = clone $query;
  97.         $countQuery->limit 0;
  98.         $countQuery->offset 0;
  99.         try {
  100.             return $this->searchService->findLocations($countQuery)->totalCount;
  101.         } catch (ApiException\InvalidArgumentException $e) {
  102.             throw new ArgumentsException($e->getMessage(), $e->getCode(), $e);
  103.         }
  104.     }
  105.     protected function getUrlAlias($pathinfo): URLAlias
  106.     {
  107.         $rootLocationId $this->configResolver->getParameter('content.tree_root.location_id');
  108.         $pathPrefix $this->urlAliasGenerator->getPathPrefixByRootLocationId($rootLocationId);
  109.         if (
  110.             $rootLocationId !== null &&
  111.             !$this->urlAliasGenerator->isUriPrefixExcluded($pathinfo) &&
  112.             $pathPrefix !== '/'
  113.         ) {
  114.             $urlAlias $pathPrefix $pathinfo;
  115.         } else {
  116.             $urlAlias $pathinfo;
  117.         }
  118.         return $this->urlAliasService->lookup($urlAlias);
  119.     }
  120. }
  121. class_alias(SearchLocationLoader::class, 'EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\SearchLocationLoader');