Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| DeleteTripController | |
100.00% |
10 / 10 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| 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\DeleteTripCommand; |
| 9 | use App\Trip\Application\Command\DeleteTripHandler; |
| 10 | use App\Trip\Domain\Exceptions\TripAccessDenied; |
| 11 | use App\Trip\Domain\Exceptions\TripNotFound; |
| 12 | use App\Trip\Infrastructure\Http\ValidatesTripId; |
| 13 | use OpenApi\Attributes as OA; |
| 14 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 16 | use Symfony\Component\HttpFoundation\Response; |
| 17 | use Symfony\Component\Routing\Attribute\Route; |
| 18 | |
| 19 | #[OA\Tag(name: 'Trips')] |
| 20 | class DeleteTripController extends AbstractController |
| 21 | { |
| 22 | use ResolvesAuthenticatedUser; |
| 23 | use ValidatesTripId; |
| 24 | |
| 25 | public function __construct( |
| 26 | private readonly DeleteTripHandler $handler, |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | #[Route('/trips/{id}', name: 'trips_delete', methods: ['DELETE'])] |
| 31 | public function delete(string $id): Response |
| 32 | { |
| 33 | $securityUser = $this->securityUser(); |
| 34 | |
| 35 | if ($this->parseTripId($id) === null) { |
| 36 | return new JsonResponse(['error' => 'Trip not found.'], 404); |
| 37 | } |
| 38 | |
| 39 | try { |
| 40 | $this->handler->handle(new DeleteTripCommand($id, $securityUser->userId())); |
| 41 | } catch (TripNotFound $e) { |
| 42 | return new JsonResponse(['error' => $e->getMessage()], 404); |
| 43 | } catch (TripAccessDenied $e) { |
| 44 | return new JsonResponse(['error' => $e->getMessage()], 403); |
| 45 | } |
| 46 | |
| 47 | return new Response(null, Response::HTTP_NO_CONTENT); |
| 48 | } |
| 49 | } |