Skip to content

Commit

Permalink
refactors fixer cuurrency api implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vmcvlad committed Jun 23, 2021
1 parent 7dea1b5 commit 3d46633
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 170 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions src/APIs/FixerCurrency/Actions/Convert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Actions;

use LaravelEnso\Api\Action;
use LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints\Convert as Endpoint;
use LaravelEnso\Currencies\Models\Currency;

class Convert extends Action
{
public function __construct(
private Currency $from,
private Currency $to,
private float $amount
) {
}

protected function endpoint(): Endpoint
{
return new Endpoint([
'from' => $this->from->code,
'to' => $this->to->code,
'amount' => $this->amount,
]);
}
}
24 changes: 24 additions & 0 deletions src/APIs/FixerCurrency/Actions/Exchange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Actions;

use Illuminate\Support\Collection;
use LaravelEnso\Api\Action;
use LaravelEnso\Currencies\Models\Currency;

abstract class Exchange extends Action
{
protected Currency $base;
protected Collection $currencies;

public function __construct(Currency $base, Collection $currencies)
{
$this->base = $base;
$this->currencies = $currencies;
}

protected function symbols()
{
return $this->currencies->map->code->implode(',');
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency;
namespace LaravelEnso\Currencies\APIs\FixerCurrency\Actions;

use Carbon\Carbon;
use LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints\History as Endpoint;
use LaravelEnso\Currencies\Models\Currency;

class History extends Exchange
Expand All @@ -16,18 +17,11 @@ public function __construct(Currency $base, $currencies, Carbon $date)
$this->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);
}
}
16 changes: 16 additions & 0 deletions src/APIs/FixerCurrency/Actions/Rates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Actions;

use LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints\Rates as Endpoint;

class Rates extends Exchange
{
protected function endpoint(): Endpoint
{
return new Endpoint([
'base' => $this->base->code,
'symbols' => $this->symbols(),
]);
}
}
14 changes: 14 additions & 0 deletions src/APIs/FixerCurrency/Actions/Symbols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Actions;

use LaravelEnso\Api\Action;
use LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints\Symbols as Endpoint;

class Symbols extends Action
{
protected function endpoint(): Endpoint
{
return new Endpoint();
}
}
39 changes: 0 additions & 39 deletions src/APIs/FixerCurrency/Api.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/APIs/FixerCurrency/Convert.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/APIs/FixerCurrency/Endpoints/Convert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints;

class Convert extends Endpoint
{
private const Endpoint = 'convert';

public function __construct(private array $params)
{
}

public function path(): string
{
return self::Endpoint;
}

public function params(): array
{
return $this->params;
}
}
35 changes: 35 additions & 0 deletions src/APIs/FixerCurrency/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use LaravelEnso\Api\Contracts\Endpoint as Contract;
use LaravelEnso\Api\Enums\Methods;

abstract class Endpoint implements Contract
{
abstract public function path(): string;

abstract public function params(): array;

public function method(): string
{
return Methods::get;
}

public function url(): string
{
$baseUrl = Config::get('enso.currencies.fixerCurrencyApi.host');

return Collection::wrap([$baseUrl, $this->path()])
->filter()
->implode('/');
}

public function body(): array
{
return $this->params() +
['rapidapi-key' => Config::get('enso.currencies.fixerCurrencyApi.key')];
}
}
24 changes: 24 additions & 0 deletions src/APIs/FixerCurrency/Endpoints/History.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints;

use Carbon\Carbon;

class History extends Endpoint
{
public function __construct(
private array $params,
private Carbon $date
) {
}

public function path(): string
{
return $this->date;
}

public function params(): array
{
return $this->params;
}
}
22 changes: 22 additions & 0 deletions src/APIs/FixerCurrency/Endpoints/Rates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints;

class Rates extends Endpoint
{
private const Endpoint = 'latest';

public function __construct(private array $params)
{
}

public function path(): string
{
return self::Endpoint;
}

public function params(): array
{
return $this->params;
}
}
18 changes: 18 additions & 0 deletions src/APIs/FixerCurrency/Endpoints/Symbols.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace LaravelEnso\Currencies\APIs\FixerCurrency\Endpoints;

class Symbols extends Endpoint
{
private const Endpoint = 'symbols';

public function path(): string
{
return self::Endpoint;
}

public function params(): array
{
return [];
}
}
31 changes: 0 additions & 31 deletions src/APIs/FixerCurrency/Exchange.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/APIs/FixerCurrency/Rates.php

This file was deleted.

Loading

0 comments on commit 3d46633

Please sign in to comment.