Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeclineInvitationController
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
1 / 2
4.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 decline
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
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\DeclineInvitationCommand;
9use App\Trip\Application\Command\DeclineInvitationHandler;
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 DeclineInvitationController extends AbstractController
20{
21    use ResolvesAuthenticatedUser;
22    use ValidatesInvitationId;
23
24    public function __construct(
25        private readonly DeclineInvitationHandler $handler,
26    ) {
27    }
28
29    #[Route('/invitations/{id}/decline', name: 'invitations_decline', methods: ['POST'])]
30    public function decline(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 DeclineInvitationCommand($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}