65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Exception\ApiException;
|
||
|
|
use App\Repository\CardStatusRepository;
|
||
|
|
use App\Repository\ProjectRepository;
|
||
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
||
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Read-only listing of a project's card statuses. Statuses are seeded when the
|
||
|
|
* project is created; there is no create/update/delete yet.
|
||
|
|
*/
|
||
|
|
final class CardStatusController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly ProjectRepository $projects,
|
||
|
|
private readonly CardStatusRepository $statuses,
|
||
|
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* GET /api/projects/{projectId}/statuses
|
||
|
|
*/
|
||
|
|
public function index(Request $request, Response $response, array $args): Response
|
||
|
|
{
|
||
|
|
$projectId = $this->requireOwnedProjectId($request, $args);
|
||
|
|
|
||
|
|
return $this->json($response, [
|
||
|
|
'statuses' => array_map($this->present(...), $this->statuses->allForProject($projectId)),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, string> $args
|
||
|
|
*/
|
||
|
|
private function requireOwnedProjectId(Request $request, array $args): int
|
||
|
|
{
|
||
|
|
$project = $this->projects->findOwnedBy((int) $args['projectId'], $this->user($request)['id']);
|
||
|
|
|
||
|
|
if ($project === null) {
|
||
|
|
throw new ApiException('Project not found.', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $project['id'];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array{id: int, project_id: int, name: string, position: int, created_at: string, updated_at: string} $status
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
private function present(array $status): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $status['id'],
|
||
|
|
'project_id' => $status['project_id'],
|
||
|
|
'name' => $status['name'],
|
||
|
|
'position' => $status['position'],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|