diff --git a/app/Alias/RemoteConnection.php b/app/Alias/RemoteConnection.php new file mode 100644 index 0000000..20d0d25 --- /dev/null +++ b/app/Alias/RemoteConnection.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/app/Helper/functions.php b/app/Helper/functions.php index 5b61120..7c55e14 100644 --- a/app/Helper/functions.php +++ b/app/Helper/functions.php @@ -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) { diff --git a/app/Service/Money.php b/app/Service/Money.php new file mode 100644 index 0000000..f0a2204 --- /dev/null +++ b/app/Service/Money.php @@ -0,0 +1,54 @@ +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]; + } +} diff --git a/app/Service/Remote.php b/app/Service/Remote.php new file mode 100644 index 0000000..2e6e69e --- /dev/null +++ b/app/Service/Remote.php @@ -0,0 +1,19 @@ +connection->connect(); + $statement = $connection->query($query); + $result = $statement->fetch(PDO::FETCH_ASSOC); + return $result['ip']; + } +}