src/Controller/Front/StoreFinderController.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: michael
  5.  * Date: 04/02/2019
  6.  * Time: 15:39
  7.  */
  8. namespace App\Controller\Front;
  9. use App\Entity\Store;
  10. use App\Repository\StoreRepository;
  11. use App\Service\GmapsService;
  12. use App\Service\RefereeService;
  13. use App\Service\StoreService;
  14. use Geocoder\Exception\Exception;
  15. use Http\Discovery\Exception\NotFoundException;
  16. use Knp\Component\Pager\PaginatorInterface;
  17. use Psr\Log\LoggerInterface;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. class StoreFinderController extends AbstractController
  24. {
  25.     /**
  26.      * Lister les centres auditifs sur la page de recherche
  27.      *
  28.      * @Route(
  29.      *     "/finder",
  30.      *     name="find_nearest_store",
  31.      *     methods={"GET", "POST"},
  32.      *     options = { "expose" = true }
  33.      * )
  34.      * @param Request $request
  35.      * @param PaginatorInterface $paginator
  36.      * @param GmapsService $gmapsService
  37.      * @param RefereeService $refereeService
  38.      * @param LoggerInterface $logger
  39.      * @param StoreRepository $storeRepository
  40.      * @param StoreService $storeService
  41.      * @return Response
  42.      * @throws Exception
  43.      */
  44.     public function findNearestStore(Request $requestPaginatorInterface $paginatorGmapsService $gmapsServiceRefereeService $refereeServiceLoggerInterface $loggerStoreRepository $storeRepositoryStoreService $storeService): Response
  45.     {
  46.         if($request->query->has('page') && !is_numeric($request->query->get('page'))){
  47.             throw new NotFoundHttpException('Page not found');
  48.         }
  49.         $geocodeAddress null;
  50.         $arrayPresentableStore = [];
  51.         $arraySelectionStore = [];
  52.         $arraySelectionSerialize = [];
  53.         if ($request->query->has('address')) {
  54.             $queryAddress $request->get('address');
  55.             if(!empty($queryAddress) && $queryAddress != "null") {
  56.                 // geocoding par adresse
  57.                 $logger->info('get address by geocoding : ' $queryAddress);
  58.                 $geocodeAddress $gmapsService->getCoordinateFromAddress($queryAddress);
  59.             } else if($request->query->has('lat') && $request->query->has('lng')) {
  60.                 // reverse geocoding par lat/lng
  61.                 $queryLat $request->get('lat');
  62.                 $queryLng $request->get('lng');
  63.                 $logger->info('get address by reverse geocoding : ' $queryLat ' ; ' $queryLng);
  64.                 $geocodeAddress $gmapsService->getAddressFromCoordinates($queryLat$queryLng);
  65.             }
  66.         }
  67.         if ($geocodeAddress !== null && !$geocodeAddress->isEmpty()) {
  68.             $latitude $geocodeAddress->first()->getCoordinates()->getLatitude();
  69.             $longitude $geocodeAddress->first()->getCoordinates()->getLongitude();
  70.             //store lat/long for later use (display direction on store page map)
  71.             $request->getSession()->set('finder_coordinates', [$latitude$longitude]);
  72.             $logger->info('find store for address : lat(' $latitude ') / lng(' $longitude ')');
  73.             $storeAndDistance $storeRepository->findActiveStoresAroundLocation($latitude$longitude$request->query->get('area_search'Store::AREA_SEARCH));
  74.             if (!empty($storeAndDistance)) {
  75.                 foreach ($storeAndDistance as $storeWithDistance) {
  76.                     // tableau des stores présentables sérialisés pour la vue
  77.                     $arrayPresentableStore[$storeWithDistance[0]->getId()] = $storeService->serializeStoreForView($storeWithDistance[0], $storeWithDistance['distance']);
  78.                 }
  79.                 if($request->query->getInt('page'1) == 1) {
  80.                     // afficher la selection MCA uniquement sur la page 1
  81.                     $arraySelectionStore $refereeService->getHightligthStore($latitude$longitude);
  82.                 }
  83.                 foreach ($arraySelectionStore as $selectedStoreWithDistance) {
  84.                     $selectedStoreId $selectedStoreWithDistance['store']->getId();
  85.                     // retirer la selection de liste des stores présentables
  86.                     unset($arrayPresentableStore[$selectedStoreId]);
  87.                     // tableau des store selectionnés sérialisés pour la vue
  88.                     $arraySelectionSerialize[$selectedStoreId] = $storeService->serializeStoreForView($selectedStoreWithDistance['store'], $selectedStoreWithDistance['distance_reel']);
  89.                 }
  90.                 // pagination des stores pour la vue
  91.                 $paginatorOnStore $paginator->paginate(
  92.                     $arrayPresentableStore,
  93.                     $request->query->getInt('page'1),
  94.                     Store::NUM_STORE_PER_PAGE,
  95.                     array('wrap-queries' => true)
  96.                 );
  97.                 return $this->render('front_v4/mca/store/finder.html.twig', [
  98.                     'stores' => $paginatorOnStore->getItems(),
  99.                     'pagination' => $paginatorOnStore,
  100.                     'preferred_stores' => $arraySelectionSerialize,
  101.                     'formatted_address_query' => $geocodeAddress->first()->getFormattedAddress(),
  102.                     'city_address_query' => $geocodeAddress->first()->getLocality()
  103.                 ]);
  104.             }
  105.             // aucun store trouvé
  106.             return $this->render('front_v4/mca/store/finder.html.twig', [
  107.                 'stores' => null,
  108.                 'pagination' => null,
  109.                 'preferred_stores' => null,
  110.                 'formatted_address_query' => $geocodeAddress->first()->getFormattedAddress(),
  111.                 'city_address_query' => $geocodeAddress->first()->getLocality()
  112.             ]);
  113.         }
  114.         // adresse invalide
  115.         $logger->info('no address found');
  116.         return $this->render('front_v4/mca/store/finder.html.twig', [
  117.             'stores' => null,
  118.             'pagination' => null,
  119.             'preferred_stores' => null,
  120.             'formatted_address_query' => '',
  121.             'city_address_query' => ''
  122.         ]);
  123.     }
  124. }