<?php
declare(strict_types = 1);
namespace App\Action\Ajax;
use App\Infra\Services\FilterApiService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class HomeProjectsDisplayAction
{
public function __construct(
FilterApiService $filterApiService,
Environment $environment
)
{}
/**
* @Route("/projects/home/display/{infos}", name="home-projects-display", methods={"GET"})
*/
public function __invoke(Request $request, string $infos)
{
switch ($infos) {
case 'all':
$filterApiServiceMethod = 'getAllProjects';
break;
case 'futur':
$filterApiServiceMethod = 'getFuturProjects';
break;
case 'construction':
$filterApiServiceMethod = 'getConstructionProjects';
break;
default:
throw new BadRequestHttpException(sprintf('The value of "infos" is not handled'));
}
return new JsonResponse([
'data' => $this->environment->render('Ajax/Projects/home-projects.html.twig', [
'futurProjects' => $this->filterApiService->{$filterApiServiceMethod}()
])
]);
}
}