Files
oficial/app/src/Controller/API/Money.php

101 lines
3.8 KiB
PHP
Raw Normal View History

2023-11-22 19:08:19 -03:00
<?php
namespace Incoviba\Controller\API;
use DateInterval;
2023-11-23 00:53:49 -03:00
use DateTimeImmutable;
use DateTimeInterface;
2023-11-22 19:08:19 -03:00
use Incoviba\Common\Implement\Exception\EmptyRedis;
2023-11-23 00:53:49 -03:00
use Incoviba\Controller\withRedis;
2023-11-22 19:08:19 -03:00
use Incoviba\Service;
2023-11-23 00:53:49 -03:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
2023-11-22 19:08:19 -03:00
class Money
{
use withJson, withRedis;
private int $time = 60 * 60 * 24 * 30;
2023-11-23 00:53:49 -03:00
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Money $moneyService): ResponseInterface
2023-11-22 19:08:19 -03:00
{
$data = $request->getParsedBody();
$output = [
'input' => $data
];
$output[$data['provider']] = 0;
$redisKey = $data['provider'];
$date = new DateTimeImmutable($data['fecha']);
if (isset($data['start'])) {
$start = new DateTimeImmutable($data['start']);
}
$value = $this->getValue($redisService, $redisKey, $moneyService, $date, $data['provider']);
if (isset($start)) {
$months = $date->diff($start)->m;
$current = clone $start;
$value = $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
for ($i = 1; $i <= $months; $i ++) {
$current = $current->add(new DateInterval("P{$i}M"));
$value += $this->getValue($redisService, $redisKey, $moneyService, $current, $data['provider']);
}
}
$output[$data['provider']] = $value;
return $this->withJson($response, $output);
}
protected array $data;
2023-11-23 00:53:49 -03:00
protected function getValue(Service\Redis $redisService, string $redisKey, Service\Money $moneyService,
DateTimeInterface $date, string $provider): float
2023-11-22 19:08:19 -03:00
{
2023-11-25 00:55:31 -03:00
if (isset($this->data[$provider][$date->format('Y-m-d')])) {
return $this->data[$provider][$date->format('Y-m-d')];
2023-11-22 19:08:19 -03:00
}
try {
2023-11-25 00:55:31 -03:00
$this->data[$provider] = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$provider][$date->format('Y-m-d')])) {
2023-11-22 19:08:19 -03:00
throw new EmptyRedis($redisKey);
}
} catch (EmptyRedis) {
$result = $moneyService->get($provider, $date);
2023-11-25 00:55:31 -03:00
$this->data[$provider][$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data[$provider], $this->time);
2023-11-22 19:08:19 -03:00
}
2023-11-25 00:55:31 -03:00
return $this->data[$provider][$date->format('Y-m-d')];
2023-11-22 19:08:19 -03:00
}
2023-11-29 20:09:08 -03:00
public function uf(ServerRequestInterface $request, ResponseInterface $response, Service\UF $ufService): ResponseInterface
2023-11-22 19:08:19 -03:00
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'uf' => 0
];
$date = new DateTimeImmutable($body['fecha']);
2023-11-29 20:09:08 -03:00
$now = new DateTimeImmutable();
if ($date > $now) {
return $this->withJson($response, $output);
2023-11-22 19:08:19 -03:00
}
2023-11-29 20:09:08 -03:00
$output['uf'] = $ufService->get($date);
2023-11-22 19:08:19 -03:00
return $this->withJson($response, $output);
2023-11-29 20:09:08 -03:00
}
public function ipc(ServerRequestInterface $request, ResponseInterface $response,
Service\IPC $ipcService): ResponseInterface
2023-11-22 19:08:19 -03:00
{
$data = $request->getParsedBody();
$output = [
'input' => $data,
'date_string' => '',
'ipc' => 0
];
$start = new DateTimeImmutable($data['start']);
$end = new DateTimeImmutable($data['end']);
$now = new DateTimeImmutable();
if ($end > $now) {
return $this->withJson($response, $output);
}
$dateKey = "{$start->format('Y-m')}-{$end->format('Y-m')}";
$output['date_string'] = $dateKey;
2023-11-29 20:09:08 -03:00
$output['ipc'] = $ipcService->get($start, $end);
2023-11-22 19:08:19 -03:00
return $this->withJson($response, $output);
}
}