v0.5.0
This commit is contained in:
36
app/common/Command/UpdateIp.php
Normal file
36
app/common/Command/UpdateIp.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
namespace ProVM\Command;
|
||||
|
||||
use Exception;
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ProVM\Service\Remote;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'update',
|
||||
hidden: false
|
||||
)]
|
||||
class UpdateIp extends Command
|
||||
{
|
||||
public function __construct(protected Remote $service, protected LoggerInterface $logger, ?string $name = 'update')
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
try {
|
||||
$this->service->update();
|
||||
return Command::SUCCESS;
|
||||
} catch (PDOException $e) {
|
||||
$this->logger->warning($e);
|
||||
return Command::FAILURE;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
40
app/common/Command/Watch.php
Normal file
40
app/common/Command/Watch.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace ProVM\Command;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateInterval;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'watch',
|
||||
hidden: false
|
||||
)]
|
||||
class Watch extends Command
|
||||
{
|
||||
public function __construct(protected string $period, ?string $name = 'watch')
|
||||
{
|
||||
parent::__construct($name);
|
||||
}
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$period = new DateInterval($this->period);
|
||||
$current = new DateTimeImmutable();
|
||||
while(true) {
|
||||
$now = new DateTimeImmutable();
|
||||
if ($now->diff($current) === $period) {
|
||||
$this->runUpdate();
|
||||
$current = $now;
|
||||
}
|
||||
}
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
protected function runUpdate(): void
|
||||
{
|
||||
$command = '/app/bin/console update';
|
||||
shell_exec($command);
|
||||
}
|
||||
}
|
27
app/common/Service/Ipify.php
Normal file
27
app/common/Service/Ipify.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace ProVM\Service;
|
||||
|
||||
use Exception;
|
||||
use Psr\Http\Client\ClientInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use function Safe\json_decode;
|
||||
|
||||
class Ipify
|
||||
{
|
||||
public function __construct(protected ClientInterface $client, protected LoggerInterface $logger) {}
|
||||
|
||||
public function get(): string
|
||||
{
|
||||
$this->logger->debug('Getting IP');
|
||||
$response = $this->client->get('?format=json');
|
||||
if (round($response->getCode() / 100, 0) !== 2) {
|
||||
throw new Exception("Could not connect to '{$this->client->base_uri}'");
|
||||
}
|
||||
$body = $response->getBody();
|
||||
$json = json_decode($body->getContents());
|
||||
if (!isset($json->ip)) {
|
||||
throw new Exception('Missing `ip` in JSON response');
|
||||
}
|
||||
return $json->ip;
|
||||
}
|
||||
}
|
13
app/common/Service/Remote.php
Normal file
13
app/common/Service/Remote.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace ProVM\Service;
|
||||
|
||||
class Remote
|
||||
{
|
||||
public function __construct(protected Ipify $ipService, protected Repository $dbService) {}
|
||||
|
||||
public function update(): void
|
||||
{
|
||||
$ip = $this->ipService->get();
|
||||
$this->dbService->update($ip);
|
||||
}
|
||||
}
|
41
app/common/Service/Repository.php
Normal file
41
app/common/Service/Repository.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace ProVM\Service;
|
||||
|
||||
use PDO;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Repository
|
||||
{
|
||||
public function __construct(protected PDO $connection, 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);
|
||||
|
||||
if ($old_ip === $ip) {
|
||||
$this->logger->debug('No change in IP');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->doUpdate();
|
||||
$this->logger->debug('Updated IP');
|
||||
}
|
||||
|
||||
protected function getOld(): string
|
||||
{
|
||||
$query = "SELECT `ip` FROM `{$this->table}` WHERE `host` = ?";
|
||||
$statement = $this->connection->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->connection->prepare($query);
|
||||
$statement->execute([$ip, 'vialdelamaza']);
|
||||
}
|
||||
}
|
19
app/common/Wrapper/App.php
Normal file
19
app/common/Wrapper/App.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace ProVM\Wrapper;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\Console\Application;
|
||||
|
||||
class App extends Application
|
||||
{
|
||||
protected ContainerInterface $container;
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
public function setContainer(ContainerInterface $container): App
|
||||
{
|
||||
$this->container = $container;
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user