2022-02-01 20:38:11 +01:00
|
|
|
<?php declare(strict_types=1);
|
2022-01-31 21:05:14 +01:00
|
|
|
|
|
|
|
namespace App\Repository;
|
|
|
|
|
|
|
|
use App\Controller\DatabaseConnection;
|
|
|
|
use App\Entity\Panel;
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class PanelRepository
|
|
|
|
{
|
2022-09-22 19:11:04 +02:00
|
|
|
public function __construct(private readonly DatabaseConnection $databaseConnection)
|
|
|
|
{
|
|
|
|
// no body
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
public function findSelf(): ?array
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
|
2022-09-17 15:40:44 +02:00
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE self = 1";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
$panels = [];
|
|
|
|
|
|
|
|
$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'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
|
|
|
|
$panels[] = $panel;
|
|
|
|
}
|
|
|
|
return $panels;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function findAll(): ?array
|
|
|
|
{
|
|
|
|
$panels = [];
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
ORDER BY name";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
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'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
|
|
|
|
$panels[] = $panel;
|
|
|
|
}
|
|
|
|
return $panels;
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
*
|
|
|
|
* @return null|Panel
|
|
|
|
*/
|
|
|
|
public function findByID(int $id): ?Panel
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM . " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE id = :id";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$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'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $name
|
|
|
|
*
|
|
|
|
* @return Panel|null
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): ?Panel
|
|
|
|
{
|
|
|
|
$sql = "
|
|
|
|
SELECT id, name, a, aaaa, apikey, apikey_prefix, self
|
2022-01-31 21:05:14 +01:00
|
|
|
FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE name = :name";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$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'], apikeyPrefix: $result['apikey_prefix'], self: $result['self']);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Panel $panel
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
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, apikey_prefix, self)
|
|
|
|
VALUES (:name, :a, :aaaa, :apikey, :prefix, :self)";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: ':name', var: $name);
|
|
|
|
$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 intval(value: $this->databaseConnection->getConnection()->lastInsertId());
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Panel $panel
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
if (empty($a)) {
|
|
|
|
$a = $current->getA();
|
|
|
|
}
|
|
|
|
if (empty($aaaa)) {
|
|
|
|
$aaaa = $current->getAaaa();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($passphrase)) {
|
|
|
|
$apikey = $current->getApikey();
|
|
|
|
$apikeyPrefix = $current->getApikeyPrefix();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($self)) {
|
|
|
|
$self = $current->getSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "
|
2022-01-31 21:05:14 +01:00
|
|
|
UPDATE " . DatabaseConnection::TABLE_PANELS . " SET
|
|
|
|
name = :name,
|
|
|
|
a = :a,
|
|
|
|
aaaa = :aaaa,
|
2022-09-17 15:40:44 +02:00
|
|
|
apikey = :apikey,
|
2022-09-22 19:11:04 +02:00
|
|
|
apikey_prefix = :apikey_prefix,
|
2022-09-17 15:40:44 +02:00
|
|
|
self = :self
|
2022-01-31 21:05:14 +01:00
|
|
|
WHERE id = :id";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->bindParam(param: 'name', var: $name);
|
|
|
|
$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 intval(value: $statement->rowCount());
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function delete($id): ?int
|
|
|
|
{
|
|
|
|
$sql = "
|
2022-01-31 21:05:14 +01:00
|
|
|
DELETE FROM " . DatabaseConnection::TABLE_PANELS . "
|
|
|
|
WHERE id = :id";
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->bindParam(param: 'id', var: $id);
|
|
|
|
$statement->execute();
|
|
|
|
|
|
|
|
return intval(value: $statement->rowCount());
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function getLongestEntry(string $field): ?int
|
|
|
|
{
|
|
|
|
$sql = "
|
2022-01-31 21:05:14 +01:00
|
|
|
SELECT MAX(LENGTH(" . $field . ")) as length FROM " . DatabaseConnection::TABLE_PANELS;
|
2022-09-22 19:11:04 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
|
|
|
$statement->execute();
|
|
|
|
$result = $statement->fetch();
|
|
|
|
return $result['length'];
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
exit($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 10:58:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
public function getSelf(): ?Panel
|
|
|
|
{
|
|
|
|
$panels = $this->findAll();
|
|
|
|
foreach ($panels as $panel) {
|
|
|
|
if ($panel->getSelf() === 'yes') {
|
|
|
|
return $panel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-17 15:40:44 +02:00
|
|
|
}
|