Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| RemoveMemberController | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| isUuid | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 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\RemoveMemberCommand; |
| 9 | use App\Trip\Application\Command\RemoveMemberHandler; |
| 10 | use App\Trip\Domain\Exceptions\MemberNotFound; |
| 11 | use App\Trip\Domain\Exceptions\TripAccessDenied; |
| 12 | use App\Trip\Domain\Exceptions\TripNotFound; |
| 13 | use App\Trip\Domain\ValueObject\MemberId; |
| 14 | use App\Trip\Infrastructure\Http\ValidatesTripId; |
| 15 | use InvalidArgumentException; |
| 16 | use OpenApi\Attributes as OA; |
| 17 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 18 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 19 | use Symfony\Component\HttpFoundation\Response; |
| 20 | use Symfony\Component\Routing\Attribute\Route; |
| 21 | |
| 22 | #[OA\Tag(name: 'Trips')] |
| 23 | class RemoveMemberController extends AbstractController |
| 24 | { |
| 25 | use ResolvesAuthenticatedUser; |
| 26 | use ValidatesTripId; |
| 27 | |
| 28 | public function __construct( |
| 29 | private readonly RemoveMemberHandler $handler, |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | #[Route('/trips/{tripId}/members/{userId}', name: 'trips_remove_member', methods: ['DELETE'])] |
| 34 | public function remove(string $tripId, string $userId): Response |
| 35 | { |
| 36 | $securityUser = $this->securityUser(); |
| 37 | |
| 38 | if ($this->parseTripId($tripId) === null || !$this->isUuid($userId)) { |
| 39 | return new JsonResponse(['error' => 'Not found.'], 404); |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | $this->handler->handle(new RemoveMemberCommand( |
| 44 | tripId: $tripId, |
| 45 | actingUserId: $securityUser->userId(), |
| 46 | targetUserId: $userId, |
| 47 | )); |
| 48 | } catch (TripNotFound|MemberNotFound $e) { |
| 49 | return new JsonResponse(['error' => $e->getMessage()], 404); |
| 50 | } catch (TripAccessDenied $e) { |
| 51 | return new JsonResponse(['error' => $e->getMessage()], 403); |
| 52 | } |
| 53 | |
| 54 | return new Response(null, Response::HTTP_NO_CONTENT); |
| 55 | } |
| 56 | |
| 57 | private function isUuid(string $id): bool |
| 58 | { |
| 59 | try { |
| 60 | new MemberId($id); |
| 61 | |
| 62 | return true; |
| 63 | } catch (InvalidArgumentException) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | } |