<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Bundle\Storefront\EventSubscriber;
use Ibexa\Contracts\ProductCatalog\Events\RegionResolveEvent;
use Ibexa\Contracts\Storefront\Repository\RegionServiceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class RegionResolverSubscriber implements EventSubscriberInterface
{
private RegionServiceInterface $regionService;
public function __construct(RegionServiceInterface $regionService)
{
$this->regionService = $regionService;
}
public static function getSubscribedEvents(): array
{
return [
RegionResolveEvent::class => ['onRegionResolve', -90],
];
}
public function onRegionResolve(RegionResolveEvent $event): void
{
if ($event->getRegion() !== null) {
return;
}
$region = $this->regionService->getSessionRegion();
if ($region !== null) {
$event->setRegion($region);
$event->stopPropagation();
}
}
}