Optimized connection to db
This commit is contained in:
41
app/common/Service/Connector.php
Normal file
41
app/common/Service/Connector.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace ProVM\Service;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Connector
|
||||
{
|
||||
public function __construct(protected string $host, protected string $name, protected string $username, protected string $password, protected string $retries, protected LoggerInterface $logger) {}
|
||||
|
||||
protected PDO $connection;
|
||||
|
||||
/**
|
||||
* @throws PDOException
|
||||
*/
|
||||
public function connect(): PDO
|
||||
{
|
||||
if (!isset($this->connection)) {
|
||||
$this->logger->debug('Connecting');
|
||||
$r = 0;
|
||||
$exception = null;
|
||||
while($r < $this->retries) {
|
||||
try {
|
||||
$dsn = "mysql:host={$this->host};dbname={$this->name}";
|
||||
$this->connection = new PDO($dsn, $this->username, $this->password);
|
||||
return $this->connection;
|
||||
} catch (PDOException $e) {
|
||||
$this->logger->debug('Retrying');
|
||||
if ($exception !== null) {
|
||||
$e = new PDOException($e->getMessage(), $e->getCode(), $exception);
|
||||
}
|
||||
$exception = $e;
|
||||
}
|
||||
$r ++;
|
||||
}
|
||||
throw $exception;
|
||||
}
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ class Ipify
|
||||
if (!isset($json->ip)) {
|
||||
throw new Exception('Missing `ip` in JSON response');
|
||||
}
|
||||
$this->logger->debug("Current IP: {$json->ip}");
|
||||
return $json->ip;
|
||||
}
|
||||
}
|
||||
|
@ -6,28 +6,28 @@ use Psr\Log\LoggerInterface;
|
||||
|
||||
class Repository
|
||||
{
|
||||
public function __construct(protected PDO $connection, protected string $table, protected LoggerInterface $logger) {}
|
||||
public function __construct(protected Connector $connector, protected string $table, protected LoggerInterface $logger) {}
|
||||
|
||||
public function update(string $ip): void
|
||||
{
|
||||
$this->logger->debug('Updating Database');
|
||||
|
||||
$old_ip = $this->getOld();
|
||||
$this->logger->debug($old_ip);
|
||||
$this->logger->debug("Old IP: {$old_ip}");
|
||||
|
||||
if ($old_ip === $ip) {
|
||||
$this->logger->debug('No change in IP');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->doUpdate();
|
||||
$this->doUpdate($ip);
|
||||
$this->logger->debug('Updated IP');
|
||||
}
|
||||
|
||||
protected function getOld(): string
|
||||
{
|
||||
$query = "SELECT `ip` FROM `{$this->table}` WHERE `host` = ?";
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement = $this->connector->connect()->prepare($query);
|
||||
$statement->execute(['vialdelamaza']);
|
||||
|
||||
return $statement->fetch()['ip'];
|
||||
@ -35,7 +35,7 @@ class Repository
|
||||
protected function doUpdate(string $ip): void
|
||||
{
|
||||
$query = "UPDATE `remote_ip` SET `ip` = ?, `updated` = CURRENT_TIMESTAMP() WHERE `host` = ?";
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement = $this->connector->connect()->prepare($query);
|
||||
$statement->execute([$ip, 'vialdelamaza']);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user