-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Exchange)!: improve store and invalid cache
- Loading branch information
Showing
45 changed files
with
1,389 additions
and
1,037 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Exchange\Download; | ||
|
||
use DateTimeImmutable; | ||
|
||
final class SourceData | ||
{ | ||
/** | ||
* @param iterable<mixed> $properties | ||
*/ | ||
public function __construct( | ||
public DateTimeImmutable $date, | ||
public string $refresh, | ||
public iterable $properties, | ||
) | ||
{ | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Exchange\Download; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use h4kuna\Exchange\Driver\Source; | ||
use h4kuna\Exchange\RatingList\RatingList; | ||
use h4kuna\Exchange\Utils; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use ReflectionClass; | ||
|
||
final class SourceDownload implements SourceDownloadInterface | ||
{ | ||
/** | ||
* @var array<string, SourceData> | ||
*/ | ||
private array $cache = []; | ||
|
||
|
||
/** | ||
* @param array<string, int> $allowedCurrencies | ||
*/ | ||
public function __construct( | ||
private ClientInterface $client, | ||
private RequestFactoryInterface $requestFactory, | ||
private array $allowedCurrencies = [], | ||
) | ||
{ | ||
} | ||
|
||
|
||
public function execute(Source $sourceExchange, ?DateTimeInterface $date): RatingList | ||
{ | ||
$date = Utils::toImmutable($date, $sourceExchange->getTimeZone()); | ||
$key = self::makeKey($sourceExchange, $date); | ||
|
||
$sourceData = $this->cache[$key] | ||
?? $this->cache[$key] = $sourceExchange->createSourceData( | ||
$this->client->sendRequest( | ||
$this->createRequest($sourceExchange, $date) | ||
) | ||
); | ||
|
||
$expire = $date === null ? new DateTime($sourceData->refresh . sprintf(', +%s seconds', Utils::CacheMinutes), $sourceExchange->getTimeZone()) : null; | ||
|
||
$properties = []; | ||
foreach ($sourceData->properties as $item) { | ||
$property = $sourceExchange->createProperty($item); | ||
if ($property->rate === 0.0 || ($this->allowedCurrencies !== [] && isset($this->allowedCurrencies[$property->code]) === false)) { | ||
continue; | ||
} | ||
|
||
$properties[$property->code] = $property; | ||
} | ||
|
||
return new RatingList($sourceData->date, $date, $expire, $properties); | ||
} | ||
|
||
|
||
private static function makeKey(Source $sourceExchange, ?DateTimeImmutable $date): string | ||
{ | ||
$rf = new ReflectionClass($sourceExchange); | ||
do { | ||
$class = $rf->getName(); | ||
$rf = $rf->getParentClass(); | ||
} while ($rf !== false); | ||
|
||
if ($date === null) { | ||
$date = new DateTimeImmutable('now', $sourceExchange->getTimeZone()); | ||
} | ||
|
||
return $class . '.' . $date->format(Utils::DateFormat); | ||
} | ||
|
||
|
||
private function createRequest(Source $sourceExchange, ?DateTimeInterface $date): RequestInterface | ||
{ | ||
$request = $this->requestFactory->createRequest('GET', $sourceExchange->makeUrl($date)); | ||
$request->withHeader('X-Powered-By', 'h4kuna/exchange'); | ||
|
||
return $request; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace h4kuna\Exchange\Download; | ||
|
||
use DateTimeInterface; | ||
use h4kuna\Exchange\Driver\Source; | ||
use h4kuna\Exchange\RatingList\RatingList; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
|
||
interface SourceDownloadInterface | ||
{ | ||
|
||
/** | ||
* @throws ClientExceptionInterface | ||
*/ | ||
function execute(Source $sourceExchange, ?DateTimeInterface $date): RatingList; | ||
} |
Oops, something went wrong.