<?php
namespace App\Controller\Front;
use App\Repository\ContentRepository;
use App\Repository\SliderHeaderRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class PropertyController extends AbstractController
{
const API_LINK = 'https://middleware-production.easy2pilot-v8.com/api/';
/**
* @var HttpClientInterface
*/
private $httpClient;
private $token;
private $advertType;
private $cache;
public function __construct(HttpClientInterface $httpClient, CacheInterface $cache)
{
$this->httpClient = $httpClient;
$this->cache = $cache;
}
/**
* @Route("/annonces/{type}/{page}", defaults={"page"=1}, name="front_property_list")
*/
public function list(string $type, int $page, ContentRepository $contentRepository, PaginatorInterface $paginator, SliderHeaderRepository $sliderHeaderRepository)
{
$this->token = $this->getToken();
$this->advertType = $type;
$adverts = $this->cache->get('adverts', function (ItemInterface $item) {
$item->expiresAfter(120);
$adverts = $this->httpClient->request(
Request::METHOD_GET,
self::API_LINK . $this->getParameter('api_uuid').'/annonces', [
'headers' => [
'token' => $this->token
],
'body' => '{"filters":[{"key": "vente_location", "value": "'.$this->advertType.'", "compare": "="}]}'
]
);
return $adverts->toArray();
});
$advertFiltered = [];
$cities = [];
foreach ($adverts['data'] as $advert) {
if (!in_array($advert['localisation']['ville'], $cities)) {
$cities[] = $advert['localisation']['ville'];
}
if ($advert['info']['vente_location'] === $type) {
if (isset($advertFiltered[$advert['prix']['budget']])) {
$advertFiltered[$advert['prix']['budget'] + 1] = $advert;
} else {
$advertFiltered[$advert['prix']['budget']] = $advert;
}
}
}
ksort($advertFiltered);
$pagination = $paginator->paginate($advertFiltered, $page, 20);
return $this->render('front/property/list.html.twig', [
'contentHeader' => $contentRepository->findOneBy(['page' => ($type === 'vente') ? 'sale' : 'rent', 'section' => 'header']),
'adverts' => $pagination,
'countAdverts' => count($advertFiltered),
'type' => $type,
'advertCities' => $cities,
'slider' => $sliderHeaderRepository->findOneBy(['page' => $type]),
]);
}
/**
* @Route("/annonce/{id}/{type}/{city}/details", name="front_property_single")
*/
public function single(int $id, string $type, string $city)
{
$token = $this->getToken();
$data = $this->httpClient->request(
Request::METHOD_GET,
self::API_LINK . $this->getParameter('api_uuid').'/annonces', [
'headers' => [
'token' => $token,
],
'body' => '{"filters":[{"key": "id", "value": "'.$id.'", "compare": "="}]}'
]
);
$advert = $data->toArray();
return $this->render('front/property/single.html.twig', [
'advert' => $advert['data'][0]
]);
}
private function getToken()
{
$response = $this->httpClient->request(
Request::METHOD_POST,
self::API_LINK . $this->getParameter('api_uuid').'/token', [
'headers' => [
'login' => $this->getParameter('api_login'),
'password' => $this->getParameter('api_password'),
],
]
);
return $response->toArray()['data']['token'];
}
/**
* @Route("/filtre/annonces/{advertType}")
*/
public function filterList(string $advertType, Request $request)
{
$allType = [];
if (is_array($request->get('type'))) {
// Form desktop
foreach ($request->get('type') as $key => $type) {
$allType[] = $key;
}
} else {
// Form mobile
$allType[] = $request->get('type');
}
$token = $this->getToken();
$adverts = $this->httpClient->request(
Request::METHOD_GET,
self::API_LINK . $this->getParameter('api_uuid').'/annonces', [
'headers' => [
'token' => $token
]
]
);
$adverts = $adverts->toArray();
$advertsFiltered = $adverts['data'];
foreach ($advertsFiltered as $key => $advert) {
if ($request->request->get('type')) {
if (!empty($allType) && !in_array($advert['info']['nature'], $allType)) {
unset($advertsFiltered[$key]);
}
}
if ($request->request->get('rent') && $advert['info']['vente_location'] !== $request->request->get('rent')) {
unset($advertsFiltered[$key]);
}
if ($request->request->get('is-mobile')) {
if ($request->request->get('advert-type') && $advert['info']['vente_location'] !== $request->request->get('advert-type')) {
unset($advertsFiltered[$key]);
}
}
if ($request->request->get('location') && $advert['localisation']['ville'] !== $request->request->get('location')) {
unset($advertsFiltered[$key]);
}
if ($request->request->get('max_price') && $advert['prix']['budget'] > $request->request->get('max_price')) {
unset($advertsFiltered[$key]);
}
if ($request->request->get('min_room') && $advert['info']['nombre_chambres'] < $request->request->get('min_room')) {
unset($advertsFiltered[$key]);
}
}
$advertsByPrice = [];
foreach ($advertsFiltered as $advert) {
$advertsByPrice[$advert['prix']['budget']] = $advert;
}
ksort($advertsByPrice);
return $this->json([
'template' => $this->render('front/html/advert_list.html.twig', ['adverts' => $advertsByPrice]),
'countAdverts' => count($advertsByPrice)
]);
}
}