public function fetchRate(CurrencyPair $currencyPair)
 {
     $providerIds = [(int) $this->mainProvider, (int) $this->secondProvider];
     /** @var CurrencyRateProvider[] $providers */
     $providers = CurrencyRateProvider::find()->where(['id' => $providerIds])->orderBy(['FIELD (`id`, ' . implode(',', $providerIds) . ')' => ''])->all();
     if (count($providers) !== 2) {
         throw new Exception('One of providers not found');
     }
     $rates = [];
     foreach ($providers as $provider) {
         try {
             $providerHandler = $provider->getImplementationInstance($this->httpAdapter);
             if ($providerHandler !== null) {
                 $swap = new Swap($providerHandler);
                 $rate = $swap->quote($currencyPair->getBaseCurrency() . '/' . $currencyPair->getQuoteCurrency())->getValue();
                 $rates[] = floatval($rate);
             } else {
                 throw new Exception('Provider "' . $provider->name . '" not found');
             }
         } catch (\Exception $e) {
             throw new Exception('One or more currency providers did not return result');
         }
     }
     $min = min($rates);
     $max = max($rates);
     return new Rate($max - $min >= $max * $this->criticalDifference / 100 ? $max : $rates[0], new \DateTime());
 }
 /**
  * @param CurrencyPair $currencyPair
  * @return Rate
  * @throws Exception
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $providers = [$this->mainProvider => CurrencyRateProvider::getByName($this->mainProvider), $this->secondProvider => CurrencyRateProvider::getByName($this->secondProvider)];
     if (count($providers) !== 2) {
         throw new Exception('One of providers not found');
     }
     $rates = [];
     /** @var CurrencyRateProvider $provider */
     foreach ($providers as $name => $provider) {
         if (null === $provider) {
             throw new Exception("Provider \"{$name}\" not found!");
         }
         try {
             $providerHandler = $provider->getImplementationInstance($this->httpAdapter);
             if ($providerHandler !== null) {
                 $swap = new Swap($providerHandler);
                 $rate = $swap->quote($currencyPair->getBaseCurrency() . '/' . $currencyPair->getQuoteCurrency())->getValue();
                 $rates[] = floatval($rate);
             } else {
                 throw new Exception('Provider "' . $provider->name . '" not found');
             }
         } catch (\Exception $e) {
             throw new Exception('One or more currency providers did not return result');
         }
     }
     $min = min($rates);
     $max = max($rates);
     return new Rate($max - $min >= $max * $this->criticalDifference / 100 ? $max : $rates[0], new \DateTime());
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency() . $currencyPair->getQuoteCurrency(), $this->token);
     $content = $this->fetchContent($url);
     $json = StringUtil::jsonToArray($content);
     $data = $json[0];
     if ('Success' === $data['Outcome']) {
         $dateString = $data['Date'] . ' ' . $data['Time'];
         return new Rate((string) $data['Bid'], \DateTime::createFromFormat('m/d/Y H:i:s A', $dateString, new \DateTimeZone('UTC')));
     }
     throw new Exception($data['Message']);
 }
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     if (!$this->enterprise && 'USD' !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     if ($this->enterprise) {
         $url = sprintf(self::ENTERPRISE_URL, $this->appId, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     } else {
         $url = sprintf(self::FREE_URL, $this->appId);
     }
     $content = $this->fetchContent($url);
     $data = StringUtil::jsonToArray($content);
     if (isset($data['error'])) {
         throw new Exception($data['description']);
     }
     $date = new \DateTime();
     $date->setTimestamp($data['timestamp']);
     if ($data['base'] === $currencyPair->getBaseCurrency() && isset($data['rates'][$currencyPair->getQuoteCurrency()])) {
         return new Rate((string) $data['rates'][$currencyPair->getQuoteCurrency()], $date);
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair, $date = null)
 {
     $getAppend = function ($from, $to, $date = null) {
         $date = isset($date) ? $date : 'latest';
         return "{$date}?symbols={$to}&base={$from}";
     };
     $url = self::URL . $getAppend($currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency(), $date);
     $content = $this->fetchContent($url);
     $return = StringUtil::jsonToArray($content);
     $return['date'] = isset($date) ? new \DateTime($date) : new \DateTime();
     return new Rate(current($return['rates']), $return['date']);
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
Пример #6
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $queryPairs = sprintf('"%s%s"', $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $query = sprintf('select * from yahoo.finance.xchange where pair in (%s)', $queryPairs);
     $url = sprintf(self::URL, urlencode($query));
     $content = $this->fetchContent($url);
     $json = StringUtil::jsonToArray($content);
     $data = $json['query']['results']['rate'];
     if ('0.00' === $data['Rate'] || 'N/A' === $data['Date']) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $dateString = $data['Date'] . ' ' . $data['Time'];
     $date = \DateTime::createFromFormat('m/d/Y H:ia', $dateString);
     return new Rate($data['Rate'], $date);
 }
Пример #7
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $content = $this->fetchContent($url);
     $document = new \DOMDocument();
     @$document->loadHTML($content);
     $xpath = new \DOMXPath($document);
     $nodes = $xpath->query('//span[@class="bld"]');
     if (0 === $nodes->length) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     $nodeContent = $nodes->item(0)->textContent;
     $bid = strstr($nodeContent, ' ', true);
     if (!is_numeric($bid)) {
         throw new Exception('The currency is not supported or Google changed the response format');
     }
     return new Rate($bid, new \DateTime());
 }
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     if ('EUR' !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $content = $this->fetchContent(self::URL);
     $xmlElement = StringUtil::xmlToElement($content);
     $cube = $xmlElement->Cube->Cube;
     $cubeAttributes = $cube->attributes();
     $date = new \DateTime((string) $cubeAttributes['time']);
     foreach ($cube->Cube as $cube) {
         $cubeAttributes = $cube->attributes();
         $cubeQuoteCurrency = (string) $cubeAttributes['currency'];
         if ($cubeQuoteCurrency === $currencyPair->getQuoteCurrency()) {
             return new Rate((string) $cubeAttributes['rate'], $date);
         }
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $content = $this->fetchContent(self::URL);
     $xmlElement = StringUtil::xmlToElement($content);
     $baseCurrency = (string) $xmlElement->Body->OrigCurrency;
     if ($baseCurrency !== $currencyPair->getBaseCurrency()) {
         throw new UnsupportedCurrencyPairException($currencyPair);
     }
     $cube = $xmlElement->Body->Cube;
     $cubeAttributes = $cube->attributes();
     $date = new \DateTime((string) $cubeAttributes['date']);
     foreach ($cube->Rate as $rate) {
         $rateAttributes = $rate->attributes();
         $rateQuoteCurrency = (string) $rateAttributes['currency'];
         if ($rateQuoteCurrency === $currencyPair->getQuoteCurrency()) {
             $rateValue = !empty($rateAttributes['multiplier']) ? (double) $rate / (int) $rateAttributes['multiplier'] : (double) $rate;
             return new Rate((string) $rateValue, $date);
         }
     }
     throw new UnsupportedCurrencyPairException($currencyPair);
 }
Пример #10
0
 public function fetchRate(CurrencyPair $currencyPair)
 {
     /** @var CurrencyRateProvider $provider */
     $provider = CurrencyRateProvider::findOne($this->provider);
     if ($provider === null) {
         throw new Exception('Provider not found');
     }
     try {
         $providerHandler = $provider->getImplementationInstance($this->httpAdapter);
         $swap = new Swap($providerHandler);
         $rate = $swap->quote($currencyPair->getBaseCurrency() . '/' . $currencyPair->getQuoteCurrency())->getValue();
         if ($this->marginMultiplier !== null && $this->marginMultiplier > 1) {
             $rate *= $this->marginMultiplier;
         }
         if ($this->fixedMargin !== null && $this->fixedMargin > 0) {
             $rate += $this->fixedMargin;
         }
     } catch (\Exception $e) {
         throw new Exception('Calculating error');
     }
     return new Rate($rate, new \DateTime());
 }
Пример #11
0
 /**
  * {@inheritdoc}
  */
 public function fetchRate(CurrencyPair $currencyPair)
 {
     $url = sprintf(self::URL, $currencyPair->getBaseCurrency(), $currencyPair->getQuoteCurrency());
     $content = $this->fetchContent($url);
     return new Rate((string) StringUtil::xmlToElement($content), new \DateTime());
 }