2023-09-12

This commit is contained in:
Juan Pablo Vial
2023-09-13 18:51:46 -03:00
parent fa15da1ee2
commit 0cd357b6cb
47 changed files with 1225 additions and 102 deletions

View File

@ -0,0 +1,39 @@
<?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_intercambio';
public function __construct(protected ClientInterface $client) {}
public function get(string $money_symbol, DateTimeInterface $dateTime): float
{
$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 ($json->codigo !== $money_symbol or count($json->serie) === 0) {
throw new EmptyResponse($request_uri);
}
return $json->serie[0]->valor;
}
}