Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateTripController
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace App\Trip\Infrastructure\Controllers;
6
7use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
8use App\Shared\Infrastructure\Http\ReadsJsonPayload;
9use App\Trip\Application\Command\UpdateTripCommand;
10use App\Trip\Application\Command\UpdateTripHandler;
11use App\Trip\Domain\Exceptions\TripAccessDenied;
12use App\Trip\Domain\Exceptions\TripNotFound;
13use App\Trip\Infrastructure\Http\ValidatesTripId;
14use InvalidArgumentException;
15use OpenApi\Attributes as OA;
16use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17use Symfony\Component\HttpFoundation\JsonResponse;
18use Symfony\Component\HttpFoundation\Request;
19use Symfony\Component\Routing\Attribute\Route;
20
21#[OA\Tag(name: 'Trips')]
22class 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}