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;
use App\Controller\DatabaseConnection;
use App\Entity\Domain;
use App\Entity\Panel;
use PDO;
use PDOException;
@ -14,39 +13,37 @@ use PDOException;
class PanelRepository
{
public function __construct(private readonly DatabaseConnection $databaseConnection)
{}
{
// no body
}
public function findSelf(): array
/**
* @return array|null
*/
public function findSelf(): ?array
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE self = 1";
$panels = [];
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
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;
}
return $panels;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @return array
*/
public function findAll(): array
public function findAll(): ?array
{
$panels = [];
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . "
ORDER BY name";
@ -54,7 +51,7 @@ class PanelRepository
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
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;
}
return $panels;
@ -67,12 +64,12 @@ class PanelRepository
/**
* @param int $id
*
* @return null|\App\Entity\Panel
* @return null|Panel
*/
public function findByID(int $id): ?Panel
{
$sql = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM . " . DatabaseConnection::TABLE_PANELS . "
WHERE id = :id";
@ -81,7 +78,7 @@ class PanelRepository
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
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 {
return null;
}
@ -94,12 +91,12 @@ class PanelRepository
/**
* @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 = "
SELECT id, name, a, aaaa, apikey, self
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
FROM " . DatabaseConnection::TABLE_PANELS . "
WHERE name = :name";
@ -108,9 +105,9 @@ class PanelRepository
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
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 {
return false;
return null;
}
} catch (PDOException $e) {
exit($e->getMessage());
@ -119,18 +116,21 @@ class PanelRepository
/**
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return string|false
* @param Panel $panel
* @return int|null
*/
public function insert(string $name, string $a, string $aaaa, String $apikey, int $self): bool|string
public function insert(Panel $panel): ?int
{
$name = $panel->getName();
$a = $panel->getA();
$aaaa = $panel->getAaaa();
$apikey = $panel->getApikey();
$apikeyPrefix = $panel->getApikeyPrefix();
$self = $panel->getSelf();
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, self)
VALUES (:name, :a, :aaaa, :apikey, :self)";
INSERT INTO " . DatabaseConnection::TABLE_PANELS . " (name, a, aaaa, apikey, apikey_prefix, self)
VALUES (:name, :a, :aaaa, :apikey, :prefix, :self)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
@ -138,10 +138,11 @@ class PanelRepository
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->bindParam(param: ':apikey', var: $apikey);
$statement->bindParam(param: ':prefix', var: $apikeyPrefix);
$statement->bindParam(param: ':self', var: $self);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
return intval(value: $this->databaseConnection->getConnection()->lastInsertId());
} catch (PDOException $e) {
exit($e->getMessage());
}
@ -149,18 +150,22 @@ class PanelRepository
/**
* @param Int $id
* @param String $name
* @param String $a
* @param String $aaaa
* @param String $apikey
*
* @return false|int
* @param Panel $panel
* @return int|null
*/
public function update(int $id, string $name, string $a, string $aaaa, String $apikey, int $self): bool|int
public function update(Panel $panel): ?int
{
$id = $panel->getId();
$name = $panel->getName();
$a = $panel->getA();
$aaaa = $panel->getAaaa();
$apikey = $panel->getApikey();
$apikeyPrefix = $panel->getApikeyPrefix();
$passphrase = $panel->getPassphrase();
$current = $this->findByID(id: $id);
if (empty($name)) {
$name = $current->getName();
}
@ -170,25 +175,24 @@ class PanelRepository
if (empty($aaaa)) {
$aaaa = $current->getAaaa();
}
if (empty($apikey)) {
if (empty($passphrase)) {
$apikey = $current->getApikey();
$apikeyPrefix = $current->getApikeyPrefix();
}
if (empty($self)) {
echo "self is empty";
$self = $current->getSelf();
} else {
if ($self == -1) {
$self = 0;
}
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
name = :name,
a = :a,
aaaa = :aaaa,
apikey = :apikey,
apikey_prefix = :apikey_prefix,
self = :self
WHERE id = :id";
@ -199,13 +203,14 @@ class PanelRepository
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->bindParam(param: 'apikey', var: $apikey);
$statement->bindParam(param: 'apikey_prefix', var: $apikeyPrefix);
$statement->bindParam(param: 'self', var: $self);
$statement->execute();
return $statement->rowCount();
return intval(value: $statement->rowCount());
} catch (PDOException $e) {
echo $e->getMessage();
return false;
return null;
}
}
@ -213,9 +218,9 @@ class PanelRepository
/**
* @param $id
*
* @return int
* @return int|null
*/
public function delete($id): int
public function delete($id): ?int
{
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
@ -226,7 +231,7 @@ class PanelRepository
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
return $statement->rowCount();
return intval(value: $statement->rowCount());
} catch (PDOException $e) {
exit($e->getMessage());
}
@ -236,9 +241,9 @@ class PanelRepository
/**
* @param String $field
*
* @return int
* @return int|null
*/
public function getLongestEntry(String $field): int
public function getLongestEntry(string $field): ?int
{
$sql = "
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;