diff --git a/app/src/Service/Money.php b/app/src/Service/Money.php index db62940..3405f3a 100644 --- a/app/src/Service/Money.php +++ b/app/src/Service/Money.php @@ -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 { - try { - return $this->getProvider($provider)->get(MiIndicador::getSymbol($provider), $dateTime); - } catch (EmptyResponse) { - return 0; + $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 { - try { - return $this->getProvider('uf')->get(MiIndicador::UF, $dateTime); - } catch (EmptyResponse $exception) { - $this->logger->debug($exception); - return 0; + $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; } - try { - return $this->getProvider('ipc')->getVar($start, $end); - } catch (EmptyResponse) { - 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 { - try { - return $this->getProvider('usd')->get(MiIndicador::USD, $dateTime); - } catch (EmptyResponse $exception) { - $this->logger->critical($exception); - return 0; + $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; } } diff --git a/app/src/Service/Money/MiIndicador.php b/app/src/Service/Money/MiIndicador.php index 68240e1..85a20b1 100644 --- a/app/src/Service/Money/MiIndicador.php +++ b/app/src/Service/Money/MiIndicador.php @@ -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; - } }