initial commit

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-31 14:33:27 +01:00
parent 073d9cae9e
commit c297eaad81
1 changed files with 240 additions and 0 deletions

View File

@ -0,0 +1,240 @@
<?php
namespace App\Repository;
use App\Controller\DatabaseConnection;
use App\Controller\PanelController;
use App\Entity\Domain;
use PDO;
use PDOException;
/**
*
*/
class DomainRepository
{
public function __construct(private DatabaseConnection $databaseConnection, private PanelController $panelController)
{}
/**
* @return array
*/
public function findAll(): array
{
$domains = [];
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
ORDER BY name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->execute();
while ($result = $statement->fetch(mode: PDO::FETCH_ASSOC)) {
$domain = new Domain(name: $result['name'], id: $result['id'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
$domains[] = $domain;
}
return $domains;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param int $id
*
* @return \App\Entity\Domain
*/
public function findByID(int $id): Domain
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM . " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':id', var: $id);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
*
* @return \App\Entity\Domain
*/
public function findByName(string $name): Domain
{
$sql = "
SELECT id, name, panel_id, a, aaaa
FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE name = :name";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->execute();
$result = $statement->fetch(mode: PDO::FETCH_ASSOC);
return new Domain(name: $result['name'], panelID: $result['panel_id'], a: $result['a'], aaaa: $result['aaaa']);
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return string|false
*/
public function insert(string $name, int $panelID, string $a, string $aaaa): bool|string
{
print("here");
$sql = "
INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel_id, a, aaaa)
VALUES (:name, :panel_id, :a, :aaaa)";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: ':name', var: $name);
$statement->bindParam(param: ':panel_id', var: $panelID);
$statement->bindParam(param: ':a', var: $a);
$statement->bindParam(param: ':aaaa', var: $aaaa);
$statement->execute();
return $this->databaseConnection->getConnection()->lastInsertId();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @param Int $id
* @param String $name
* @param int $panelID
* @param String $a
* @param String $aaaa
*
* @return false|int
*/
public function update(int $id, string $name, int $panelID, string $a, string $aaaa): bool|int
{
$current = $this->findByID(id: $id);
/* doesn't work
$statement = "
INSERT INTO domains(id, name, a, aaaa)
VALUES(:id, :name, :a, :aaaa)
ON DUPLICATE KEY UPDATE
name=COALESCE(VALUES(name), :name),
a=COALESCE(:a, a),
aaaa=COALESCE(:aaaa, aaaa)";
*/
if (empty($name)) {
$name = $current['name'];
}
if (empty($panelID)) {
$panelID = $current['panel_id'];
}
$panelID = intval(value: $panelID);
if (empty($a)) {
$a = $current['a'];
}
if (empty($aaaa)) {
$aaaa = $current['aaaa'];
}
$sql = "
UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET
name = :name,
panel_id = :panel_id,
a = :a,
aaaa = :aaaa
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->bindParam(param: 'name', var: $name);
$statement->bindParam(param: 'panel_id', var: $panelID);
$statement->bindParam(param: 'a', var: $a);
$statement->bindParam(param: 'aaaa', var: $aaaa);
$statement->execute();
// recreate zonefile
if ($panel = $this->panelController->findByID(id: intval(value: $panelID))) {
$a = $panel['a'];
$aaaa = $panel['aaaa'];
}
exec(command: '/usr/sbin/rndc reload');
return $statement->rowCount();
} catch (PDOException $e) {
print($e->getMessage());
return false;
}
}
/**
* @param $id
*
* @return int
*/
public function delete($id): int
{
$sql = "
DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . "
WHERE id = :id";
try {
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
$statement->bindParam(param: 'id', var: $id);
$statement->execute();
return $statement->rowCount();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
/**
* @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());
}
}
}