src/Controller/Front/PropertyController.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Repository\ContentRepository;
  4. use App\Repository\SliderHeaderRepository;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Contracts\Cache\CacheInterface;
  10. use Symfony\Contracts\Cache\ItemInterface;
  11. use Symfony\Contracts\HttpClient\HttpClientInterface;
  12. class PropertyController extends AbstractController
  13. {
  14.     const API_LINK 'https://middleware-production.easy2pilot-v8.com/api/';
  15.     /**
  16.      * @var HttpClientInterface
  17.      */
  18.     private $httpClient;
  19.     private $token;
  20.     private $advertType;
  21.     private $cache;
  22.     public function __construct(HttpClientInterface $httpClientCacheInterface $cache)
  23.     {
  24.         $this->httpClient $httpClient;
  25.         $this->cache $cache;
  26.     }
  27.     /**
  28.      * @Route("/annonces/{type}/{page}", defaults={"page"=1}, name="front_property_list")
  29.      */
  30.     public function list(string $typeint $pageContentRepository $contentRepositoryPaginatorInterface $paginatorSliderHeaderRepository $sliderHeaderRepository)
  31.     {
  32.         $this->token $this->getToken();
  33.         $this->advertType $type;
  34.         $adverts $this->cache->get('adverts', function (ItemInterface $item) {
  35.             $item->expiresAfter(120);
  36.             $adverts $this->httpClient->request(
  37.                 Request::METHOD_GET,
  38.                 self::API_LINK $this->getParameter('api_uuid').'/annonces', [
  39.                     'headers' => [
  40.                         'token' => $this->token
  41.                     ],
  42.                     'body' => '{"filters":[{"key": "vente_location", "value": "'.$this->advertType.'", "compare": "="}]}'
  43.                 ]
  44.             );
  45.             return $adverts->toArray();
  46.         });
  47.         $advertFiltered = [];
  48.         $cities = [];
  49.         foreach ($adverts['data'] as $advert) {
  50.             if (!in_array($advert['localisation']['ville'], $cities)) {
  51.                 $cities[] = $advert['localisation']['ville'];
  52.             }
  53.             if ($advert['info']['vente_location'] === $type) {
  54.                 if (isset($advertFiltered[$advert['prix']['budget']])) {
  55.                     $advertFiltered[$advert['prix']['budget'] + 1] = $advert;
  56.                 } else {
  57.                     $advertFiltered[$advert['prix']['budget']] = $advert;
  58.                 }
  59.             }
  60.         }
  61.         ksort($advertFiltered);
  62.         $pagination $paginator->paginate($advertFiltered$page20);
  63.         return $this->render('front/property/list.html.twig', [
  64.             'contentHeader' => $contentRepository->findOneBy(['page' => ($type === 'vente') ? 'sale' 'rent''section' => 'header']),
  65.             'adverts' => $pagination,
  66.             'countAdverts' => count($advertFiltered),
  67.             'type' => $type,
  68.             'advertCities' => $cities,
  69.             'slider' => $sliderHeaderRepository->findOneBy(['page' => $type]),
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/annonce/{id}/{type}/{city}/details", name="front_property_single")
  74.      */
  75.     public function single(int $idstring $typestring $city)
  76.     {
  77.         $token $this->getToken();
  78.         $data $this->httpClient->request(
  79.             Request::METHOD_GET,
  80.             self::API_LINK $this->getParameter('api_uuid').'/annonces', [
  81.                 'headers' => [
  82.                     'token' => $token,
  83.                 ],
  84.                 'body' => '{"filters":[{"key": "id", "value": "'.$id.'", "compare": "="}]}'
  85.             ]
  86.         );
  87.         $advert $data->toArray();
  88.         return $this->render('front/property/single.html.twig', [
  89.             'advert' => $advert['data'][0]
  90.         ]);
  91.     }
  92.     private function getToken()
  93.     {
  94.         $response $this->httpClient->request(
  95.             Request::METHOD_POST,
  96.             self::API_LINK $this->getParameter('api_uuid').'/token', [
  97.                 'headers' => [
  98.                     'login' => $this->getParameter('api_login'),
  99.                     'password' => $this->getParameter('api_password'),
  100.                 ],
  101.             ]
  102.         );
  103.         return $response->toArray()['data']['token'];
  104.     }
  105.     /**
  106.      * @Route("/filtre/annonces/{advertType}")
  107.      */
  108.     public function filterList(string $advertTypeRequest $request)
  109.     {
  110.         $allType = [];
  111.         if (is_array($request->get('type'))) {
  112.             // Form desktop
  113.             foreach ($request->get('type') as $key => $type) {
  114.                 $allType[] = $key;
  115.             }
  116.         } else {
  117.             // Form mobile
  118.             $allType[] = $request->get('type');
  119.         }
  120.         $token $this->getToken();
  121.         $adverts $this->httpClient->request(
  122.             Request::METHOD_GET,
  123.             self::API_LINK $this->getParameter('api_uuid').'/annonces', [
  124.                 'headers' => [
  125.                     'token' => $token
  126.                 ]
  127.             ]
  128.         );
  129.         $adverts $adverts->toArray();
  130.         $advertsFiltered $adverts['data'];
  131.         foreach ($advertsFiltered as $key => $advert) {
  132.             if ($request->request->get('type')) {
  133.                 if (!empty($allType) && !in_array($advert['info']['nature'], $allType)) {
  134.                     unset($advertsFiltered[$key]);
  135.                 }
  136.             }
  137.             if ($request->request->get('rent') && $advert['info']['vente_location'] !== $request->request->get('rent')) {
  138.                 unset($advertsFiltered[$key]);
  139.             }
  140.             if ($request->request->get('is-mobile')) {
  141.                 if ($request->request->get('advert-type') && $advert['info']['vente_location'] !== $request->request->get('advert-type')) {
  142.                     unset($advertsFiltered[$key]);
  143.                 }
  144.             }
  145.             if ($request->request->get('location') && $advert['localisation']['ville'] !== $request->request->get('location')) {
  146.                 unset($advertsFiltered[$key]);
  147.             }
  148.             if ($request->request->get('max_price') && $advert['prix']['budget'] > $request->request->get('max_price')) {
  149.                 unset($advertsFiltered[$key]);
  150.             }
  151.             if ($request->request->get('min_room') && $advert['info']['nombre_chambres'] < $request->request->get('min_room')) {
  152.                 unset($advertsFiltered[$key]);
  153.             }
  154.         }
  155.         $advertsByPrice = [];
  156.         foreach ($advertsFiltered as $advert) {
  157.             $advertsByPrice[$advert['prix']['budget']] = $advert;
  158.         }
  159.         ksort($advertsByPrice);
  160.         return $this->json([
  161.             'template' => $this->render('front/html/advert_list.html.twig', ['adverts' => $advertsByPrice]),
  162.             'countAdverts' => count($advertsByPrice)
  163.         ]);
  164.     }
  165. }