Files
oficial/app/src/Service/Money.php
aldarien 307f2ac7d7 feature/cierres (#25)
Varios cambios

Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl>
Reviewed-on: #25
2025-07-22 13:18:00 +00:00

94 lines
2.7 KiB
PHP

<?php
namespace Incoviba\Service;
use DateTimeInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define\Money\Provider;
use Incoviba\Common\Implement\Exception\EmptyResponse;
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 in_array($provider, $this->providers[$name])) {
return $this;
}
if (!isset($this->providers[$name])) {
$this->providers[$name] = [];
}
$this->providers[$name] []= $provider;
return $this;
}
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 $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 $provider->get(self::UF, $dateTime);
} catch (EmptyResponse $exception) {
$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 $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 $provider->get(self::USD, $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->notice($exception);
}
}
return 0;
}
public static function getSymbol(string $identifier): string
{
$upper = strtoupper($identifier);
$output = '';
eval("\$output = self::{$upper};");
return $output;
}
}