vendor/tbbc/money-bundle/Pair/PairManager.php line 50

Open in your IDE?
  1. <?php
  2. namespace Tbbc\MoneyBundle\Pair;
  3. use Money\Converter;
  4. use Money\Currencies;
  5. use Money\Currencies\ISOCurrencies;
  6. use Money\Currency;
  7. use Money\CurrencyPair;
  8. use Money\Exchange;
  9. use Money\Money;
  10. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  11. use Tbbc\MoneyBundle\MoneyException;
  12. use Tbbc\MoneyBundle\TbbcMoneyEvents;
  13. /**
  14.  * Class PairManager
  15.  * @package Tbbc\MoneyBundle\Pair
  16.  * @author Philippe Le Van.
  17.  */
  18. class PairManager implements PairManagerInterfaceExchange
  19. {
  20.     /** @var  StorageInterface */
  21.     protected $storage;
  22.     /** @var  array */
  23.     protected $currencyCodeList;
  24.     /** @var  string */
  25.     protected $referenceCurrencyCode;
  26.     /** @var  RatioProviderInterface */
  27.     protected $ratioProvider;
  28.     /** @var EventDispatcherInterface  */
  29.     protected $dispatcher;
  30.     /**
  31.      * @var Currencies
  32.      */
  33.     protected $currencies;
  34.     /**
  35.      * PairManager constructor.
  36.      *
  37.      * @param StorageInterface         $storage
  38.      * @param array                    $currencyCodeList
  39.      * @param string                   $referenceCurrencyCode
  40.      * @param EventDispatcherInterface $dispatcher
  41.      */
  42.     public function __construct(
  43.         StorageInterface $storage,
  44.         $currencyCodeList,
  45.         $referenceCurrencyCode,
  46.         EventDispatcherInterface $dispatcher
  47.     ) {
  48.         $this->storage $storage;
  49.         $this->currencyCodeList $currencyCodeList;
  50.         $this->referenceCurrencyCode $referenceCurrencyCode;
  51.         $this->dispatcher $dispatcher;
  52.         $this->currencies = new ISOCurrencies();
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function convert(Money $amount$currencyCode)
  58.     {
  59.         $converter = new Converter($this->currencies$this);
  60.         return $converter->convert($amount, new Currency($currencyCode));
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function quote(Currency $baseCurrencyCurrency $counterCurrency)
  66.     {
  67.         $ratio $this->getRelativeRatio($baseCurrency->getCode(), $counterCurrency->getCode());
  68.         return new CurrencyPair($baseCurrency$counterCurrency$ratio);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function saveRatio($currencyCode$ratio)
  74.     {
  75.         $currency = new Currency($currencyCode);
  76.         // end of hack
  77.         $ratio floatval($ratio);
  78.         if ($ratio <= 0) {
  79.             throw new MoneyException('ratio has to be strictly positive');
  80.         }
  81.         $ratioList $this->storage->loadRatioList(true);
  82.         $ratioList[$currency->getCode()] = $ratio;
  83.         $ratioList[$this->getReferenceCurrencyCode()] = (float) 1;
  84.         $this->storage->saveRatioList($ratioList);
  85.         $savedAt = new \DateTime();
  86.         $event = new SaveRatioEvent(
  87.             $this->getReferenceCurrencyCode(),
  88.             $currencyCode,
  89.             $ratio,
  90.             $savedAt
  91.         );
  92.         $this->dispatcher->dispatch(TbbcMoneyEvents::AFTER_RATIO_SAVE$event);
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function getRelativeRatio($referenceCurrencyCode$currencyCode)
  98.     {
  99.         $currency = new Currency($currencyCode);
  100.         $referenceCurrency = new Currency($referenceCurrencyCode);
  101.         if ($currencyCode === $referenceCurrencyCode) {
  102.             return (float) 1;
  103.         }
  104.         $ratioList $this->storage->loadRatioList();
  105.         if (!array_key_exists($currency->getCode(), $ratioList)) {
  106.             throw new MoneyException('unknown ratio for currency '.$currencyCode);
  107.         }
  108.         if (!array_key_exists($referenceCurrency->getCode(), $ratioList)) {
  109.             throw new MoneyException('unknown ratio for currency '.$referenceCurrencyCode);
  110.         }
  111.         return $ratioList[$currency->getCode()] / $ratioList[$referenceCurrency->getCode()];
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function getCurrencyCodeList()
  117.     {
  118.         return $this->currencyCodeList;
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function getReferenceCurrencyCode()
  124.     {
  125.         return $this->referenceCurrencyCode;
  126.     }
  127.     /**
  128.      * {@inheritdoc}
  129.      */
  130.     public function getRatioList()
  131.     {
  132.         return $this->storage->loadRatioList();
  133.     }
  134.     /**
  135.      * {@inheritdoc}
  136.      */
  137.     public function setRatioProvider(RatioProviderInterface $ratioProvider)
  138.     {
  139.         $this->ratioProvider $ratioProvider;
  140.     }
  141.     /**
  142.      * {@inheritdoc}
  143.      */
  144.     public function saveRatioListFromRatioProvider()
  145.     {
  146.         if (!$this->ratioProvider) {
  147.             throw new MoneyException('no ratio provider defined');
  148.         }
  149.         foreach ($this->getCurrencyCodeList() as $currencyCode) {
  150.             if ($currencyCode != $this->getReferenceCurrencyCode()) {
  151.                 $ratio $this->ratioProvider->fetchRatio($this->getReferenceCurrencyCode(), $currencyCode);
  152.                 $this->saveRatio($currencyCode$ratio);
  153.             }
  154.         }
  155.     }
  156. }