From 69cca0c2a7b531c2dc914416a544451a112146cc Mon Sep 17 00:00:00 2001 From: tracer Date: Wed, 6 Apr 2022 16:23:57 +0200 Subject: [PATCH] initial commit Signed-off-by: tracer --- src/Repository/DynDNSRepository.php | 245 ++++++++++++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 src/Repository/DynDNSRepository.php diff --git a/src/Repository/DynDNSRepository.php b/src/Repository/DynDNSRepository.php new file mode 100644 index 0000000..d136af2 --- /dev/null +++ b/src/Repository/DynDNSRepository.php @@ -0,0 +1,245 @@ +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()); + } + } +} \ No newline at end of file