<?php
declare(strict_types = 1);
namespace App\Action;
use App\Domain\Repository\Interfaces\TextRepositoryInterface;
use App\Form\Handler\ContactFormHandler;
use App\Form\Type\ContactFormType;
use App\Responder\RedirectResponder;
use App\Responder\TemplateResponder;
use App\Infra\Services\FilterApiService;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class VendreAction extends AbstractAction
{
public function __construct(
TemplateResponder $templateResponder,
RedirectResponder $redirectResponder,
FormFactoryInterface $formFactory,
ContactFormHandler $contactFormHandler,
FilterApiService $apiService
)
{
$this->templateResponder = $templateResponder;
$this->redirectResponder = $redirectResponder;
$this->formFactory = $formFactory;
$this->contactFormHandler = $contactFormHandler;
$this->apiService = $apiService;
}
/**
* @Route("/vendre", name="vendre")
*/
public function __invoke(Request $request)
{
$contactType = $this->formFactory
->create(ContactFormType::class)
->handleRequest($request);
if ($this->contactFormHandler->handle($contactType)) {
return $this->redirectResponder->__invoke('contact');
}
$toSales = $this->apiService->getPropertiesByCategory(1);
$salesProperties = [];
foreach ($toSales as $property) {
//if ($property['category'] == 1 or $property['category'] == 2) {
$salesProperties[] = [$property][0];
//}
}
$toRents = $this->apiService->getPropertiesByCategory(2);
$rentsProperties = [];
foreach ($toRents as $property) {
//if ($property['category'] == 1 or $property['category'] == 2) {
$rentsProperties[] = [$property][0];
//}
}
return $this->templateResponder->__invoke(
'vendre.html.twig', [
'form' => $contactType->createView(),
'salesProperties' => array_slice($salesProperties, 0 , 3),
'rentsProperties' => array_slice($rentsProperties, 0 , 3)
]
);
}
}