added orderBy to findAll()

This commit is contained in:
tracer 2022-10-23 12:40:40 +02:00
parent d19e7d5b25
commit 9a485b5424
1 changed files with 43 additions and 119 deletions

View File

@ -1,21 +1,25 @@
<?php <?php
/*
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace App\Repository; namespace App\Repository;
use App\Service\Config;
use App\Service\DatabaseConnection; use App\Service\DatabaseConnection;
use App\Entity\User; use App\Entity\User;
use PDO; use PDO;
use PDOException; use PDOException;
/** /**
* * Handles CRUD od User class.
*/ */
class DomainRepository class UserRepository
{ {
public function __construct( public function __construct(private readonly DatabaseConnection $databaseConnection)
private readonly DatabaseConnection $databaseConnection,
private readonly Config $configController)
{ {
// empty body // empty body
} }
@ -24,40 +28,35 @@ class DomainRepository
/** /**
* @return array * @return array
*/ */
public function findAll(): array public function findAll(string $orderBy = 'nick'): array
{ {
$users = []; $users = [];
$sql = " $sql = "
SELECT id, nick, first, last, is_admin SELECT id, nick, first, last, is_admin
FROM " . DatabaseConnection::TABLE_USERS . " FROM " . DatabaseConnection::TABLE_USERS . "
ORDER BY name"; ORDER BY :order";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':order', var: $order);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$domain = new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); $user = new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
$domains[] = $domain; $users[] = $user;
} }
return $domains; return $users;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** public function findByID(int $id): ?User
* @param int $id
*
* @return bool|\App\Entity\Domain
*/
public function findByID(int $id): bool|Domain
{ {
$this->logger->debug(message: "findById($id)");
$sql = " $sql = "
SELECT id, name, panel SELECT id, nick, first, last, is_admin
FROM . " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_USERS . "
WHERE id = :id"; WHERE id = :id";
try { try {
@ -65,85 +64,41 @@ 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 User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
return new Domain(name: $result['name'], panel: $result['panel'], id: $result['id']);
} else { } else {
return false; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
public function findByNick(string $nick): ?User
/**
* @param String $name
*
* @return \App\Entity\Domain|bool
*/
public function findByName(string $name): Domain|bool
{ {
$this->logger->debug(message: "findByName($name)");
$sql = " $sql = "
SELECT id, name, panel SELECT id, nick, first, last, is_admin
FROM " . DatabaseConnection::TABLE_DOMAINS . " FROM " . DatabaseConnection::TABLE_USERS . "
WHERE name = :name"; WHERE nick = :nick";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':nick', var: $nick);
$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'], panel: $result['panel'], id: $result['id']); return new User(nick: $result['nick'], first: $result['first'], last: $result['last'], id: $result['id']);
} else { } else {
return false; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
public function insert(User $user): bool|string
/**
* @param string $host
*
* @return \App\Entity\Domain|bool
*/
public function findByHost(string $host): Domain|bool
{ {
$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 = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel) INSERT INTO " . DatabaseConnection::TABLE_USERS . " (name, panel)
VALUES (:name, :panel)"; VALUES (:name, :panel)";
try { try {
@ -158,22 +113,17 @@ class DomainRepository
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
*/
return false;
} }
/** public function update(User $user): bool|int
* @param \App\Entity\Domain $domain
*
* @return false|int
*/
public function update(Domain $domain): bool|int
{ {
$domainName = $domain->getName(); $id = $user->getId();
$this->logger->debug(message: "update($domainName)");
$id = $domain->getId();
$current = $this->findByID(id: $id); $current = $this->findByID(id: $id);
/*
if (empty($domain->getName())) { if (empty($domain->getName())) {
$name = $current->getName(); $name = $current->getName();
} else { } else {
@ -186,7 +136,7 @@ class DomainRepository
} }
$sql = " $sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET UPDATE " . DatabaseConnection::TABLE_USER . " SET
name = :name, name = :name,
panel = :panel panel = :panel
WHERE id = :id"; WHERE id = :id";
@ -203,26 +153,20 @@ class DomainRepository
echo $e->getMessage(); echo $e->getMessage();
return false; return false;
} }
*/
return false;
} }
/** public function delete(User $user): int
* @param \App\Entity\Domain $domain
*
* @return int
*/
public function delete(Domain $domain): int
{ {
$domainName = $domain->getName();
$this->logger->debug(message: "delete($domainName)");
$sql = " $sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " DELETE FROM " . DatabaseConnection::TABLE_USERS . "
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(); $id = $user->getId();
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $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());
}
}
} }