Files
oficial/app/src/Service/Money.php

65 lines
1.8 KiB
PHP
Raw Normal View History

2023-09-13 18:51:46 -03:00
<?php
namespace Incoviba\Service;
use DateTimeInterface;
2023-11-22 19:08:19 -03:00
use DateTimeImmutable;
use DateInterval;
2023-09-13 18:51:46 -03:00
use Incoviba\Common\Define\Money\Provider;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Service\Money\MiIndicador;
2024-02-13 01:16:17 -03:00
use Psr\Log\LoggerInterface;
2023-09-13 18:51:46 -03:00
class Money
{
2024-02-13 01:16:17 -03:00
public function __construct(protected LoggerInterface $logger) {}
2023-09-13 18:51:46 -03:00
protected array $providers;
public function register(string $name, Provider $provider): Money
{
if (isset($this->providers) and isset($this->providers[$name]) and $this->providers[$name] === $provider) {
return $this;
}
$this->providers[$name] = $provider;
return $this;
}
public function getProvider(string $name): Provider
{
return $this->providers[$name];
}
2023-11-22 19:08:19 -03:00
public function get(string $provider, DateTimeInterface $dateTime): float
{
try {
$upper = strtoupper($provider);
return $this->getProvider($provider)->get(MiIndicador::getSymbol($provider), $dateTime);
} catch (EmptyResponse) {
return 0;
}
}
2023-09-13 18:51:46 -03:00
public function getUF(DateTimeInterface $dateTime): float
{
try {
return $this->getProvider('uf')->get(MiIndicador::UF, $dateTime);
} catch (EmptyResponse) {
return 0;
}
}
2023-11-29 20:09:08 -03:00
public function getIPC(DateTimeInterface $start, DateTimeInterface $end): float
2023-09-13 18:51:46 -03:00
{
try {
2023-11-29 20:09:08 -03:00
return $this->getProvider('ipc')->getVar($start, $end);
2023-09-13 18:51:46 -03:00
} catch (EmptyResponse) {
return 0;
}
}
2024-02-13 01:16:17 -03:00
public function getUSD(DateTimeInterface $dateTime): float
{
try {
return $this->getProvider('usd')->get(MiIndicador::USD, $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->critical($exception);
return 0;
}
}
2023-09-13 18:51:46 -03:00
}