Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| AcceptInvitationController | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| accept | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Trip\Infrastructure\Controllers; |
| 6 | |
| 7 | use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser; |
| 8 | use App\Trip\Application\Command\AcceptInvitationCommand; |
| 9 | use App\Trip\Application\Command\AcceptInvitationHandler; |
| 10 | use App\Trip\Domain\Exceptions\InvitationNotFound; |
| 11 | use App\Trip\Infrastructure\Http\ValidatesInvitationId; |
| 12 | use OpenApi\Attributes as OA; |
| 13 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 14 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 15 | use Symfony\Component\HttpFoundation\Response; |
| 16 | use Symfony\Component\Routing\Attribute\Route; |
| 17 | |
| 18 | #[OA\Tag(name: 'Trips')] |
| 19 | class 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 | } |