Skip to content

Commit

Permalink
Implements Storage Cache Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoinds committed Apr 3, 2024
1 parent d841325 commit 43ff82e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Converter/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

namespace Brunoinds\FrankfurterLaravel\Converter;

use Illuminate\Support\Facades\Cache;
use Brunoinds\FrankfurterLaravel\Store\Store;
use DateTime;
use Carbon\Carbon;


class Converter{
public static function convertFromTo(DateTime $date, float $amount, string $from, string $to){
public static Store|null $store = null;
public static function convertFromTo(DateTime $date, float $amount, string $from, string $to)
{
return Converter::fetchConvertion($date, $amount, $from, $to);
}
private static function fetchConvertion(DateTime $date, float $amount, string $from, string $to){
private static function fetchConvertion(DateTime $date, float $amount, string $from, string $to)
{
if ($date->format('Y-m-d') > Carbon::now()->timezone('America/Lima')->format('Y-m-d')){
$date = Carbon::now()->timezone('America/Lima')->toDateTime();
}
Expand All @@ -23,7 +26,7 @@ private static function fetchConvertion(DateTime $date, float $amount, string $f
$curlURL = 'https://api.frankfurter.app/' . $dateString . '?from=' . $from . '&to=' . $to . '&amount=' . $amount;

$stores = [];
$cachedValue = Cache::store('file')->get('Brunoinds/FrankfurterLaravelStore');
$cachedValue = Converter::$store->get();
if ($cachedValue){
$stores = json_decode($cachedValue, true);
if (isset($stores[$curlURL])){
Expand Down Expand Up @@ -54,14 +57,17 @@ private static function fetchConvertion(DateTime $date, float $amount, string $f

try {
$rate = $results['rates'][$to];

$stores[$curlURL] = $rate;
Cache::store('file')->put('Brunoinds/FrankfurterLaravelStore', json_encode($stores));
} catch (\Throwable $th) {
throw new \Exception('Error while parsing the conversion rate. The API response was: ' . $response);
}

try {
Converter::$store->set(json_encode($stores));
return $rate;
} catch (\Throwable $th) {
throw new \Exception('Invalid JSON response: "' . json_last_error_msg(). '". The API response was: ' . $response);
throw $th;
throw new \Exception('Error while storing the conversion rate. If you are using a custom adapter, please make sure it is working correctly. The API response was: ' . $response);
}
}

}
7 changes: 7 additions & 0 deletions src/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use DateTime;
use Brunoinds\FrankfurterLaravel\ExchangeDate\ExchangeDate;
use Brunoinds\FrankfurterLaravel\Store\Store;
use Brunoinds\FrankfurterLaravel\Converter\Converter;

class Exchange{
public static function on(DateTime $date): ExchangeDate
Expand All @@ -14,4 +16,9 @@ public static function on(DateTime $date): ExchangeDate
public static function now():ExchangeDate{
return new ExchangeDate(new DateTime());
}

public static function useStore(Store $store) :void
{
Converter::$store = $store;
}
}
5 changes: 5 additions & 0 deletions src/ExchangeDate/ExchangeDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
use Brunoinds\FrankfurterLaravel\Enums\Currency;
use Brunoinds\FrankfurterLaravel\ExchangeTransaction\ExchangeTransaction;
use DateTime;
use Brunoinds\FrankfurterLaravel\Store\Store;

class ExchangeDate{
public DateTime $date;

public function __construct(DateTime $date){
if (!Converter::$store){
Converter::$store = Store::newFromLaravelCache();
}

$this->date = $date;
}

Expand Down
56 changes: 56 additions & 0 deletions src/Store/Store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Brunoinds\FrankfurterLaravel\Store;

use Illuminate\Support\Facades\Cache;

class Store{
private mixed $store;

private function __construct(mixed $store)
{
$this->store = $store;
}

public function get()
{
return $this->store->get();
}

public function set(string $value)
{
return $this->store->set($value);
}
public static function newFromLaravelCache($store = 'file')
{
return new self(new StoreLaravelCache($store));
}

public static function newFromAdapter(mixed $adapter)
{
if (!method_exists($adapter, 'get') || !method_exists($adapter, 'set')){
throw new \Exception('The adapter must have the methods get and set');
}
return new self($adapter);
}
}


class StoreLaravelCache
{
private string $store;
public function __construct(string $store = 'file')
{
$this->store = $store;
}

public function get()
{
return Cache::store($this->store)->get('Brunoinds/FrankfurterLaravelStore');
}

public function set(string $value)
{
Cache::store($this->store)->put('Brunoinds/FrankfurterLaravelStore', $value);
}
}

0 comments on commit 43ff82e

Please sign in to comment.