|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Exchanger. |
| 7 | + * |
| 8 | + * (c) Florian Voutzinos <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Exchanger\Service; |
| 15 | + |
| 16 | +use Exchanger\Contract\CurrencyPair; |
| 17 | +use Exchanger\Contract\ExchangeRateQuery; |
| 18 | +use Exchanger\Contract\HistoricalExchangeRateQuery; |
| 19 | +use Exchanger\Exception\Exception; |
| 20 | +use Exchanger\Exception\UnsupportedCurrencyPairException; |
| 21 | +use Exchanger\ExchangeRate; |
| 22 | +use Exchanger\StringUtil; |
| 23 | +use Exchanger\Contract\ExchangeRate as ExchangeRateContract; |
| 24 | + |
| 25 | +/** |
| 26 | + * Exchangerate Service. |
| 27 | + * |
| 28 | + * @author Florian Voutzinos <[email protected]> |
| 29 | + */ |
| 30 | +final class ExchangerateHost extends HttpService |
| 31 | +{ |
| 32 | + use SupportsHistoricalQueries; |
| 33 | + |
| 34 | + const LATEST_URL = 'https://api.exchangerate.host/latest?base=%s&v=%s'; |
| 35 | + |
| 36 | + const HISTORICAL_URL = 'https://api.exchangerate.host/%s?base=%s'; |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritdoc} |
| 40 | + */ |
| 41 | + protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRateContract |
| 42 | + { |
| 43 | + $currencyPair = $exchangeQuery->getCurrencyPair(); |
| 44 | + |
| 45 | + $url = sprintf( |
| 46 | + self::LATEST_URL, |
| 47 | + $currencyPair->getBaseCurrency(), |
| 48 | + date('Y-m-d') |
| 49 | + ); |
| 50 | + |
| 51 | + return $this->doCreateRate($url, $currencyPair); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * {@inheritdoc} |
| 56 | + */ |
| 57 | + protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRateContract |
| 58 | + { |
| 59 | + $currencyPair = $exchangeQuery->getCurrencyPair(); |
| 60 | + |
| 61 | + $url = sprintf( |
| 62 | + self::HISTORICAL_URL, |
| 63 | + $exchangeQuery->getDate()->format('Y-m-d'), |
| 64 | + $exchangeQuery->getCurrencyPair()->getBaseCurrency() |
| 65 | + ); |
| 66 | + |
| 67 | + return $this->doCreateRate($url, $currencyPair); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + public function supportQuery(ExchangeRateQuery $exchangeQuery): bool |
| 74 | + { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Creates a rate. |
| 80 | + * |
| 81 | + * @param string $url |
| 82 | + * @param CurrencyPair $currencyPair |
| 83 | + * |
| 84 | + * @return ExchangeRate |
| 85 | + * |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | + private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate |
| 89 | + { |
| 90 | + $content = $this->request($url); |
| 91 | + $data = StringUtil::jsonToArray($content); |
| 92 | + |
| 93 | + if (false === $data['success']) { |
| 94 | + throw new Exception(); |
| 95 | + } |
| 96 | + |
| 97 | + if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) { |
| 98 | + $date = new \DateTime($data['date']); |
| 99 | + $rate = $data['rates'][$currencyPair->getQuoteCurrency()]; |
| 100 | + |
| 101 | + return $this->createRate($currencyPair, (float) $rate, $date); |
| 102 | + } |
| 103 | + |
| 104 | + throw new UnsupportedCurrencyPairException($currencyPair, $this); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * {@inheritdoc} |
| 109 | + */ |
| 110 | + public function getName(): string |
| 111 | + { |
| 112 | + return 'exchangeratehost'; |
| 113 | + } |
| 114 | +} |
0 commit comments