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

94 lines
2.7 KiB
PHP
Raw Normal View History

2023-09-13 18:51:46 -03:00
<?php
namespace Incoviba\Service;
use DateTimeInterface;
2024-03-26 16:41:33 -03:00
use Psr\Log\LoggerInterface;
2023-09-13 18:51:46 -03:00
use Incoviba\Common\Define\Money\Provider;
use Incoviba\Common\Implement\Exception\EmptyResponse;
class Money
{
const UF = 'uf';
const USD = 'usd';
const IPC = 'ipc';
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 in_array($provider, $this->providers[$name])) {
2023-09-13 18:51:46 -03:00
return $this;
}
if (!isset($this->providers[$name])) {
$this->providers[$name] = [];
}
$this->providers[$name] []= $provider;
2023-09-13 18:51:46 -03:00
return $this;
}
public function getProviders(string $name): array
2023-09-13 18:51:46 -03:00
{
return $this->providers[$name];
}
2023-11-22 19:08:19 -03:00
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);
}
2023-11-22 19:08:19 -03:00
}
return 0;
2023-11-22 19:08:19 -03:00
}
public function getUF(?DateTimeInterface $dateTime = null): float
2023-09-13 18:51:46 -03:00
{
$providers = $this->getProviders('uf');
foreach ($providers as $provider) {
try {
return $provider->get(self::UF, $dateTime);
} catch (EmptyResponse $exception) {
$this->logger->notice($exception);
}
2023-09-13 18:51:46 -03:00
}
return 0;
2023-09-13 18:51:46 -03:00
}
2023-11-29 20:09:08 -03:00
public function getIPC(DateTimeInterface $start, DateTimeInterface $end): float
2023-09-13 18:51:46 -03:00
{
2024-05-16 21:23:24 -04:00
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);
}
2023-09-13 18:51:46 -03:00
}
return 0;
2023-09-13 18:51:46 -03:00
}
2024-02-13 01:16:17 -03:00
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);
}
2024-02-13 01:16:17 -03:00
}
return 0;
}
public static function getSymbol(string $identifier): string
{
$upper = strtoupper($identifier);
$output = '';
eval("\$output = self::{$upper};");
return $output;
2024-02-13 01:16:17 -03:00
}
2023-09-13 18:51:46 -03:00
}