added logger

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-02-06 17:42:26 +01:00
parent b2452d349a
commit 6665933a41
1 changed files with 34 additions and 20 deletions

View File

@ -4,6 +4,7 @@ namespace App\Repository;
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Entity\Domain; use App\Entity\Domain;
use Monolog\Logger;
use PDO; use PDO;
use PDOException; use PDOException;
@ -12,7 +13,7 @@ use PDOException;
*/ */
class DomainRepository class DomainRepository
{ {
public function __construct(private DatabaseConnection $databaseConnection) public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log)
{} {}
@ -58,6 +59,7 @@ class DomainRepository
$statement->bindParam(param: ':id', var: $id); $statement->bindParam(param: ':id', var: $id);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} else { } else {
return false; return false;
@ -85,7 +87,7 @@ class DomainRepository
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']); return new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} else { } else {
return false; return false;
@ -97,23 +99,28 @@ class DomainRepository
} }
/** /**
* @param String $name * @param \App\Entity\Domain $domain
* @param int $panelID
* @param String $a
* @param String $aaaa
* *
* @return string|false * @return string|false
*/ */
public function insert(string $name, int $panelID, string $a, string $aaaa): bool|string public function insert(Domain $domain): bool|string
{ {
print("here"); if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "insert($domainName)");
}
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa) INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)"; VALUES (:name, :panel_id, :a, :aaaa)";
try { try {
$name = $domain->getName();
$panelID = $domain->getPanelID();
$a = $domain->getA();
$aaaa = $domain->getAaaa();
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_id', var: $panelID); $statement->bindParam(param: ':panel_id', var: $panelID);
@ -129,17 +136,18 @@ class DomainRepository
/** /**
* @param Int $id * @param \App\Entity\Domain $domain
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
* *
* @return false|int * @return false|int
*/ */
public function update(int $id, string $name, int $panelID, string $a, string $aaaa): bool|int public function update(Domain $domain): bool|int
{ {
$current = $this->findByID(id: $id); if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "update($domainName)");
}
$current = $this->findByID(id: $domain->getId());
/* doesn't work /* doesn't work
$statement = " $statement = "
@ -151,10 +159,10 @@ class DomainRepository
aaaa=COALESCE(:aaaa, aaaa)"; aaaa=COALESCE(:aaaa, aaaa)";
*/ */
if (empty($name)) { if (empty($domain->getName())) {
$name = $current['name']; $name = $current['name'];
} }
if (empty($panelID)) { if (empty($domain->getPanelID())) {
$panelID = $current['panel_id']; $panelID = $current['panel_id'];
} }
$panelID = intval(value: $panelID); $panelID = intval(value: $panelID);
@ -191,18 +199,24 @@ class DomainRepository
/** /**
* @param $id * @param \App\Entity\Domain $domain
* *
* @return int * @return int
*/ */
public function delete($id): int public function delete(Domain $domain): int
{ {
if ($this->config['debug']) {
$domainName = $domain->getName();
$this->log->debug(message: "delete($domainName)");
}
$sql = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$id = $domain->getId();
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();