vendor/ibexa/product-catalog/src/lib/Local/Repository/Region/RegionProvider.php line 43

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\ProductCatalog\Local\Repository\Region;
  8. use Ibexa\Contracts\ProductCatalog\Values\RegionInterface;
  9. use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
  10. use Ibexa\Core\MVC\Symfony\MVCEvents;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. final class RegionProvider implements EventSubscriberInterface, RegionProviderInterface
  13. {
  14. private RegionPoolFactoryInterface $factory;
  15. private ?RegionPoolInterface $pool = null;
  16. public function __construct(RegionPoolFactoryInterface $factory)
  17. {
  18. $this->factory = $factory;
  19. }
  20. /**
  21. * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
  22. */
  23. public function getRegion(string $identifier): RegionInterface
  24. {
  25. return $this->getPool()->getRegion($identifier);
  26. }
  27. /**
  28. * @return iterable<array-key,\Ibexa\Contracts\ProductCatalog\Values\RegionInterface>
  29. */
  30. public function getRegions(): iterable
  31. {
  32. return $this->getPool()->getRegions();
  33. }
  34. public function onScopeChange(ScopeChangeEvent $event): void
  35. {
  36. $this->pool = null;
  37. }
  38. private function getPool(): RegionPoolInterface
  39. {
  40. if ($this->pool === null) {
  41. $this->pool = $this->factory->createPool();
  42. }
  43. return $this->pool;
  44. }
  45. public static function getSubscribedEvents(): array
  46. {
  47. return [
  48. MVCEvents::CONFIG_SCOPE_CHANGE => 'onScopeChange',
  49. ];
  50. }
  51. }