vendor/ibexa/product-catalog/src/bundle/EventSubscriber/CurrencyResolverSubscriber.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\CurrencyServiceInterface;
  10. use Ibexa\Contracts\ProductCatalog\Events\CurrencyResolveEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class CurrencyResolverSubscriber implements EventSubscriberInterface
  13. {
  14.     private CurrencyServiceInterface $currencyService;
  15.     private ConfigResolverInterface $configResolver;
  16.     public function __construct(CurrencyServiceInterface $currencyServiceConfigResolverInterface $configResolver)
  17.     {
  18.         $this->currencyService $currencyService;
  19.         $this->configResolver $configResolver;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CurrencyResolveEvent::class => ['onCurrencyResolve', -100],
  25.         ];
  26.     }
  27.     public function onCurrencyResolve(CurrencyResolveEvent $event): void
  28.     {
  29.         $code $this->resolveCurrencyCode();
  30.         if ($code !== null) {
  31.             $event->setCurrency($this->currencyService->getCurrencyByCode($code));
  32.         }
  33.     }
  34.     private function resolveCurrencyCode(): ?string
  35.     {
  36.         $currencies $this->configResolver->getParameter('product_catalog.currencies');
  37.         if (!empty($currencies)) {
  38.             return reset($currencies);
  39.         }
  40.         return null;
  41.     }
  42. }