config['debug']) { $this->log->debug(message: "DomainRepository::__construct()"); } } /** * @return array */ public function findAll(): array { if ($this->config['debug']) { $this->log->debug(message: "findAll()"); } $domains = []; $sql = " SELECT id, name, panel 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'], panel: $result['panel'], id: $result['id']); $domains[] = $domain; } return $domains; } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param int $id * * @return bool|\App\Entity\Domain */ public function findByID(int $id): bool|Domain { if ($this->config['debug']) { $this->log->debug(message: "findById($id)"); } $sql = " SELECT id, name, panel FROM . " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; 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 Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); } else { return false; } } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $name * * @return \App\Entity\Domain|bool */ public function findByName(string $name): Domain|bool { if ($this->config['debug']) { $this->log->debug(message: "findByName($name)"); } $sql = " SELECT id, name, panel FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE name = :name"; 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 Domain(name: $result['name'], panel: $result['panel'], id: $result['id']); } else { return false; } } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param String $name * * @return \App\Entity\Domain|bool */ public function findByHost(string $host): Domain|bool { if ($this->config['debug']) { $this->log->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 { if ($this->config['debug']) { $domainName = $domain->getName(); $this->log->debug(message: "insert($domainName)"); } $sql = " INSERT INTO " . DatabaseConnection::TABLE_DOMAINS . " (name, panel) VALUES (:name, :panel)"; try { $name = $domain->getName(); $panel = $domain->getPanel(); $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $statement->bindParam(param: ':name', var: $name); $statement->bindParam(param: ':panel', var: $panel); $statement->execute(); return $this->databaseConnection->getConnection()->lastInsertId(); } catch (PDOException $e) { exit($e->getMessage()); } } /** * @param \App\Entity\Domain $domain * * @return false|int */ public function update(Domain $domain): bool|int { if ($this->config['debug']) { $domainName = $domain->getName(); $this->log->debug(message: "update($domainName)"); } $id = $domain->getId(); $current = $this->findByID(id: $id); if (empty($domain->getName())) { $name = $current->getName(); } else { $name = $domain->getName(); } if (empty($domain->getPanel())) { $panel = $current->getPanel(); } else { $panel = $domain->getPanel(); } $sql = " UPDATE " . DatabaseConnection::TABLE_DOMAINS . " SET name = :name, panel = :panel 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', var: $panel); $statement->execute(); return $statement->rowCount(); } catch (PDOException $e) { echo $e->getMessage(); return false; } } /** * @param \App\Entity\Domain $domain * * @return int */ public function delete(Domain $domain): int { if ($this->config['debug']) { $domainName = $domain->getName(); $this->log->debug(message: "delete($domainName)"); } $sql = " DELETE FROM " . DatabaseConnection::TABLE_DOMAINS . " WHERE id = :id"; try { $statement = $this->databaseConnection->getConnection()->prepare(query: $sql); $id = $domain->getId(); $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()); } } }