diff --git a/composer.json b/composer.json index 965af70..954bb8c 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ ], "homepage": "https://github.com/laravel-enso/currencies", "require": { + "laravel-enso/api": "^1.1", "laravel-enso/core": "^7.0", "laravel-enso/countries": "^2.0", "laravel-enso/helpers": "^2.0", diff --git a/src/APIs/FixerCurrency/Actions/Convert.php b/src/APIs/FixerCurrency/Actions/Convert.php new file mode 100644 index 0000000..2e15f27 --- /dev/null +++ b/src/APIs/FixerCurrency/Actions/Convert.php @@ -0,0 +1,26 @@ + $this->from->code, + 'to' => $this->to->code, + 'amount' => $this->amount, + ]); + } +} diff --git a/src/APIs/FixerCurrency/Actions/Exchange.php b/src/APIs/FixerCurrency/Actions/Exchange.php new file mode 100644 index 0000000..2f850e9 --- /dev/null +++ b/src/APIs/FixerCurrency/Actions/Exchange.php @@ -0,0 +1,24 @@ +base = $base; + $this->currencies = $currencies; + } + + protected function symbols() + { + return $this->currencies->map->code->implode(','); + } +} diff --git a/src/APIs/FixerCurrency/History.php b/src/APIs/FixerCurrency/Actions/History.php similarity index 52% rename from src/APIs/FixerCurrency/History.php rename to src/APIs/FixerCurrency/Actions/History.php index d529544..4fc1deb 100644 --- a/src/APIs/FixerCurrency/History.php +++ b/src/APIs/FixerCurrency/Actions/History.php @@ -1,8 +1,9 @@ date = $date; } - public function handle() + protected function endpoint(): Endpoint { - return $this->api->endPoint($this->date->format('Y-m-d')) - ->query($this->query()) - ->request(); - } - - private function query() - { - return [ - 'base' => $this->base, + return new Endpoint([ + 'base' => $this->base->code, 'symbols' => $this->symbols(), - ]; + ], $this->date); } } diff --git a/src/APIs/FixerCurrency/Actions/Rates.php b/src/APIs/FixerCurrency/Actions/Rates.php new file mode 100644 index 0000000..f88f5e3 --- /dev/null +++ b/src/APIs/FixerCurrency/Actions/Rates.php @@ -0,0 +1,16 @@ + $this->base->code, + 'symbols' => $this->symbols(), + ]); + } +} diff --git a/src/APIs/FixerCurrency/Actions/Symbols.php b/src/APIs/FixerCurrency/Actions/Symbols.php new file mode 100644 index 0000000..1bdb307 --- /dev/null +++ b/src/APIs/FixerCurrency/Actions/Symbols.php @@ -0,0 +1,14 @@ +headers()) - ->get($this->endPoint, $this->query) - ->throw() - ->json(); - } - - public function endPoint(string $endPoint): self - { - $this->endPoint = $endPoint; - - return $this; - } - - public function query(array $query): self - { - $this->query = $query; - - return $this; - } - - private function headers() - { - return ['x-rapidapi-key' => Config::get('enso.currencies.fixerCurrencyApi.key')]; - } -} diff --git a/src/APIs/FixerCurrency/Convert.php b/src/APIs/FixerCurrency/Convert.php deleted file mode 100644 index 2b3f843..0000000 --- a/src/APIs/FixerCurrency/Convert.php +++ /dev/null @@ -1,39 +0,0 @@ -api = new Api(); - $this->from = $from; - $this->to = $to; - $this->amount = $amount; - } - - public function handle() - { - return $this->api->endPoint(self::EndPoint) - ->query($this->query()) - ->request(); - } - - private function query() - { - return [ - 'from' => $this->from->code, - 'to' => $this->to->code, - 'amount' => $this->amount, - ]; - } -} diff --git a/src/APIs/FixerCurrency/Endpoints/Convert.php b/src/APIs/FixerCurrency/Endpoints/Convert.php new file mode 100644 index 0000000..efb4b22 --- /dev/null +++ b/src/APIs/FixerCurrency/Endpoints/Convert.php @@ -0,0 +1,22 @@ +params; + } +} diff --git a/src/APIs/FixerCurrency/Endpoints/Endpoint.php b/src/APIs/FixerCurrency/Endpoints/Endpoint.php new file mode 100644 index 0000000..65ba6b4 --- /dev/null +++ b/src/APIs/FixerCurrency/Endpoints/Endpoint.php @@ -0,0 +1,35 @@ +path()]) + ->filter() + ->implode('/'); + } + + public function body(): array + { + return $this->params() + + ['rapidapi-key' => Config::get('enso.currencies.fixerCurrencyApi.key')]; + } +} diff --git a/src/APIs/FixerCurrency/Endpoints/History.php b/src/APIs/FixerCurrency/Endpoints/History.php new file mode 100644 index 0000000..eb6ce2d --- /dev/null +++ b/src/APIs/FixerCurrency/Endpoints/History.php @@ -0,0 +1,24 @@ +date; + } + + public function params(): array + { + return $this->params; + } +} diff --git a/src/APIs/FixerCurrency/Endpoints/Rates.php b/src/APIs/FixerCurrency/Endpoints/Rates.php new file mode 100644 index 0000000..3433ecc --- /dev/null +++ b/src/APIs/FixerCurrency/Endpoints/Rates.php @@ -0,0 +1,22 @@ +params; + } +} diff --git a/src/APIs/FixerCurrency/Endpoints/Symbols.php b/src/APIs/FixerCurrency/Endpoints/Symbols.php new file mode 100644 index 0000000..b4ec353 --- /dev/null +++ b/src/APIs/FixerCurrency/Endpoints/Symbols.php @@ -0,0 +1,18 @@ +api = new Api(); - $this->base = $base; - $this->currencies = $currencies; - } - - protected function symbols() - { - $collection = $this->currencies instanceof Collection - ? $this->currencies - : new Collection([$this->currencies]); - - return $collection - ->map(fn (Currency $currency) => $currency->code) - ->implode(','); - } -} diff --git a/src/APIs/FixerCurrency/Rates.php b/src/APIs/FixerCurrency/Rates.php deleted file mode 100644 index 85fe431..0000000 --- a/src/APIs/FixerCurrency/Rates.php +++ /dev/null @@ -1,23 +0,0 @@ -api->endPoint(self::EndPoint) - ->query($this->query()) - ->request(); - } - - private function query() - { - return [ - 'base' => $this->base->code, - 'symbols' => $this->symbols(), - ]; - } -} diff --git a/src/APIs/FixerCurrency/Symbols.php b/src/APIs/FixerCurrency/Symbols.php deleted file mode 100644 index dcbf0c3..0000000 --- a/src/APIs/FixerCurrency/Symbols.php +++ /dev/null @@ -1,20 +0,0 @@ -api = new Api(); - } - - public function handle() - { - return $this->api->endPoint(self::EndPoint)->request(); - } -} diff --git a/src/Services/FetchExchangeRates.php b/src/Services/FetchExchangeRates.php index 86a302c..37bc202 100644 --- a/src/Services/FetchExchangeRates.php +++ b/src/Services/FetchExchangeRates.php @@ -4,7 +4,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Config; -use LaravelEnso\Currencies\APIs\FixerCurrency\Rates; +use LaravelEnso\Currencies\APIs\FixerCurrency\Actions\Rates; use LaravelEnso\Currencies\Models\Currency; use LaravelEnso\Currencies\Models\ExchangeRate; use LaravelEnso\Helpers\Services\Decimals; @@ -19,14 +19,13 @@ public function __construct( private Currency $base, private $toCurrencies ) { - $this->base = $base; - $this->toCurrencies = $toCurrencies; $this->precision = Config::get('enso.currencies.apiPrecision'); } public function handle() { - $this->response = (new Rates($this->base, $this->toCurrencies))->handle(); + $this->response = (new Rates($this->base, $this->toCurrencies))->handle() + ->json(); $this->currencies = Currency::all(); @@ -37,7 +36,7 @@ public function handle() private function processAll() { - (new Collection($this->response->rates)) + (new Collection($this->response['rates'])) ->each(fn ($rate, $currency) => $this->process($rate, $currency)); } @@ -57,7 +56,7 @@ private function persist(Currency $from, Currency $to, string $rate) ExchangeRate::updateOrCreate([ 'from_id' => $from->id, 'to_id' => $to->id, - 'date' => $this->response->date, + 'date' => $this->response['date'], ], [ 'conversion' => $rate, ]);