48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Exception\ApiException;
|
||
|
|
use App\Repository\ProjectRepository;
|
||
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Shared helper for controllers whose routes are nested under
|
||
|
|
* /projects/{projectId}: looking up the project named in the route, 404-ing
|
||
|
|
* if it's missing or not owned by the caller.
|
||
|
|
*/
|
||
|
|
abstract class ProjectScopedController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct(protected readonly ProjectRepository $projects)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The project named in the route, or 404 if it is missing or not owned
|
||
|
|
* by the authenticated user.
|
||
|
|
*
|
||
|
|
* @param array<string, string> $args
|
||
|
|
* @return array{id: int, owner_id: int, title: string, description: string, card_count: int, completed_count: int, created_at: string, updated_at: string}
|
||
|
|
*/
|
||
|
|
protected function requireOwnedProject(Request $request, array $args): array
|
||
|
|
{
|
||
|
|
$project = $this->projects->findOwnedBy((int) $args['projectId'], $this->user($request)['id']);
|
||
|
|
|
||
|
|
if ($project === null) {
|
||
|
|
throw new ApiException('Project not found.', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $project;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param array<string, string> $args
|
||
|
|
*/
|
||
|
|
protected function requireOwnedProjectId(Request $request, array $args): int
|
||
|
|
{
|
||
|
|
return $this->requireOwnedProject($request, $args)['id'];
|
||
|
|
}
|
||
|
|
}
|