initial commit
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
47585fccd4
commit
69cca0c2a7
245
src/Repository/DynDNSRepository.php
Normal file
245
src/Repository/DynDNSRepository.php
Normal file
@ -0,0 +1,245 @@
|
||||
<?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
|
||||
{
|
||||
public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log)
|
||||
{
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "DynDNSRepository::__construct()");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Entity\DynDNS $dynDNS
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function delete(DynDNS $dynDNS): int
|
||||
{
|
||||
$dynDNSName = $dynDNS->getName();
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "delete($dynDNSName)");
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "findAll()");
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "findByName($name)");
|
||||
}
|
||||
$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();
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "insert($dynDNSName)");
|
||||
}
|
||||
|
||||
$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();
|
||||
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "update($dynDNSnName)");
|
||||
}
|
||||
|
||||
$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
|
||||
{
|
||||
if ($this->config['debug']) {
|
||||
$this->log->debug(message: "findById($id)");
|
||||
}
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user