Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CreateTripController | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| 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\CreateTripCommand; |
| 10 | use App\Trip\Application\Command\CreateTripHandler; |
| 11 | use InvalidArgumentException; |
| 12 | use OpenApi\Attributes as OA; |
| 13 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 14 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 15 | use Symfony\Component\HttpFoundation\Request; |
| 16 | use Symfony\Component\Routing\Attribute\Route; |
| 17 | |
| 18 | #[OA\Tag(name: 'Trips')] |
| 19 | class CreateTripController extends AbstractController |
| 20 | { |
| 21 | use ReadsJsonPayload; |
| 22 | use ResolvesAuthenticatedUser; |
| 23 | |
| 24 | public function __construct( |
| 25 | private readonly CreateTripHandler $handler, |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | #[Route('/trips', name: 'trips_create', methods: ['POST'])] |
| 30 | public function create(Request $request): JsonResponse |
| 31 | { |
| 32 | $securityUser = $this->securityUser(); |
| 33 | |
| 34 | try { |
| 35 | $payload = $this->decodeJsonBody($request); |
| 36 | |
| 37 | $tripId = $this->handler->handle(new CreateTripCommand( |
| 38 | ownerId: $securityUser->userId(), |
| 39 | name: $this->stringField($payload, 'name'), |
| 40 | destination: $this->stringField($payload, 'destination'), |
| 41 | startDate: $this->stringField($payload, 'startDate'), |
| 42 | endDate: $this->stringField($payload, 'endDate'), |
| 43 | currency: $this->stringField($payload, 'currency'), |
| 44 | budgetAmount: $this->intField($payload, 'budgetAmount'), |
| 45 | )); |
| 46 | } catch (InvalidArgumentException $e) { |
| 47 | return new JsonResponse(['error' => $e->getMessage()], 400); |
| 48 | } |
| 49 | |
| 50 | return new JsonResponse(['id' => $tripId->value()], 201); |
| 51 | } |
| 52 | } |