vendor/sylius/sylius/src/Sylius/Component/Currency/Context/CompositeCurrencyContext.php line 18

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\Currency\Context;
  12. use Laminas\Stdlib\PriorityQueue;
  13. final class CompositeCurrencyContext implements CurrencyContextInterface
  14. {
  15.     /**
  16.      * @var PriorityQueue|CurrencyContextInterface[]
  17.      *
  18.      * @psalm-var PriorityQueue<CurrencyContextInterface>
  19.      */
  20.     private PriorityQueue $currencyContexts;
  21.     public function __construct()
  22.     {
  23.         $this->currencyContexts = new PriorityQueue();
  24.     }
  25.     public function addContext(CurrencyContextInterface $currencyContextint $priority 0): void
  26.     {
  27.         $this->currencyContexts->insert($currencyContext$priority);
  28.     }
  29.     public function getCurrencyCode(): string
  30.     {
  31.         $lastException null;
  32.         foreach ($this->currencyContexts as $currencyContext) {
  33.             try {
  34.                 return $currencyContext->getCurrencyCode();
  35.             } catch (CurrencyNotFoundException $exception) {
  36.                 $lastException $exception;
  37.                 continue;
  38.             }
  39.         }
  40.         throw new CurrencyNotFoundException(null$lastException);
  41.     }
  42. }