<?php
declare(strict_types = 1);
namespace App\Action;
use App\Domain\Repository\Interfaces\TextRepositoryInterface;
use App\Infra\Services\ApiService;
use App\Responder\RedirectResponder;
use App\Responder\TemplateResponder;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class PropertyDetailAction extends AbstractAction
{
public function __construct(
TemplateResponder $templateResponder,
RedirectResponder $redirectResponder,
ApiService $apiService
)
{
$this->templateResponder = $templateResponder;
$this->redirectResponder = $redirectResponder;
$this->apiService = $apiService;
}
/**
* @Route("/bien/detail/{id}", name="property-detail")
*/
public function __invoke(string $id)
{
$property = $this->apiService->getPropertyById($id);
$properties = [];
$smilarProperties = $this->apiService->getAllProperties()['properties'];
foreach ($smilarProperties as $smilarProperty) {
if ($property['bedrooms'] !== 0 && ($property['bedrooms'] === $smilarProperty['bedrooms']) ) {
$properties[] = $smilarProperty;
}
}
if (empty($properties)) {
foreach ($smilarProperties as $smilarProperty) {
if ($property['type'] === $smilarProperty['type']) {
$properties[] = $smilarProperty;
}
}
}
return $this->templateResponder->__invoke('property-detail.html.twig', [
'property' => $property,
'otherProperties' => array_slice($properties, 0 , 3)
]);
}
}