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