src/Controller/Front/PropertyController.php line 39

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.     private string $projectDir;
  23.     public function __construct(HttpClientInterface $httpClientCacheInterface $cachestring $projectDir)
  24.     {
  25.         $this->httpClient $httpClient;
  26.         $this->cache $cache;
  27.         $this->projectDir $projectDir;
  28.     }
  29.     /**
  30.      * @Route("/annonces/{type}/{page}", defaults={"page"=1}, name="front_property_list")
  31.      */
  32.     public function list(string $typeint $pageContentRepository $contentRepositoryPaginatorInterface $paginatorSliderHeaderRepository $sliderHeaderRepository)
  33.     {
  34.         $adverts $this->getCachedAdverts($type);
  35.         $advertFiltered = [];
  36.         $cities = [];
  37.         foreach ($adverts['data'] as $advert) {
  38.             $photo $advert['photos'][1]['name'] ?? null;
  39.             if (!$photo || !file_exists($this->projectDir '/public/upload/photo/' $photo)) {
  40.                 continue;
  41.             }
  42.             if (!in_array($advert['localisation']['ville'], $cities)) {
  43.                 $cities[] = $advert['localisation']['ville'];
  44.             }
  45.             if ($advert['info']['vente_location'] === $type) {
  46.                 $advertFiltered[] = $advert;
  47.             }
  48.         }
  49.         $this->sortAdvert($advertFiltered);
  50.         $pagination $paginator->paginate($advertFiltered$page20);
  51.         return $this->render('front/property/list.html.twig', [
  52.             'contentHeader' => $contentRepository->findOneBy(['page' => ($type === 'vente') ? 'sale' 'rent''section' => 'header']),
  53.             'adverts'       => $pagination,
  54.             'countAdverts'  => count($advertFiltered),
  55.             'type'          => $type,
  56.             'advertCities'  => $cities,
  57.             'slider'        => $sliderHeaderRepository->findOneBy(['page' => $type]),
  58.         ])->setSharedMaxAge(1800);
  59.     }
  60.     /**
  61.      * @Route("/annonce/{id}/{type}/{city}/details", name="front_property_single")
  62.      */
  63.     public function single(int $idstring $typestring $city)
  64.     {
  65.         $token $this->getToken();
  66.         $data $this->httpClient->request(
  67.             Request::METHOD_GET,
  68.             self::API_LINK $this->getParameter('api_uuid') . '/annonces', [
  69.                 'headers' => [
  70.                     'token' => $token,
  71.                 ],
  72.                 'body'    => '{"filters":[{"key": "id", "value": "' $id '", "compare": "="}]}'
  73.             ]
  74.         );
  75.         $advert $data->toArray();
  76.         return $this->render('front/property/single.html.twig', [
  77.             'advert' => $advert['data'][0]
  78.         ])->setSharedMaxAge(1800);
  79.     }
  80.     private function getToken()
  81.     {
  82.         $response $this->httpClient->request(
  83.             Request::METHOD_POST,
  84.             self::API_LINK $this->getParameter('api_uuid') . '/token', [
  85.                 'headers' => [
  86.                     'login'    => $this->getParameter('api_login'),
  87.                     'password' => $this->getParameter('api_password'),
  88.                 ],
  89.             ]
  90.         );
  91.         return $response->toArray()['data']['token'];
  92.     }
  93.     /**
  94.      * @Route("/filtre/annonces/{advertType}")
  95.      */
  96.     public function filterList(string $advertTypeRequest $request)
  97.     {
  98.         $allType = [];
  99.         if (is_array($request->get('type'))) {
  100.             // Form desktop
  101.             foreach ($request->get('type') as $key => $type) {
  102.                 $allType[] = $key;
  103.             }
  104.         } else {
  105.             // Form mobile
  106.             $allType[] = $request->get('type');
  107.         }
  108.         $adverts $this->getCachedAdverts($advertType);
  109.         $advertsFiltered $adverts['data'];
  110.         foreach ($advertsFiltered as $key => $advert) {
  111.             $photo $advert['photos'][1]['name'] ?? null;
  112.             if (!$photo || !file_exists($this->projectDir '/public/upload/photo/' $photo)) {
  113.                 continue;
  114.             }
  115.             if ($request->request->get('type')) {
  116.                 if (!empty($allType) && !in_array($advert['info']['nature'], $allType)) {
  117.                     unset($advertsFiltered[$key]);
  118.                 }
  119.             }
  120.             if ($request->request->get('rent') && $advert['info']['vente_location'] !== $request->request->get('rent')) {
  121.                 unset($advertsFiltered[$key]);
  122.             }
  123.             if ($request->request->get('is-mobile')) {
  124.                 if ($request->request->get('advert-type') && $advert['info']['vente_location'] !== $request->request->get('advert-type')) {
  125.                     unset($advertsFiltered[$key]);
  126.                 }
  127.             }
  128.             if ($request->request->get('location') && $advert['localisation']['ville'] !== $request->request->get('location')) {
  129.                 unset($advertsFiltered[$key]);
  130.             }
  131.             if ($request->request->get('max_price') && $advert['prix']['budget'] > $request->request->get('max_price')) {
  132.                 unset($advertsFiltered[$key]);
  133.             }
  134.             if ($request->request->get('min_room') && $advert['info']['nombre_chambres'] < $request->request->get('min_room')) {
  135.                 unset($advertsFiltered[$key]);
  136.             }
  137.         }
  138.         $advertsByPrice = [];
  139.         foreach ($advertsFiltered as $advert) {
  140.             $advertsByPrice[] = $advert;
  141.         }
  142.         $this->sortAdvert($advertsByPrice);
  143.         return $this->json([
  144.             'template'     => $this->renderView('front/html/advert_list.html.twig', ['adverts' => $advertsByPrice]),
  145.             'countAdverts' => count($advertsByPrice)
  146.         ]);
  147.     }
  148.     /**
  149.      * @return mixed
  150.      * @throws \Psr\Cache\InvalidArgumentException
  151.      */
  152.     public function getCachedAdverts(string $type)
  153.     {
  154.         $this->advertType $type;
  155.         return $this->cache->get('adverts', function (ItemInterface $item) {
  156.             $item->expiresAfter(1800);
  157.             $this->token $this->getToken();
  158.             $adverts $this->httpClient->request(
  159.                 Request::METHOD_GET,
  160.                 self::API_LINK $this->getParameter('api_uuid') . '/annonces', [
  161.                     'headers' => [
  162.                         'token' => $this->token
  163.                     ],
  164.                     'body'    => '{"filters":[{"key": "vente_location", "value": "' $this->advertType '", "compare": "="}]}'
  165.                 ]
  166.             );
  167.             return $adverts->toArray();
  168.         });
  169.     }
  170.     protected function sortAdvert(array &$adverts) {
  171.         usort($adverts, function($a$b) {
  172.             if ($a['prix']['budget'] == $b['prix']['budget']) {
  173.                 return 0;
  174.             }
  175.             return ($a['prix']['budget'] < $b['prix']['budget']) ? -1;
  176.         });
  177.     }
  178. }