Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| UpdateTripController | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| 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\Shared\Infrastructure\Http\ReadsJsonPayload; |
| 9 | use App\Trip\Application\Command\UpdateTripCommand; |
| 10 | use App\Trip\Application\Command\UpdateTripHandler; |
| 11 | use App\Trip\Domain\Exceptions\TripAccessDenied; |
| 12 | use App\Trip\Domain\Exceptions\TripNotFound; |
| 13 | use App\Trip\Infrastructure\Http\ValidatesTripId; |
| 14 | use InvalidArgumentException; |
| 15 | use OpenApi\Attributes as OA; |
| 16 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 17 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 18 | use Symfony\Component\HttpFoundation\Request; |
| 19 | use Symfony\Component\Routing\Attribute\Route; |
| 20 | |
| 21 | #[OA\Tag(name: 'Trips')] |
| 22 | class UpdateTripController extends AbstractController |
| 23 | { |
| 24 | use ReadsJsonPayload; |
| 25 | use ResolvesAuthenticatedUser; |
| 26 | use ValidatesTripId; |
| 27 | |
| 28 | public function __construct( |
| 29 | private readonly UpdateTripHandler $handler, |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | #[Route('/trips/{id}', name: 'trips_update', methods: ['PUT'])] |
| 34 | public function update(string $id, Request $request): JsonResponse |
| 35 | { |
| 36 | $securityUser = $this->securityUser(); |
| 37 | |
| 38 | if ($this->parseTripId($id) === null) { |
| 39 | return new JsonResponse(['error' => 'Trip not found.'], 404); |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | $payload = $this->decodeJsonBody($request); |
| 44 | |
| 45 | $view = $this->handler->handle(new UpdateTripCommand( |
| 46 | tripId: $id, |
| 47 | userId: $securityUser->userId(), |
| 48 | name: $this->stringField($payload, 'name'), |
| 49 | destination: $this->stringField($payload, 'destination'), |
| 50 | startDate: $this->stringField($payload, 'startDate'), |
| 51 | endDate: $this->stringField($payload, 'endDate'), |
| 52 | budgetAmount: $this->intField($payload, 'budgetAmount'), |
| 53 | )); |
| 54 | } catch (TripNotFound $e) { |
| 55 | return new JsonResponse(['error' => $e->getMessage()], 404); |
| 56 | } catch (TripAccessDenied $e) { |
| 57 | return new JsonResponse(['error' => $e->getMessage()], 403); |
| 58 | } catch (InvalidArgumentException $e) { |
| 59 | return new JsonResponse(['error' => $e->getMessage()], 400); |
| 60 | } |
| 61 | |
| 62 | return new JsonResponse($view->toArray(), 200); |
| 63 | } |
| 64 | } |