Exemplo n.º 1
0
 public function updateRates(Event $event)
 {
     if (null === ($defaultCurrency = CurrencyQuery::create()->findOneByByDefault(true))) {
         throw new \RuntimeException('Unable to find a default currency, please define a default currency.');
     }
     $defaultCurrency->setRate(1)->save();
     $currencies = CurrencyQuery::create()->filterByByDefault(false);
     $baseValue = new Number('1');
     /** @var \Thelia\Model\Currency $currency */
     foreach ($currencies as $currency) {
         $rate = $this->currencyConverter->from($defaultCurrency->getCode())->to($currency->getCode())->convert($baseValue);
         $currency->setRate($rate->getNumber(-1))->save();
     }
 }
Exemplo n.º 2
0
 public function updateRates(CurrencyUpdateRateEvent $event)
 {
     if (null === ($defaultCurrency = CurrencyQuery::create()->findOneByByDefault(true))) {
         throw new \RuntimeException('Unable to find a default currency, please define a default currency.');
     }
     $defaultCurrency->setRate(1)->save();
     $currencies = CurrencyQuery::create()->filterByByDefault(false);
     $baseValue = new Number('1');
     /** @var \Thelia\Model\Currency $currency */
     foreach ($currencies as $currency) {
         try {
             $rate = $this->currencyConverter->from($defaultCurrency->getCode())->to($currency->getCode())->convert($baseValue);
             $currency->setRate($rate->getNumber(-1))->save();
         } catch (CurrencyNotFoundException $ex) {
             Tlog::getInstance()->addError(sprintf("Unable to find exchange rate for currency %s, ID %d", $currency->getCode(), $currency->getId()));
             $event->addUndefinedRate($currency->getId());
         }
     }
 }
 /**
  * @covers \Thelia\CurrencyConverter\CurrencyConverter::convert
  * @covers \Thelia\CurrencyConverter\CurrencyConverter::validate
  * @covers \Thelia\CurrencyConverter\CurrencyConverter::setProvider
  */
 public function testConvert()
 {
     $currencyConverter = new CurrencyConverter();
     $result = $currencyConverter->setProvider($this->getProvider())->from('EUR')->to('USD')->convert(new Number('1'));
     $this->assertInstanceOf('Thelia\\Math\\Number', $result, 'the converter must return an instance of \\Thelia\\Math\\Number');
 }