added check for debugging

This commit is contained in:
tracer 2022-09-17 15:46:25 +02:00
parent 051e7cbd0b
commit 46fdadf8e5
1 changed files with 22 additions and 35 deletions

View File

@ -2,6 +2,7 @@
namespace App\Repository; namespace App\Repository;
use App\Controller\ConfigController;
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Entity\Domain; use App\Entity\Domain;
use Monolog\Logger; use Monolog\Logger;
@ -13,11 +14,12 @@ use PDOException;
*/ */
class DomainRepository class DomainRepository
{ {
public function __construct(private DatabaseConnection $databaseConnection, private array $config, private Logger $log) public function __construct(
private readonly DatabaseConnection $databaseConnection,
private readonly ConfigController $configController,
private readonly Logger $logger)
{ {
if ($this->config['debug']) { $this->logger->debug(message: "DomainRepository::__construct()");
$this->log->debug(message: "DomainRepository::__construct()");
}
} }
@ -27,9 +29,7 @@ class DomainRepository
*/ */
public function findAll(): array public function findAll(): array
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findAll()");
$this->log->debug(message: "findAll()");
}
$domains = []; $domains = [];
$sql = " $sql = "
@ -58,9 +58,7 @@ class DomainRepository
*/ */
public function findByID(int $id): bool|Domain public function findByID(int $id): bool|Domain
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findById($id)");
$this->log->debug(message: "findById($id)");
}
$sql = " $sql = "
SELECT id, name, panel SELECT id, name, panel
@ -90,9 +88,8 @@ class DomainRepository
*/ */
public function findByName(string $name): Domain|bool public function findByName(string $name): Domain|bool
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findByName($name)");
$this->log->debug(message: "findByName($name)");
}
$sql = " $sql = "
SELECT id, name, panel SELECT id, name, panel
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -113,17 +110,14 @@ class DomainRepository
} }
/** /**
* @param String $name * @param string $host
* *
* @return \App\Entity\Domain|bool * @return \App\Entity\Domain|bool
*/ */
public function findByHost(string $host): Domain|bool public function findByHost(string $host): Domain|bool
{ {
if ($this->config['debug']) { $this->logger->debug(message: "findByHost($host)");
$this->log->debug(message: "findByHost($host)");
}
$host = strtolower(string: trim(string: $host)); $host = strtolower(string: trim(string: $host));
$count = substr_count(haystack: $host, needle: '.'); $count = substr_count(haystack: $host, needle: '.');
@ -143,7 +137,6 @@ class DomainRepository
} }
/** /**
* @param \App\Entity\Domain $domain * @param \App\Entity\Domain $domain
* *
@ -151,10 +144,8 @@ class DomainRepository
*/ */
public function insert(Domain $domain): bool|string public function insert(Domain $domain): bool|string
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->info(message: "insert($domainName)");
$this->log->debug(message: "insert($domainName)");
}
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel) INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel)
@ -182,10 +173,8 @@ class DomainRepository
*/ */
public function update(Domain $domain): bool|int public function update(Domain $domain): bool|int
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->debug(message: "update($domainName)");
$this->log->debug(message: "update($domainName)");
}
$id = $domain->getId(); $id = $domain->getId();
$current = $this->findByID(id: $id); $current = $this->findByID(id: $id);
@ -229,10 +218,8 @@ class DomainRepository
*/ */
public function delete(Domain $domain): int public function delete(Domain $domain): int
{ {
if ($this->config['debug']) { $domainName = $domain->getName();
$domainName = $domain->getName(); $this->logger->debug(message: "delete($domainName)");
$this->log->debug(message: "delete($domainName)");
}
$sql = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
@ -256,7 +243,7 @@ class DomainRepository
* *
* @return int * @return int
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(string $field): int
{ {
$sql = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS;
@ -264,7 +251,7 @@ class DomainRepository
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
$result = $statement->fetch(); $result = $statement->fetch();
return $result['length']; return $result['length'];
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());