Money and Remote services

This commit is contained in:
Juan Pablo Vial
2023-06-22 14:04:40 -04:00
parent 8a51fb9c42
commit 05e37f19ae
4 changed files with 138 additions and 21 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Alias;
use PDO;
use PDOException;
class RemoteConnection
{
public function __construct(protected int $retries = 5)
{
$this->host = $_ENV['REMOTE_HOST'];
$this->database = $_ENV['REMOTE_DATABASE'];
$this->username = $_ENV['REMOTE_USER'];
$this->password = $_ENV['REMOTE_PASSWORD'];
if (isset($_ENV['REMOTE_PORT'])) {
$this->port = $_ENV['REMOTE_PORT'];
}
}
protected string $host;
protected string $database;
protected int $port;
protected string $username;
protected string $password;
protected PDO $connection;
public function connect(): PDO
{
if (!isset($this->connection)) {
$r = 0;
$exception = null;
while ($r < $this->retries) {
try {
$dsn = $this->getDsn();
$this->connection = new PDO($dsn, $this->username, $this->password);
} catch (PDOException $e) {
if ($exception !== null) {
$e = new PDOException($e->getMessage(), $e->getCode(), $exception);
}
$exception = $e;
usleep(500);
}
$r ++;
}
throw $exception;
}
return $this->connection;
}
protected function getDsn(): string
{
$dsn = "mysql:host={$this->host};dbname={$this->database}";
if (isset($this->port)) {
$dsn .= ";port={$this->port}";
}
return $dsn;
}
}

View File

@ -31,27 +31,13 @@ function route_api() {
return call_user_func_array([$class, $a], $params);
}
function uf($date, $async = false) {
if (is_string($date)) {
$date = Carbon\Carbon::parse($date, config('app.timezone'));
}
$next_m_9 = Carbon\Carbon::today(config('app.timezone'))->copy()->endOfMonth()->addDays(9);
if ($date->greaterThanOrEqualTo($next_m_9)) {
return (object) ['total' => 0];
}
$url = 'http://' . config('locations.money') . '/api/uf/value/' . $date->format('Y-m-d');
$client = new \GuzzleHttp\Client(['base_uri' => 'http://' . config('locations.money') . '/', 'headers' => ['Accept' => 'application/json']]);
$response = $client->get('api/uf/value/' . $date->format('Y-m-d'));
//$response = $client->getResponse();
if (!$response) {
return (object) ['total' => 0];
}
$status = $response->getStatusCode();
if ($status >= 200 and $status < 300) {
$data = json_decode($response->getBody()->getContents());
return $data;
}
return (object) ['total' => 0];
$remote = new App\Service\Remote(new App\Alias\RemoteConnection());
return (new App\Service\Money(new GuzzleHttp\Client([
'base_uri' => "http://{$remote->getIP()}:8080",
'headers' => [
'Accept' => 'application/json'
]
])))->getUF($date);
}
function format($tipo, $valor, $format = null, $print = false) {
if ($valor === null) {

54
app/Service/Money.php Normal file
View File

@ -0,0 +1,54 @@
<?php
namespace App\Service;
use DateTimeInterface;
use Psr\Http\Message\ResponseInterface;
use Carbon\Carbon;
use GuzzleHttp\Client;
class Money
{
public function __construct(protected Client $client) {}
public function getUF(string|DateTimeInterface $date): object
{
$date = $this->parseDate($date);
if (!$this->checkNextMonthValueCalculationByDate($date)) {
return $this->emptyResult();
}
$response = $this->client->get('api/uf/value/' . $date->format('Y-m-d'));
if (!$this->isOk($response)) {
return $this->emptyResult();
}
$body = $response->getBody()->getContents();
if (trim($response->getBody()) === '') {
return $this->emptyResult();
}
return json_decode($body);
}
protected function parseDate(string|DateTimeInterface $date): DateTimeInterface
{
if (is_string($date)) {
$date = Carbon::parse($date, config('app.timezone'));
}
return $date;
}
protected function checkNextMonthValueCalculationByDate(DateTimeInterface $date): bool
{
$next_m_9 = Carbon::today(config('app.timezone'))->copy()->endOfMonth()->addDays(9);
return $date->lessThan($next_m_9);
}
protected function isOk(ResponseInterface $response): bool
{
$statusCode = $response->getStatusCode();
return $statusCode >= 200 and $statusCode < 300;
}
protected function emptyResult(): object
{
return (object) ['total' => 0];
}
}

19
app/Service/Remote.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace App\Service;
use PDO;
use App\Alias\RemoteConnection;
class Remote
{
public function __construct(protected RemoteConnection $connection) {}
public function getIP(): string
{
$query = "SELECT `ip` FROM `remote_ip` WHERE `host` = 'vialdelamaza'";
$connection = $this->connection->connect();
$statement = $connection->query($query);
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result['ip'];
}
}