Refatoring : Working state
This commit is contained in:
@@ -3,123 +3,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Media;
|
||||
|
||||
use PDO;
|
||||
use App\Media\Infrastructure\PdoMediaRepository;
|
||||
|
||||
final class MediaRepository implements MediaRepositoryInterface
|
||||
/**
|
||||
* Pont de compatibilité : l'implémentation PDO principale vit désormais dans
|
||||
* App\Media\Infrastructure\PdoMediaRepository.
|
||||
*/
|
||||
final class MediaRepository extends PdoMediaRepository implements MediaRepositoryInterface
|
||||
{
|
||||
private const SELECT = 'SELECT id, filename, url, hash, user_id, created_at FROM media';
|
||||
|
||||
public function __construct(private readonly PDO $db)
|
||||
{
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
$stmt = $this->db->query(self::SELECT . ' ORDER BY id DESC');
|
||||
if ($stmt === false) {
|
||||
throw new \RuntimeException('La requête SELECT sur media a échoué.');
|
||||
}
|
||||
|
||||
return array_map(fn ($row) => Media::fromArray($row), $stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
}
|
||||
|
||||
public function findPage(int $limit, int $offset): array
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' ORDER BY id DESC LIMIT :limit OFFSET :offset');
|
||||
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
|
||||
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
|
||||
return array_map(fn ($row) => Media::fromArray($row), $stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
}
|
||||
|
||||
public function countAll(): int
|
||||
{
|
||||
$stmt = $this->db->query('SELECT COUNT(*) FROM media');
|
||||
if ($stmt === false) {
|
||||
throw new \RuntimeException('La requête COUNT sur media a échoué.');
|
||||
}
|
||||
|
||||
return (int) ($stmt->fetchColumn() ?: 0);
|
||||
}
|
||||
|
||||
public function findByUserId(int $userId): array
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' WHERE user_id = :user_id ORDER BY id DESC');
|
||||
$stmt->execute([':user_id' => $userId]);
|
||||
|
||||
return array_map(fn ($row) => Media::fromArray($row), $stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
}
|
||||
|
||||
public function findByUserPage(int $userId, int $limit, int $offset): array
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' WHERE user_id = :user_id ORDER BY id DESC LIMIT :limit OFFSET :offset');
|
||||
$stmt->bindValue(':user_id', $userId, PDO::PARAM_INT);
|
||||
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
|
||||
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
|
||||
return array_map(fn ($row) => Media::fromArray($row), $stmt->fetchAll(PDO::FETCH_ASSOC));
|
||||
}
|
||||
|
||||
public function countByUserId(int $userId): int
|
||||
{
|
||||
$stmt = $this->db->prepare('SELECT COUNT(*) FROM media WHERE user_id = :user_id');
|
||||
$stmt->execute([':user_id' => $userId]);
|
||||
|
||||
return (int) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
public function findById(int $id): ?Media
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' WHERE id = :id');
|
||||
$stmt->execute([':id' => $id]);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
return $row ? Media::fromArray($row) : null;
|
||||
}
|
||||
|
||||
public function findByHash(string $hash): ?Media
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' WHERE hash = :hash ORDER BY id DESC LIMIT 1');
|
||||
$stmt->execute([':hash' => $hash]);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
return $row ? Media::fromArray($row) : null;
|
||||
}
|
||||
|
||||
public function findByHashForUser(string $hash, int $userId): ?Media
|
||||
{
|
||||
$stmt = $this->db->prepare(self::SELECT . ' WHERE hash = :hash AND user_id = :user_id ORDER BY id DESC LIMIT 1');
|
||||
$stmt->execute([':hash' => $hash, ':user_id' => $userId]);
|
||||
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
return $row ? Media::fromArray($row) : null;
|
||||
}
|
||||
|
||||
public function create(Media $media): int
|
||||
{
|
||||
$stmt = $this->db->prepare(
|
||||
'INSERT INTO media (filename, url, hash, user_id, created_at)
|
||||
VALUES (:filename, :url, :hash, :user_id, :created_at)'
|
||||
);
|
||||
|
||||
$stmt->execute([
|
||||
':filename' => $media->getFilename(),
|
||||
':url' => $media->getUrl(),
|
||||
':hash' => $media->getHash(),
|
||||
':user_id' => $media->getUserId(),
|
||||
':created_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
return (int) $this->db->lastInsertId();
|
||||
}
|
||||
|
||||
public function delete(int $id): int
|
||||
{
|
||||
$stmt = $this->db->prepare('DELETE FROM media WHERE id = :id');
|
||||
$stmt->execute([':id' => $id]);
|
||||
|
||||
return $stmt->rowCount();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user