const en Money y multi provider

This commit is contained in:
Juan Pablo Vial
2025-05-05 18:15:29 -04:00
parent 594cb68b09
commit 3ced9e40b1
2 changed files with 50 additions and 33 deletions

View File

@ -5,61 +5,89 @@ use DateTimeInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define\Money\Provider;
use Incoviba\Common\Implement\Exception\EmptyResponse;
use Incoviba\Service\Money\MiIndicador;
class Money
{
const UF = 'uf';
const USD = 'usd';
const IPC = 'ipc';
public function __construct(protected LoggerInterface $logger) {}
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) {
if (isset($this->providers) and isset($this->providers[$name]) and in_array($provider, $this->providers[$name])) {
return $this;
}
$this->providers[$name] = $provider;
if (!isset($this->providers[$name])) {
$this->providers[$name] = [];
}
$this->providers[$name] []= $provider;
return $this;
}
public function getProvider(string $name): Provider
public function getProviders(string $name): array
{
return $this->providers[$name];
}
public function get(string $provider, DateTimeInterface $dateTime): float
{
$providers = $this->getProviders($provider);
foreach ($providers as $provider) {
try {
return $this->getProvider($provider)->get(MiIndicador::getSymbol($provider), $dateTime);
} catch (EmptyResponse) {
return 0;
return $provider->get(self::getSymbol($provider), $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->notice($exception);
}
}
return 0;
}
public function getUF(?DateTimeInterface $dateTime = null): float
{
$providers = $this->getProviders('uf');
foreach ($providers as $provider) {
try {
return $this->getProvider('uf')->get(MiIndicador::UF, $dateTime);
return $provider->get(self::UF, $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->debug($exception);
return 0;
$this->logger->notice($exception);
}
}
return 0;
}
public function getIPC(DateTimeInterface $start, DateTimeInterface $end): float
{
if ($start >= $end) {
return 0;
}
$providers = $this->getProviders('ipc');
foreach ($providers as $provider) {
try {
return $this->getProvider('ipc')->getVar($start, $end);
} catch (EmptyResponse) {
return 0;
return $provider->getVar($start, $end);
} catch (EmptyResponse $exception) {
$this->logger->notice($exception);
}
}
return 0;
}
public function getUSD(DateTimeInterface $dateTime): float
{
$providers = $this->getProviders('usd');
foreach ($providers as $provider) {
try {
return $this->getProvider('usd')->get(MiIndicador::USD, $dateTime);
return $provider->get(self::USD, $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->critical($exception);
$this->logger->notice($exception);
}
}
return 0;
}
public static function getSymbol(string $identifier): string
{
$upper = strtoupper($identifier);
$output = '';
eval("\$output = self::{$upper};");
return $output;
}
}

View File

@ -9,10 +9,6 @@ use Incoviba\Common\Implement\Exception\EmptyResponse;
class MiIndicador implements Provider
{
const UF = 'uf';
const IPC = 'ipc';
const USD = 'dolar';
public function __construct(protected ClientInterface $client) {}
/**
@ -42,11 +38,4 @@ class MiIndicador implements Provider
}
return $json->serie[0]->valor;
}
public static function getSymbol(string $identifier): string
{
$upper = strtoupper($identifier);
$output = '';
eval("\$output = self::{$upper};");
return $output;
}
}