/
Alexsey-VR
/
php-project-9
Обзор
Документация
Войти
/
Alexsey-VR
/
php-project-9
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Repository/UrlCheckRepository.php
157 строк
5 KB
Alexsey-VR
updated url check repository
24 май 2026, 13:40
24 май 2026, 13:40
2cdbb7b
Код
Авторство
О чём код?
<?php namespace Analyzer\Repository; use Analyzer\Interfaces\UrlCheckRepositoryInterface; use Analyzer\Interfaces\UrlCheckInterface; use Analyzer\UrlCheck\UrlCheck as UrlCheck; use Analyzer\Exceptions\UrlException as UrlException; use PDO; class UrlCheckRepository implements UrlCheckRepositoryInterface { private PDO $connection; private const string ERROR_MESSAGE_FOR_TIMESTAMP = "PDO error: timestamp has a wrong type"; private const string ERROR_MESSAGE_FOR_ID = "PDO error: can't get last insert id"; public function __construct(PDO $connection) { $this->connection = $connection; date_default_timezone_set('UTC'); } /** * @param array<mixed> $dbData * @return array<int, UrlCheckInterface> */ private function getUrlCheckList(array $dbData): array { $urlChecks = []; foreach ($dbData as $item) { if (is_array($item)) { $urlCheck = UrlCheck::fromArray($item); $foundId = $item['id']; $timestamp = $item['created_at']; $urlCheck->setId( is_int($foundId) ? $foundId : throw new UrlException(self::ERROR_MESSAGE_FOR_ID) ); $urlCheck->setTimestamp( is_string($timestamp) ? $timestamp : throw new UrlException(self::ERROR_MESSAGE_FOR_TIMESTAMP) ); $urlChecks[] = $urlCheck; } } return $urlChecks; } public function save(UrlCheckInterface $urlCheck): void { if ($urlCheck->exists()) { $this->update($urlCheck); } else { $this->create($urlCheck); } } public function create(UrlCheckInterface $urlCheck): void { $sql = "INSERT INTO url_checks (url_id, status, h1, title, description, created_at) VALUES " . "(:url_id, :status, :h1, :title, :description, :timestamp)"; $stmt = $this->connection->prepare($sql); $urlId = $urlCheck->getUrlId(); $status = $urlCheck->getStatus(); $h1 = $urlCheck->getH1(); $title = $urlCheck->getTitle(); $description = $urlCheck->getDescription(); $timestamp = date('Y-m-d H:i:s'); $stmt->bindParam(':url_id', $urlId); $stmt->bindParam(':status', $status); $stmt->bindParam(':h1', $h1); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':timestamp', $timestamp); $stmt->execute(); $id = intval($this->connection->lastInsertId()); $urlCheck->setId( $id ?: throw new UrlException(self::ERROR_MESSAGE_FOR_ID) ); $urlCheck->setTimestamp( is_string($timestamp) ? $timestamp : throw new UrlException(self::ERROR_MESSAGE_FOR_TIMESTAMP) ); } public function update(UrlCheckInterface $urlCheck): void { $sql = "UPDATE url_checks SET url_id = :url_id, status = :status" . ", h1 = :h1, " . "title = :title" . ", description = :description" . ", created_at = :timestamp" . " WHERE id = :id"; $stmt = $this->connection->prepare($sql); $urlId = $urlCheck->getUrlId(); $status = $urlCheck->getStatus(); $h1 = $urlCheck->getH1(); $title = $urlCheck->getTitle(); $description = $urlCheck->getDescription(); $timestamp = date('Y-m-d H:i:s'); $id = $urlCheck->getId(); $stmt->bindParam(':url_id', $urlId); $stmt->bindParam(':status', $status); $stmt->bindParam(':h1', $h1); $stmt->bindParam(':title', $title); $stmt->bindParam(':description', $description); $stmt->bindParam(':timestamp', $timestamp); $stmt->bindParam(':id', $id); $stmt->execute(); } public function find(int $id): ?UrlCheckInterface { $sql = "SELECT * FROM url_checks WHERE id = :id"; $stmt = $this->connection->prepare($sql); $stmt->bindParam(':id', $id); $stmt->execute(); $result = $this->getUrlCheckList([$stmt->fetch()]); return isset($result[0]) ? $result[0] : null; } public function delete(int $id): void { $sql = "DELETE FROM url_checks WHERE id = :id"; $stmt = $this->connection->prepare($sql); $stmt->bindParam(':id', $id); $stmt->execute(); } public function getEntities(): array { $sql = "SELECT * FROM url_checks ORDER BY created_at DESC"; $stmt = $this->connection->query($sql); return $this->getUrlCheckList( $stmt !== false ? $stmt->fetchAll(PDO::FETCH_DEFAULT) : [] ); } public function getEntitiesByUrlId(int $urlId): array { $sql = "SELECT * FROM url_checks WHERE url_id = :url_id ORDER BY created_at DESC"; $stmt = $this->connection->prepare($sql); $stmt->bindParam(':url_id', $urlId); $stmt->execute(); return $this->getUrlCheckList( $stmt !== false ? $stmt->fetchAll(PDO::FETCH_DEFAULT) : [] ); } }