src/Controller/Front/StoreFinderController.php line 48

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