-
-
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.
- Loading branch information
Showing
40 changed files
with
1,269 additions
and
947 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
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 = [], | ||
) | ||
{ | ||
} | ||
|
||
|
||
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('Y-m-d'); | ||
} | ||
|
||
|
||
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 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; | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.