2022-04-06 16:23:57 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Controller\DatabaseConnection;
|
|
|
|
use App\Entity\DynDNS;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class DynDNSRepository
|
|
|
|
{
|
2022-09-17 16:29:58 +02:00
|
|
|
public function __construct(
|
|
|
|
private readonly DatabaseConnection $databaseConnection,
|
|
|
|
private readonly Logger $logger)
|
2022-04-06 16:23:57 +02:00
|
|
|
{
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "DynDNSRepository::__construct()");
|
2022-04-06 16:23:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \App\Entity\DynDNS $dynDNS
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function delete(DynDNS $dynDNS): int
|
|
|
|
{
|
|
|
|
$dynDNSName = $dynDNS->getName();
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "delete($dynDNSName)");
|
2022-04-06 16:23:57 +02:00
|
|
|
|
|
|
|
$sql = "
|
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_DYNDNS . "
|
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$id = $dynDNS->getId();
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $statement->rowCount();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function findAll(): array
|
|
|
|
{
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "findAll()");
|
2022-04-06 16:23:57 +02:00
|
|
|
|
|
|
|
$dyndns = [];
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa
|
|
|
|
FROM " . DatabaseConnection::TABLE_DYNDNS . "
|
|
|
|
ORDER BY name";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
|
|
$dyndns = new DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
|
|
|
|
$dyndns[] = $dyndns;
|
|
|
|
}
|
|
|
|
return $dyndns;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
*
|
|
|
|
* @return \App\Entity\Domain|bool
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): DynDNS|bool
|
|
|
|
{
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "findByName($name)");
|
|
|
|
|
2022-04-06 16:23:57 +02:00
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa
|
|
|
|
FROM " . DatabaseConnection::TABLE_DYNDNS . "
|
|
|
|
WHERE name = :name";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
$statement->execute();
|
|
|
|
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
|
|
return new DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLongestEntry(string $field): int
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DYNDNS;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetch();
|
|
|
|
return $result['length'];
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \App\Entity\DynDNS $dynDNS
|
|
|
|
*
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
public function insert(DynDNS $dynDNS): bool|string
|
|
|
|
{
|
|
|
|
$dynDNSName = $dynDNS->getName();
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "insert($dynDNSName)");
|
2022-04-06 16:23:57 +02:00
|
|
|
|
|
|
|
$sql = "
|
|
|
|
INSERT INTO " . DatabaseConnection::TABLE_DYNDNS . " (name, a, aaaa)
|
|
|
|
VALUES (:name, :a, :aaaa)";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$a = $dynDNS->getA();
|
|
|
|
$aaaa = $dynDNS->getAaaa();
|
|
|
|
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':name', var: $dynDNSName);
|
|
|
|
$statement->bindParam(param: ':a', var: $a);
|
|
|
|
$statement->bindParam(param: ':aaaa', var: $aaaa);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $this->databaseConnection->getConnection()->lastInsertId();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \App\Entity\DynDNS $dynDNS
|
|
|
|
*
|
|
|
|
* @return false|int
|
|
|
|
*/
|
|
|
|
public function update(DynDNS $dynDNS): bool|int
|
|
|
|
{
|
|
|
|
$dynDNSnName = $dynDNS->getName();
|
|
|
|
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "update($dynDNSnName)");
|
2022-04-06 16:23:57 +02:00
|
|
|
|
|
|
|
$id = $dynDNS->getId();
|
|
|
|
$current = $this->findByID(id: $id);
|
|
|
|
|
|
|
|
if (empty($dynDNSnName)) {
|
|
|
|
$name = $current->getName();
|
|
|
|
} else {
|
|
|
|
$name = $dynDNSnName;
|
|
|
|
}
|
|
|
|
if (empty($dynDNS->getA())) {
|
|
|
|
$a = $current->getA();
|
|
|
|
} else {
|
|
|
|
$a = $dynDNS->getA();
|
|
|
|
}
|
|
|
|
if (empty($dynDNS->getAaaa())) {
|
|
|
|
$aaaa = $current->getAaaa();
|
|
|
|
} else {
|
|
|
|
$aaaa = $dynDNS->getAaaa();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "
|
|
|
|
UPDATE " . DatabaseConnection::TABLE_DYNDNS . " SET
|
|
|
|
name = :name,
|
|
|
|
a = :a,
|
|
|
|
aaaa = :aaaa
|
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->bindParam(param: 'name', var: $name);
|
|
|
|
$statement->bindParam(param: 'a', var: $a);
|
|
|
|
$statement->bindParam(param: 'aaaa', var: $aaaa);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return $statement->rowCount();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return bool|\App\Entity\Domain
|
|
|
|
*/
|
|
|
|
public function findByID(int $id): bool|DynDNS
|
|
|
|
{
|
2022-09-17 16:29:58 +02:00
|
|
|
$this->logger->debug(message: "findById($id)");
|
2022-04-06 16:23:57 +02:00
|
|
|
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa
|
|
|
|
FROM . " . DatabaseConnection::TABLE_DYNDNS . "
|
|
|
|
WHERE id = :id";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':id', var: $id);
|
|
|
|
$statement->execute();
|
|
|
|
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
|
|
|
|
return new DynDNS(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], id: $result['id']);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 16:29:58 +02:00
|
|
|
}
|