vendor/ibexa/product-catalog/src/lib/Money/DecimalMoneyFactory.php line 44

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\Money;
  8. use Ibexa\Contracts\Core\Repository\Iterator\BatchIterator;
  9. use Ibexa\Contracts\ProductCatalog\CurrencyServiceInterface;
  10. use Ibexa\Contracts\ProductCatalog\Iterator\BatchIteratorAdapter\CurrencyFetchAdapter;
  11. use Ibexa\Core\MVC\Symfony\MVCEvents;
  12. use Money\Currencies\CurrencyList;
  13. use Money\Formatter\DecimalMoneyFormatter;
  14. use Money\Parser\DecimalMoneyParser;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. final class DecimalMoneyFactory implements EventSubscriberInterface
  17. {
  18.     private const CURRENCIES_LOAD_BATCH_SIZE 200;
  19.     private CurrencyServiceInterface $currencyService;
  20.     private ?CurrencyList $currencies;
  21.     private ?DecimalMoneyParser $moneyParser;
  22.     private ?DecimalMoneyFormatter $moneyFormatter;
  23.     public function __construct(CurrencyServiceInterface $currencyService)
  24.     {
  25.         $this->currencyService $currencyService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             MVCEvents::CONFIG_SCOPE_CHANGE => 'reset',
  31.         ];
  32.     }
  33.     public function reset(): void
  34.     {
  35.         $this->moneyParser null;
  36.         $this->moneyFormatter null;
  37.         $this->currencies null;
  38.     }
  39.     public function getMoneyParser(): DecimalMoneyParser
  40.     {
  41.         if (!isset($this->moneyParser)) {
  42.             $currencies $this->loadCurrencies();
  43.             $this->moneyParser = new DecimalMoneyParser($currencies);
  44.         }
  45.         return $this->moneyParser;
  46.     }
  47.     public function getMoneyFormatter(): DecimalMoneyFormatter
  48.     {
  49.         if (!isset($this->moneyFormatter)) {
  50.             $currencies $this->loadCurrencies();
  51.             $this->moneyFormatter = new DecimalMoneyFormatter($currencies);
  52.         }
  53.         return $this->moneyFormatter;
  54.     }
  55.     private function loadCurrencies(): CurrencyList
  56.     {
  57.         if (!isset($this->currencies)) {
  58.             $currencyCodeToSubunitsMap = [];
  59.             /** @var iterable<\Ibexa\Contracts\ProductCatalog\Values\CurrencyInterface> $currencies */
  60.             $currencies = new BatchIterator(
  61.                 new CurrencyFetchAdapter($this->currencyService),
  62.                 self::CURRENCIES_LOAD_BATCH_SIZE
  63.             );
  64.             foreach ($currencies as $currency) {
  65.                 $currencyCodeToSubunitsMap[$currency->getCode()] = $currency->getSubUnits();
  66.             }
  67.             $this->currencies = new CurrencyList($currencyCodeToSubunitsMap);
  68.         }
  69.         return $this->currencies;
  70.     }
  71. }