made all methods use objects instead of single parameters

This commit is contained in:
tracer 2022-09-22 19:11:04 +02:00
parent 51b9e67ea9
commit 8f5a57ee54
1 changed files with 227 additions and 222 deletions

View File

@ -3,7 +3,6 @@
namespace App\Repository; namespace App\Repository;
use App\Controller\DatabaseConnection; use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel; use App\Entity\Panel;
use PDO; use PDO;
use PDOException; use PDOException;
@ -13,243 +12,249 @@ use PDOException;
*/ */
class PanelRepository class PanelRepository
{ {
public function __construct(private readonly DatabaseConnection $databaseConnection) public function __construct(private readonly DatabaseConnection $databaseConnection)
{} {
// no body
public function findSelf(): array }
{
$sql = " /**
SELECT id, name, a, aaaa, apikey, self * @return array|null
*/
public function findSelf(): ?array
{
$sql = "
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1"; WHERE self = 1";
$panels = []; $panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} catch (PDOException $e) { }
exit($e->getMessage());
}
public function findAll(): ?array
{
} $panels = [];
$sql = "
/** SELECT id, name, a, aaaa, apikey, apikey_prefix, self
* @return array
*/
public function findAll(): array
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name"; ORDER BY name";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute(); $statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) { while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); $panel = new Panel(name: $result['name'], id: $result['id'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
$panels[] = $panel; $panels[] = $panel;
} }
return $panels; return $panels;
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param int $id * @param int $id
* *
* @return null|\App\Entity\Panel * @return null|Panel
*/ */
public function findByID(int $id): ?Panel public function findByID(int $id): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, self SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM . " . DatabaseConnection::TABLE_PANELS . " FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$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 Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
} else { } else {
return null; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $name * @param String $name
* *
* @return \App\Entity\Panel|bool * @return Panel|null
*/ */
public function findByName(string $name): Panel|bool public function findByName(string $name): ?Panel
{ {
$sql = " $sql = "
SELECT id, name, a, aaaa, apikey, self SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . " FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name"; WHERE name = :name";
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: ':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 Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], self: $result['self']); return new Panel(name: $result['name'], a: $result['a'], aaaa: $result['aaaa'], apikey: $result['apikey'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
} else { } else {
return false; return null;
} }
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $name * @param Panel $panel
* @param String $a * @return int|null
* @param String $aaaa */
* @param String $apikey public function insert(Panel $panel): ?int
* {
* @return string|false $name = $panel->getName();
*/ $a = $panel->getA();
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string $aaaa = $panel->getAaaa();
{ $apikey = $panel->getApikey();
$sql = " $apikeyPrefix = $panel->getApikeyPrefix();
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self) $self = $panel->getSelf();
VALUES (:name, :a, :aaaa, :apikey, :self)";
$sql = "
try { INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, apikey_prefix, self)
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); VALUES (:name, :a, :aaaa, :apikey, :prefix, :self)";
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':a', var: $a); try {
$statement->bindParam(param: ':aaaa', var: $aaaa); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':apikey', var: $apikey); $statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':self', var: $self); $statement->bindParam(param: ':a', var: $a);
$statement->execute(); $statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
return $this->databaseConnection->getConnection()->lastInsertId(); $statement->bindParam(param: ':prefix', var: $apikeyPrefix);
} catch (PDOException $e) { $statement->bindParam(param: ':self', var: $self);
exit($e->getMessage()); $statement->execute();
}
} return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
} catch (PDOException $e) {
exit($e->getMessage());
/** }
* @param Int $id }
* @param String $name
* @param String $a
* @param String $aaaa /**
* @param String $apikey * @param Panel $panel
* * @return int|null
* @return false|int */
*/ public function update(Panel $panel): ?int
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int {
{ $id = $panel->getId();
$current = $this->findByID(id: $id); $name = $panel->getName();
$a = $panel->getA();
if (empty($name)) { $aaaa = $panel->getAaaa();
$name = $current->getName(); $apikey = $panel->getApikey();
} $apikeyPrefix = $panel->getApikeyPrefix();
if (empty($a)) { $passphrase = $panel->getPassphrase();
$a = $current->getA();
} $current = $this->findByID(id: $id);
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
} if (empty($name)) {
if (empty($apikey)) { $name = $current->getName();
$apikey = $current->getApikey(); }
} if (empty($a)) {
$a = $current->getA();
if (empty($self)) { }
echo "self is empty"; if (empty($aaaa)) {
$self = $current->getSelf(); $aaaa = $current->getAaaa();
} else { }
if ($self == -1) {
$self = 0; if (empty($passphrase)) {
} $apikey = $current->getApikey();
} $apikeyPrefix = $current->getApikeyPrefix();
}
$sql = "
if (empty($self)) {
$self = $current->getSelf();
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name, name = :name,
a = :a, a = :a,
aaaa = :aaaa, aaaa = :aaaa,
apikey = :apikey, apikey = :apikey,
apikey_prefix = :apikey_prefix,
self = :self self = :self
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name); $statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'a', var: $a); $statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa); $statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey); $statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'self', var: $self); $statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix);
$statement->execute(); $statement->bindParam(param: 'self', var: $self);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) { return intval(value: $statement->rowCount());
echo $e->getMessage(); } catch (PDOException $e) {
return false; echo $e->getMessage();
} return null;
} }
}
/**
* @param $id /**
* * @param $id
* @return int *
*/ * @return int|null
public function delete($id): int */
{ public function delete($id): ?int
$sql = " {
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_PANELS . " DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id"; WHERE id = :id";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id); $statement->bindParam(param: 'id', var: $id);
$statement->execute(); $statement->execute();
return $statement->rowCount(); return intval(value: $statement->rowCount());
} catch (PDOException $e) { } catch (PDOException $e) {
exit($e->getMessage()); exit($e->getMessage());
} }
} }
/** /**
* @param String $field * @param String $field
* *
* @return int * @return int|null
*/ */
public function getLongestEntry(String $field): int public function getLongestEntry(string $field): ?int
{ {
$sql = " $sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS; SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
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());
} }
} }
} }