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