Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ListExpensesController
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
4
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
 list
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace App\Expense\Infrastructure\Controllers;
6
7use App\Expense\Application\Query\ExpenseView;
8use App\Expense\Application\Query\ListExpensesHandler;
9use App\Expense\Application\Query\ListExpensesQuery;
10use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
11use App\Trip\Domain\Exceptions\TripNotFound;
12use App\Trip\Infrastructure\Http\ValidatesTripId;
13use OpenApi\Attributes as OA;
14use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15use Symfony\Component\HttpFoundation\JsonResponse;
16use Symfony\Component\Routing\Attribute\Route;
17
18#[OA\Tag(name: 'Expenses')]
19class ListExpensesController extends AbstractController
20{
21    use ResolvesAuthenticatedUser;
22    use ValidatesTripId;
23
24    public function __construct(
25        private readonly ListExpensesHandler $handler,
26    ) {
27    }
28
29    #[Route('/trips/{tripId}/expenses', name: 'expenses_list', methods: ['GET'])]
30    public function list(string $tripId): JsonResponse
31    {
32        $securityUser = $this->securityUser();
33
34        if ($this->parseTripId($tripId) === null) {
35            return new JsonResponse(['error' => 'Trip not found.'], 404);
36        }
37
38        try {
39            $views = $this->handler->handle(
40                new ListExpensesQuery($tripId, $securityUser->userId(), $securityUser->preferredCurrency()),
41            );
42        } catch (TripNotFound $e) {
43            return new JsonResponse(['error' => $e->getMessage()], 404);
44        }
45
46        return new JsonResponse(array_map(static fn (ExpenseView $view) => $view->toArray(), $views), 200);
47    }
48}