Files
oficial/app/src/Service/Money/MiIndicador.php
2025-05-05 16:45:43 -04:00

53 lines
1.5 KiB
PHP

<?php
namespace Incoviba\Service\Money;
use DateTimeInterface;
use Psr\Http\Client\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Incoviba\Common\Define\Money\Provider;
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) {}
/**
* @throws EmptyResponse
*/
public function get(string $money_symbol, ?DateTimeInterface $dateTime = null): float
{
$request_uri = "{$money_symbol}";
if ($dateTime !== null) {
$request_uri = "{$money_symbol}/{$dateTime->format('d-m-Y')}";
}
try {
$response = $this->client->get($request_uri);
} catch (GuzzleException) {
throw new EmptyResponse($request_uri);
}
if ((int) floor($response->getStatusCode() / 100) !== 2) {
throw new EmptyResponse($request_uri);
}
$body = $response->getBody();
$json = json_decode($body->getContents());
if (empty($json) or $json->codigo !== $money_symbol or count($json->serie) === 0) {
throw new EmptyResponse($request_uri);
}
return $json->serie[0]->valor;
}
public static function getSymbol(string $identifier): string
{
$upper = strtoupper($identifier);
$output = '';
eval("\$output = self::{$upper};");
return $output;
}
}