/**
  * {@inheritdoc}
  */
 public function fetch($currencyCode, $rateType = 'default', \DateTime $date = null)
 {
     $currencyCode = CurrencyCodeUtil::clean($currencyCode);
     if (!Api::supports($currencyCode, $rateType)) {
         throw new \RuntimeException(sprintf('Banca Intesa Serbia does not supports currency code "%s" for rate type "%s".', $currencyCode, $rateType));
     }
     if ($date === null) {
         $date = new \DateTime('now');
     }
     if (!array_key_exists($rateType, $this->cache)) {
         try {
             $this->load($date);
         } catch (\Exception $e) {
             $message = sprintf('Unable to load data from "%s" for "%s" of rate type "%s".', $this->getName(), $currencyCode, $rateType);
             $this->getLogger()->emergency($message);
             throw new SourceNotAvailableException($message, 0, $e);
         }
     }
     if (array_key_exists($currencyCode, $this->cache[$rateType])) {
         return $this->cache[$rateType][$currencyCode];
     }
     $message = sprintf('API Changed: source "%s" does not provide currency code "%s" for rate type "%s".', $this->getName(), $currencyCode, $rateType);
     $this->getLogger()->critical($message);
     throw new \RuntimeException($message);
 }
Пример #2
0
 public function __construct($currencyCode, $rateType, $sourceName, array $extraConfiguration = array())
 {
     $this->currencyCode = CurrencyCodeUtil::clean($currencyCode);
     $this->rateType = $rateType;
     $this->sourceName = $sourceName;
     $this->extraConfiguration = $extraConfiguration;
 }
 /**
  * {@inheritdoc}
  */
 public function process($baseCurrencyCode, RatesConfigurationRegistryInterface $configurations, array $rates)
 {
     $baseCurrencyCode = CurrencyCodeUtil::clean($baseCurrencyCode);
     /**
      * @var RateInterface $rate
      */
     foreach ($rates as $rate) {
         if ($baseCurrencyCode !== $rate->getBaseCurrencyCode()) {
             $message = sprintf('Invalid base currency code "%s" of rate "%s" from source "%s" is not calculated.', $rate->getBaseCurrencyCode(), $rate->getCurrencyCode(), $rate->getSourceName());
             $this->getLogger()->critical($message);
             throw new ConfigurationException($message);
         }
     }
     return $rates;
 }
Пример #4
0
 public function __construct($sourceName, $value, $currencyCode, $rateType, $date, $baseCurrencyCode, $createdAt = null, $modifiedAt = null)
 {
     $this->sourceName = $sourceName;
     $this->value = $value;
     $this->currencyCode = CurrencyCodeUtil::clean($currencyCode);
     $this->rateType = $rateType;
     $this->baseCurrencyCode = CurrencyCodeUtil::clean($baseCurrencyCode);
     $processDate = function ($arg) {
         $arg = is_null($arg) ? new \DateTime('now') : $arg;
         return is_numeric($arg) ? date_timestamp_set(new \DateTime(), $arg) : clone $arg;
     };
     $this->date = $processDate($date);
     $this->createdAt = $processDate($createdAt);
     $this->modifiedAt = $processDate($modifiedAt);
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = 'default')
 {
     $currencyCode = CurrencyCodeUtil::clean($currencyCode);
     if ($this->has($sourceName, $currencyCode, $date, $rateType)) {
         return $this->get($sourceName, $currencyCode, $date, $rateType);
     }
     if ((int) $date->format('N') === 6 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P1D')), $rateType)) {
         return $this->get($sourceName, $currencyCode, $lastFriday, $rateType);
     } elseif ((int) $date->format('N') === 7 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P2D')), $rateType)) {
         return $this->get($sourceName, $currencyCode, $lastFriday, $rateType);
     }
     $message = sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for historical date "%s".', $currencyCode, $rateType, $sourceName, $date->format('Y-m-d'));
     $this->getLogger()->critical($message);
     throw new ExchangeRateException($message);
 }
 /**
  * @test
  *
  * @expectedException \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
  */
 public function invalid()
 {
     CurrencyCodeUtil::clean('non-existing');
 }
 /**
  * Configure base currency.
  *
  * @param array $config Configuration parameters.
  * @param ContainerBuilder $container Service container.
  * @return Extension $this Fluent interface.
  */
 protected function configureBaseCurrency(array $config, ContainerBuilder $container)
 {
     $baseCurrency = CurrencyCodeUtil::clean($config['base_currency']);
     $container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency);
     return $this;
 }