diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index cd559dd..2430c9e 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -1,21 +1,25 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + */ namespace App\Repository; -use App\Service\Config; use App\Service\DatabaseConnection; use App\Entity\User; use PDO; use PDOException; /** - * + * Handles CRUD od User class. */ -class DomainRepository +class UserRepository { - public function __construct( - private readonly DatabaseConnection $databaseConnection, - private readonly Config $configController) + public function __construct(private readonly DatabaseConnection $databaseConnection) { // empty body } @@ -24,40 +28,35 @@ class DomainRepository /** * @return array */ - public function findAll(): array + public function findAll(string $orderBy = 'nick'): array { $users = []; $sql = " SELECT id, nick, first, last, is_admin FROM " . DatabaseConnection::TABLE_USERS . " - ORDER BY name"; + ORDER BY :order"; try { $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); + $statement->bindParam(param: ':order', var: $order); + $statement->execute(); while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { - $domain = new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); - $domains[] = $domain; + $user = new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']); + $users[] = $user; } - return $domains; + return $users; } catch (PDOException $e) { exit($e->getMessage()); } } - /** - * @param int $id - * - * @return bool|\App\Entity\Domain - */ - public function findByID(int $id): bool|Domain + public function findByID(int $id): ?User { - $this->logger->debug(message: "findById($id)"); - $sql = " - SELECT id, name, panel - FROM . " . DatabaseConnection::TABLE_DOMAINS . " + SELECT id, nick, first, last, is_admin + FROM " . DatabaseConnection::TABLE_USERS . " WHERE id = :id"; try { @@ -65,85 +64,41 @@ class DomainRepository $statement->bindParam(param: ':id', var: $id); $statement->execute(); if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { - - return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); + return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']); } else { - return false; + return null; } } catch (PDOException $e) { exit($e->getMessage()); } } - - /** - * @param String $name - * - * @return \App\Entity\Domain|bool - */ - public function findByName(string $name): Domain|bool + public function findByNick(string $nick): ?User { - $this->logger->debug(message: "findByName($name)"); - $sql = " - SELECT id, name, panel - FROM " . DatabaseConnection::TABLE_DOMAINS . " - WHERE name = :name"; + SELECT id, nick, first, last, is_admin + FROM " . DatabaseConnection::TABLE_USERS . " + WHERE nick = :nick"; try { $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); - $statement->bindParam(param: ':name', var: $name); + $statement->bindParam(param: ':nick', var: $nick); $statement->execute(); if ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { - return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); + return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']); } else { - return false; + return null; } } catch (PDOException $e) { exit($e->getMessage()); } } - - /** - * @param string $host - * - * @return \App\Entity\Domain|bool - */ - public function findByHost(string $host): Domain|bool + public function insert(User $user): bool|string { - $this->logger->debug(message: "findByHost($host)"); - - $host = strtolower(string: trim(string: $host)); - $count = substr_count(haystack: $host, needle: '.'); - if ($count == 2) { - if (strlen(string: explode(separator: '.', string: $host)[1]) > 3) { - $host = explode(separator: '.', string: $host, limit: 2)[1]; - } - } elseif ($count > 2) { - $host = $this->findByHost(host: explode(separator: '.', string: $host, limit: 2)[1]); - } - - if ($domain = $this->findByName(name: $host)) { - return $domain; - } else { - return false; - } - } - - - /** - * @param \App\Entity\Domain $domain - * - * @return string|false - */ - public function insert(Domain $domain): bool|string - { - $domainName = $domain->getName(); - $this->logger->info(message: "insert($domainName)"); - + /* $sql = " - INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel) + INSERT INTO " . DatabaseConnection::TABLE_USERS . " (name, panel) VALUES (:name, :panel)"; try { @@ -158,22 +113,17 @@ class DomainRepository } catch (PDOException $e) { exit($e->getMessage()); } + */ + return false; } - /** - * @param \App\Entity\Domain $domain - * - * @return false|int - */ - public function update(Domain $domain): bool|int + public function update(User $user): bool|int { - $domainName = $domain->getName(); - $this->logger->debug(message: "update($domainName)"); - - $id = $domain->getId(); + $id = $user->getId(); $current = $this->findByID(id: $id); + /* if (empty($domain->getName())) { $name = $current->getName(); } else { @@ -186,7 +136,7 @@ class DomainRepository } $sql = " - UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET + UPDATE " . DatabaseConnection::TABLE_USER . " SET name = :name, panel = :panel WHERE id = :id"; @@ -203,26 +153,20 @@ class DomainRepository echo $e->getMessage(); return false; } + */ + return false; } - /** - * @param \App\Entity\Domain $domain - * - * @return int - */ - public function delete(Domain $domain): int + public function delete(User $user): int { - $domainName = $domain->getName(); - $this->logger->debug(message: "delete($domainName)"); - $sql = " - DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " + DELETE FROM " . DatabaseConnection::TABLE_USERS . " WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); - $id = $domain->getId(); + $id = $user->getId(); $statement->bindParam(param: 'id', var: $id); $statement->execute(); @@ -232,24 +176,4 @@ class DomainRepository } } - - /** - * @param String $field - * - * @return int - */ - public function getLongestEntry(string $field): int - { - $sql = " - SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_DOMAINS; - - try { - $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); - $statement->execute(); - $result = $statement->fetch(); - return $result['length']; - } catch (PDOException $e) { - exit($e->getMessage()); - } - } } \ No newline at end of file