Files
project-manager/src/Repository/ProjectRepository.php
T

144 lines
4.1 KiB
PHP
Raw Normal View History

2026-09-03 18:30:52 +01:00
<?php
declare(strict_types=1);
namespace App\Repository;
use PDO;
/**
* Data access for the `projects` table.
2026-09-03 18:30:52 +01:00
*
* @phpstan-type ProjectRow array{
2026-09-03 18:30:52 +01:00
* id: int, owner_id: int, title: string, description: string,
* card_count: int, completed_count: int, created_at: string, updated_at: string
2026-09-03 18:30:52 +01:00
* }
*/
final class ProjectRepository
2026-09-03 18:30:52 +01:00
{
private const SELECT = <<<'SQL'
SELECT p.id, p.owner_id, p.title, p.description, p.created_at, p.updated_at,
(SELECT COUNT(*) FROM cards c WHERE c.project_id = p.id) AS card_count,
(SELECT COUNT(*) FROM cards c WHERE c.project_id = p.id AND c.complete = 1) AS completed_count
FROM projects p
2026-09-03 18:30:52 +01:00
SQL;
public function __construct(private readonly PDO $pdo)
{
}
/**
* Every project owned by the user, always sorted alphabetically by title
* (case-insensitive). There is deliberately no other ordering option.
*
* @return ProjectRow[]
2026-09-03 18:30:52 +01:00
*/
public function allForOwner(int $ownerId): array
{
$stmt = $this->pdo->prepare(
self::SELECT . ' WHERE p.owner_id = :owner ORDER BY p.title COLLATE NOCASE ASC, p.id ASC'
);
2026-09-03 18:30:52 +01:00
$stmt->execute(['owner' => $ownerId]);
return array_map($this->cast(...), $stmt->fetchAll());
}
public function countForOwner(int $ownerId): int
{
$stmt = $this->pdo->prepare('SELECT COUNT(*) FROM projects WHERE owner_id = :owner');
$stmt->execute(['owner' => $ownerId]);
return (int) $stmt->fetchColumn();
}
2026-09-03 18:30:52 +01:00
/**
* @return ProjectRow|null
2026-09-03 18:30:52 +01:00
*/
public function findOwnedBy(int $id, int $ownerId): ?array
{
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE p.id = :id AND p.owner_id = :owner');
2026-09-03 18:30:52 +01:00
$stmt->execute(['id' => $id, 'owner' => $ownerId]);
$row = $stmt->fetch();
return $row === false ? null : $this->cast($row);
}
/**
* @return ProjectRow
2026-09-03 18:30:52 +01:00
*/
public function create(int $ownerId, string $title, string $description): array
{
$stmt = $this->pdo->prepare(
'INSERT INTO projects (owner_id, title, description) VALUES (:owner, :title, :description)'
2026-09-03 18:30:52 +01:00
);
$stmt->execute([
'owner' => $ownerId,
'title' => $title,
'description' => $description,
]);
/** @var ProjectRow $project */
$project = $this->findOwnedBy((int) $this->pdo->lastInsertId(), $ownerId);
2026-09-03 18:30:52 +01:00
return $project;
2026-09-03 18:30:52 +01:00
}
/**
* @param array{title?: string, description?: string} $fields
* @return ProjectRow
2026-09-03 18:30:52 +01:00
*/
public function update(int $id, int $ownerId, array $fields): array
{
$sets = ['updated_at = ' . $this->nowExpr()];
$params = ['id' => $id];
foreach (['title', 'description'] as $column) {
if (array_key_exists($column, $fields)) {
$sets[] = "{$column} = :{$column}";
$params[$column] = $fields[$column];
}
}
$stmt = $this->pdo->prepare('UPDATE projects SET ' . implode(', ', $sets) . ' WHERE id = :id');
2026-09-03 18:30:52 +01:00
$stmt->execute($params);
/** @var ProjectRow $project */
$project = $this->findOwnedBy($id, $ownerId);
2026-09-03 18:30:52 +01:00
return $project;
2026-09-03 18:30:52 +01:00
}
public function delete(int $id): void
{
$this->pdo->prepare('DELETE FROM projects WHERE id = :id')->execute(['id' => $id]);
2026-09-03 18:30:52 +01:00
}
/** Bump updated_at, e.g. when the project's cards change. */
2026-09-03 18:30:52 +01:00
public function touch(int $id): void
{
$this->pdo->prepare('UPDATE projects SET updated_at = ' . $this->nowExpr() . ' WHERE id = :id')
2026-09-03 18:30:52 +01:00
->execute(['id' => $id]);
}
private function nowExpr(): string
{
return "strftime('%Y-%m-%dT%H:%M:%SZ', 'now')";
}
/**
* @param array<string, mixed> $row
* @return ProjectRow
2026-09-03 18:30:52 +01:00
*/
private function cast(array $row): array
{
$row['id'] = (int) $row['id'];
$row['owner_id'] = (int) $row['owner_id'];
$row['card_count'] = (int) $row['card_count'];
2026-09-03 18:30:52 +01:00
$row['completed_count'] = (int) $row['completed_count'];
/** @var ProjectRow $row */
2026-09-03 18:30:52 +01:00
return $row;
}
}