This commit is contained in:
Juan Pablo Vial
2024-07-26 23:15:48 -04:00
parent 43bb7a83c8
commit 84861b5e57
24 changed files with 457 additions and 18 deletions

View File

@ -4,12 +4,14 @@ namespace Incoviba\Service;
use DateTimeInterface;
use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Psr\Log\LoggerInterface;
class UF
{
protected string $redisKey = 'uf';
public function __construct(protected Redis $redisService, protected Money $moneyService) {}
public function __construct(protected Redis $redisService, protected Money $moneyService,
protected LoggerInterface $logger) {}
public function get(?DateTimeInterface $date = null): float
{
@ -25,12 +27,36 @@ class UF
$uf = $ufs[$date->format('Y-m-d')];
} catch (EmptyRedis) {
$uf = $this->moneyService->getUF($date);
if ($uf === 0.0) {
return 0.0;
}
$ufs[$date->format('Y-m-d')] = $uf;
ksort($ufs);
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
}
return $uf;
}
public function updateMany(array $dates): array
{
$ufs = [];
try {
$ufs = json_decode($this->redisService->get($this->redisKey), JSON_OBJECT_AS_ARRAY);
} catch (EmptyRedis) {}
$updated = [];
foreach ($dates as $date) {
if (!isset($ufs[$date->format('Y-m-d')]) or $ufs[$date->format('Y-m-d')] === 0) {
$uf = $this->moneyService->getUF($date);
if ($uf === 0.0) {
continue;
}
$updated[$date->format('Y-m-d')] = $uf;
$ufs[$date->format('Y-m-d')] = $this->moneyService->getUF($date);
}
}
ksort($ufs);
$this->redisService->set($this->redisKey, json_encode($ufs), 60 * 60 * 24 * 30);
return $updated;
}
public function transform(DateTimeInterface $date, float $input, string $from = 'uf'): float
{