Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AcceptInvitationController
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 accept
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Trip\Infrastructure\Controllers;
6
7use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
8use App\Trip\Application\Command\AcceptInvitationCommand;
9use App\Trip\Application\Command\AcceptInvitationHandler;
10use App\Trip\Domain\Exceptions\InvitationNotFound;
11use App\Trip\Infrastructure\Http\ValidatesInvitationId;
12use OpenApi\Attributes as OA;
13use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14use Symfony\Component\HttpFoundation\JsonResponse;
15use Symfony\Component\HttpFoundation\Response;
16use Symfony\Component\Routing\Attribute\Route;
17
18#[OA\Tag(name: 'Trips')]
19class AcceptInvitationController extends AbstractController
20{
21    use ResolvesAuthenticatedUser;
22    use ValidatesInvitationId;
23
24    public function __construct(
25        private readonly AcceptInvitationHandler $handler,
26    ) {
27    }
28
29    #[Route('/invitations/{id}/accept', name: 'invitations_accept', methods: ['POST'])]
30    public function accept(string $id): Response
31    {
32        $securityUser = $this->securityUser();
33
34        if ($this->parseInvitationId($id) === null) {
35            return new JsonResponse(['error' => 'Invitation not found.'], 404);
36        }
37
38        try {
39            $this->handler->handle(new AcceptInvitationCommand($id, $securityUser->userId()));
40        } catch (InvitationNotFound $e) {
41            return new JsonResponse(['error' => $e->getMessage()], 404);
42        }
43
44        return new Response(null, Response::HTTP_NO_CONTENT);
45    }
46}