|
| 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\ApiLayer; |
| 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\Service\HttpService; |
| 23 | +use Exchanger\Service\SupportsHistoricalQueries; |
| 24 | +use Exchanger\StringUtil; |
| 25 | +use Exchanger\Contract\ExchangeRate as ExchangeRateContract; |
| 26 | + |
| 27 | +/** |
| 28 | + * Fixer Service (https://apilayer.com/marketplace/fixer-api). |
| 29 | + * |
| 30 | + * @author Florian Voutzinos <[email protected]> |
| 31 | + */ |
| 32 | +final class Fixer extends HttpService |
| 33 | +{ |
| 34 | + use SupportsHistoricalQueries; |
| 35 | + |
| 36 | + const ACCESS_KEY_OPTION = 'access_key'; |
| 37 | + |
| 38 | + const LATEST_URL = 'https://api.apilayer.com/fixer/latest?base=%s&apikey=%s'; |
| 39 | + |
| 40 | + const HISTORICAL_URL = 'https://api.apilayer.com/fixer/%s?base=%s&apikey=%s'; |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function processOptions(array &$options): void |
| 46 | + { |
| 47 | + if (!isset($options[self::ACCESS_KEY_OPTION])) { |
| 48 | + throw new \InvalidArgumentException('The "access_key" option must be provided to use https://apilayer.com/marketplace/fixer-api'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function getLatestExchangeRate(ExchangeRateQuery $exchangeQuery): ExchangeRateContract |
| 56 | + { |
| 57 | + $currencyPair = $exchangeQuery->getCurrencyPair(); |
| 58 | + |
| 59 | + $url = sprintf( |
| 60 | + self::LATEST_URL, |
| 61 | + $exchangeQuery->getCurrencyPair()->getBaseCurrency(), |
| 62 | + $this->options[self::ACCESS_KEY_OPTION] |
| 63 | + ); |
| 64 | + |
| 65 | + return $this->doCreateRate($url, $currencyPair); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + protected function getHistoricalExchangeRate(HistoricalExchangeRateQuery $exchangeQuery): ExchangeRateContract |
| 72 | + { |
| 73 | + $currencyPair = $exchangeQuery->getCurrencyPair(); |
| 74 | + |
| 75 | + $url = sprintf( |
| 76 | + self::HISTORICAL_URL, |
| 77 | + $exchangeQuery->getDate()->format('Y-m-d'), |
| 78 | + $exchangeQuery->getCurrencyPair()->getBaseCurrency(), |
| 79 | + $this->options[self::ACCESS_KEY_OPTION] |
| 80 | + ); |
| 81 | + |
| 82 | + return $this->doCreateRate($url, $currencyPair); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * {@inheritdoc} |
| 87 | + */ |
| 88 | + public function supportQuery(ExchangeRateQuery $exchangeQuery): bool |
| 89 | + { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates a rate. |
| 95 | + * |
| 96 | + * @param string $url |
| 97 | + * @param CurrencyPair $currencyPair |
| 98 | + * |
| 99 | + * @return ExchangeRate |
| 100 | + * |
| 101 | + * @throws Exception |
| 102 | + */ |
| 103 | + private function doCreateRate($url, CurrencyPair $currencyPair): ExchangeRate |
| 104 | + { |
| 105 | + $content = $this->request($url); |
| 106 | + $data = StringUtil::jsonToArray($content); |
| 107 | + |
| 108 | + if (isset($data['error'])) { |
| 109 | + throw new Exception($this->getErrorMessage($data['error']['code'])); |
| 110 | + } |
| 111 | + |
| 112 | + if (isset($data['rates'][$currencyPair->getQuoteCurrency()])) { |
| 113 | + $date = new \DateTime($data['date']); |
| 114 | + $rate = $data['rates'][$currencyPair->getQuoteCurrency()]; |
| 115 | + |
| 116 | + return $this->createRate($currencyPair, (float) $rate, $date); |
| 117 | + } |
| 118 | + |
| 119 | + throw new UnsupportedCurrencyPairException($currencyPair, $this); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Gets the error message corresponding to the error code. |
| 124 | + * |
| 125 | + * @param string $code The error code |
| 126 | + * |
| 127 | + * @return string |
| 128 | + */ |
| 129 | + private function getErrorMessage($code): string |
| 130 | + { |
| 131 | + $errors = [ |
| 132 | + 404 => 'The requested resource does not exist.', |
| 133 | + 101 => 'No API Key was specified or an invalid API Key was specified.', |
| 134 | + 103 => 'The requested API endpoint does not exist.', |
| 135 | + 104 => 'The maximum allowed API amount of monthly API requests has been reached.', |
| 136 | + 105 => 'The current subscription plan does not support this API endpoint.', |
| 137 | + 106 => 'The current request did not return any results.', |
| 138 | + 102 => 'The account this API request is coming from is inactive.', |
| 139 | + 201 => 'An invalid base currency has been entered.', |
| 140 | + 202 => 'One or more invalid symbols have been specified.', |
| 141 | + 301 => 'No date has been specified.', |
| 142 | + 302 => 'An invalid date has been specified.', |
| 143 | + 403 => 'No or an invalid amount has been specified.', |
| 144 | + 501 => 'No or an invalid timeframe has been specified.', |
| 145 | + 502 => 'No or an invalid "start_date" has been specified.', |
| 146 | + 503 => 'No or an invalid "end_date" has been specified.', |
| 147 | + 504 => 'An invalid timeframe has been specified.', |
| 148 | + 505 => 'The specified timeframe is too long, exceeding 365 days.', |
| 149 | + ]; |
| 150 | + |
| 151 | + return isset($errors[$code]) ? $errors[$code] : ''; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * {@inheritdoc} |
| 156 | + */ |
| 157 | + public function getName(): string |
| 158 | + { |
| 159 | + return 'apilayer_fixer'; |
| 160 | + } |
| 161 | +} |
0 commit comments