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