vendor/ibexa/product-catalog/src/bundle/EventSubscriber/RegionResolverSubscriber.php line 35

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. declare(strict_types=1);
  7. namespace Ibexa\Bundle\ProductCatalog\EventSubscriber;
  8. use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
  9. use Ibexa\Contracts\ProductCatalog\Events\RegionResolveEvent;
  10. use Ibexa\Contracts\ProductCatalog\RegionServiceInterface;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class RegionResolverSubscriber implements EventSubscriberInterface
  13. {
  14.     private RegionServiceInterface $regionService;
  15.     private ConfigResolverInterface $configResolver;
  16.     public function __construct(RegionServiceInterface $regionServiceConfigResolverInterface $configResolver)
  17.     {
  18.         $this->regionService $regionService;
  19.         $this->configResolver $configResolver;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             RegionResolveEvent::class => ['onRegionResolve', -100],
  25.         ];
  26.     }
  27.     public function onRegionResolve(RegionResolveEvent $event): void
  28.     {
  29.         $identifier $this->resolveRegionIdentifier();
  30.         if ($identifier !== null) {
  31.             $event->setRegion($this->regionService->getRegion($identifier));
  32.         }
  33.     }
  34.     private function resolveRegionIdentifier(): ?string
  35.     {
  36.         $regions $this->configResolver->getParameter('product_catalog.regions');
  37.         if (!empty($regions)) {
  38.             return reset($regions);
  39.         }
  40.         return null;
  41.     }
  42. }