From 53e942f07760b884e5a5d7c263f7f850e3a6fadf Mon Sep 17 00:00:00 2001 From: tracer Date: Sat, 22 Jan 2022 18:25:18 +0100 Subject: [PATCH] replaced PDO with DatabaseConnection Signed-off-by: tracer --- bindAPI/src/Controller/DomainController.php | 35 +++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/bindAPI/src/Controller/DomainController.php b/bindAPI/src/Controller/DomainController.php index 23ea3e4..b12dfea 100644 --- a/bindAPI/src/Controller/DomainController.php +++ b/bindAPI/src/Controller/DomainController.php @@ -15,7 +15,7 @@ class DomainController private String $namedConfLocalFile; private string $zoneCachePath; - public function __construct(private PDO $dbConnection) + public function __construct(private DatabaseConnection $databaseConnection) { $this->localZoneFile = '/etc/bind/local.zones'; $this->localZonesDir = '/etc/bind/zones/'; @@ -31,10 +31,10 @@ class DomainController { $statement = " SELECT id, name, a, aaaa - FROM domains"; + FROM " . DatabaseConnection::TABLE_DOMAINS; try { - $statement = $this->dbConnection->query($statement); + $statement = $this->databaseConnection->getConnection()->query($statement); return $statement->fetchAll(mode: PDO::FETCH_ASSOC); } catch (PDOException $e) { exit($e->getMessage()); @@ -51,11 +51,11 @@ class DomainController { $sql = " SELECT id, name, a, aaaa - FROM domains + FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE name = :name"; try { - $statement = $this->dbConnection->prepare($sql); + $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':name', var: $name); $statement->execute(); return $statement->fetch(PDO::FETCH_ASSOC); @@ -74,11 +74,11 @@ class DomainController { $sql = " SELECT id, name, a, aaaa - FROM domains + FROM . " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; try { - $statement = $this->dbConnection->prepare($sql); + $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param:':id', var: $id); $statement->execute(); return $statement->fetch(PDO::FETCH_ASSOC); @@ -99,11 +99,11 @@ class DomainController { // TODO create zone file and include $sql = " - INSERT INTO domains (name, a, aaaa) + INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, a, aaaa) VALUES (:name, :a, :aaaa)"; try { - $statement = $this->dbConnection->prepare($sql); + $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':a', var: $a); $statement->bindParam(param: ':aaaa', var: $aaaa); @@ -121,7 +121,7 @@ class DomainController exit(1); } - return $this->dbConnection->lastInsertId(); + return $this->databaseConnection->getConnection()->lastInsertId(); } catch (PDOException $e) { exit($e->getMessage()); } @@ -135,9 +135,9 @@ class DomainController * @param String $a * @param String $aaaa * - * @return int|void + * @return false|int */ - public function update(Int $id, String $name, String $a, String $aaaa) + public function update(Int $id, String $name, String $a, String $aaaa): bool|int { $current = $this->findByID($id); @@ -162,14 +162,14 @@ class DomainController } $sql = " - UPDATE domains SET + UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET name = :name, a = :a, aaaa = :aaaa WHERE id = :id"; try { - $statement = $this->dbConnection->prepare($sql); + $statement = $this->databaseConnection->getConnection()->prepare($sql); $statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'a', var: $a); @@ -178,7 +178,7 @@ class DomainController // recreate zonefile $this->createZoneFile(name: $name, a: $a, aaaa: $aaaa); - exec('/usr/sbin/rndc reload'); + exec(command: '/usr/sbin/rndc reload'); return $statement->rowCount(); } catch (PDOException $e) { @@ -197,11 +197,11 @@ class DomainController { // TODO delete zone file and include $statement = " - DELETE FROM domains + DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; try { - $statement = $this->dbConnection->prepare($statement); + $statement = $this->databaseConnection->getConnection()->prepare($statement); $statement->bindParam(param: 'id', var: $id); $statement->execute(); return $statement->rowCount(); @@ -251,6 +251,7 @@ class DomainController } } + /** * @param mixed $name * @param mixed $a