Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GetRateController | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Currency\Infrastructure\Controllers; |
| 6 | |
| 7 | use App\Currency\Application\Query\GetRateHandler; |
| 8 | use App\Currency\Application\Query\GetRateQuery; |
| 9 | use InvalidArgumentException; |
| 10 | use OpenApi\Attributes as OA; |
| 11 | use RuntimeException; |
| 12 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 13 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 14 | use Symfony\Component\HttpFoundation\Request; |
| 15 | use Symfony\Component\Routing\Attribute\Route; |
| 16 | |
| 17 | #[OA\Tag(name: 'Currencies')] |
| 18 | class GetRateController extends AbstractController |
| 19 | { |
| 20 | public function __construct( |
| 21 | private readonly GetRateHandler $handler, |
| 22 | ) { |
| 23 | } |
| 24 | |
| 25 | #[Route('/rates', name: 'rates_get', methods: ['GET'])] |
| 26 | public function get(Request $request): JsonResponse |
| 27 | { |
| 28 | try { |
| 29 | $view = $this->handler->handle(new GetRateQuery( |
| 30 | from: (string) $request->query->get('from', ''), |
| 31 | to: (string) $request->query->get('to', ''), |
| 32 | on: $request->query->get('on'), |
| 33 | )); |
| 34 | } catch (RuntimeException $e) { |
| 35 | return new JsonResponse(['error' => 'rate unavailable'], 503); |
| 36 | } catch (InvalidArgumentException $e) { |
| 37 | return new JsonResponse(['error' => $e->getMessage()], 400); |
| 38 | } |
| 39 | |
| 40 | return new JsonResponse($view); |
| 41 | } |
| 42 | } |