42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
namespace ProVM\Service;
|
|
|
|
use PDO;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Repository
|
|
{
|
|
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: {$old_ip}");
|
|
|
|
if ($old_ip === $ip) {
|
|
$this->logger->debug('No change in IP');
|
|
return;
|
|
}
|
|
|
|
$this->doUpdate($ip);
|
|
$this->logger->debug('Updated IP');
|
|
}
|
|
|
|
protected function getOld(): string
|
|
{
|
|
$query = "SELECT `ip` FROM `{$this->table}` WHERE `host` = ?";
|
|
$statement = $this->connector->connect()->prepare($query);
|
|
$statement->execute(['vialdelamaza']);
|
|
|
|
return $statement->fetch()['ip'];
|
|
}
|
|
protected function doUpdate(string $ip): void
|
|
{
|
|
$query = "UPDATE `remote_ip` SET `ip` = ?, `updated` = CURRENT_TIMESTAMP() WHERE `host` = ?";
|
|
$statement = $this->connector->connect()->prepare($query);
|
|
$statement->execute([$ip, 'vialdelamaza']);
|
|
}
|
|
}
|