src/Controller/StdGeoLocationController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Admin\Entity\StdGeoMap;
  4. use App\Service\StdGeoLocationService;
  5. use App\Service\StdTrackingService;
  6. use App\Utils\Functions;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class StdGeoLocationController
  12. {
  13.     private StdGeoLocationService $geoLocationService;
  14.     private StdTrackingService $stdTrackingService;
  15.     public function __construct(StdGeoLocationService $geoLocationServiceStdTrackingService $stdTrackingService)
  16.     {
  17.         $this->geoLocationService $geoLocationService;
  18.         $this->stdTrackingService $stdTrackingService;
  19.     }
  20.     /**
  21.      * Verify if the user's current location matches the session-stored location.
  22.      * Returns whether the location is still valid and if an update is needed.
  23.      */
  24.     #[Route('/geo-location/verify'methods: ['GET'])]
  25.     public function verifyLocation(Request $request): JsonResponse
  26.     {
  27.         $lon $request->query->get('lon');
  28.         $lat $request->query->get('lat');
  29.         if (!$lon || !$lat) {
  30.             return new JsonResponse(['error' => 'Missing parameters: lon and lat'], 400);
  31.         }
  32.         $sessionLocationIds $request->getSession()->get('close_location_ids', []);
  33.         $sessionCity $request->getSession()->get('city');
  34.         $sessionIsInPortugal $request->getSession()->get('isInPortugal');
  35.         // Get current location based on coordinates
  36.         $currentLocation $this->geoLocationService->getAllLocationsByProximity((float)$lon, (float)$lat500);
  37.         
  38.         $currentCity null;
  39.         $currentIsInPortugal false;
  40.         $currentLocationIds = [];
  41.         if (!empty($currentLocation['userLocation']) && $currentLocation['userLocation']->getDistrictName() !== 'Fora de Portugal') {
  42.             $currentCity $currentLocation['userLocation']->getMunicipalityName();
  43.             $currentIsInPortugal true;
  44.             
  45.             if (isset($currentLocation['closeLocations']) && is_array($currentLocation['closeLocations'])) {
  46.                 foreach ($currentLocation['closeLocations'] as $closeLocation) {
  47.                     if ($closeLocation && method_exists($closeLocation'getDomainValue')) {
  48.                         $currentLocationIds[] = $closeLocation->getDomainValue()->getId();
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         // Check if location has changed
  54.         $needsUpdate false;
  55.         $reason null;
  56.         // Compare city
  57.         if ($currentCity !== $sessionCity) {
  58.             $needsUpdate true;
  59.             $reason 'city_changed';
  60.         }
  61.         // Compare Portugal status
  62.         if ($currentIsInPortugal !== $sessionIsInPortugal) {
  63.             $needsUpdate true;
  64.             $reason 'country_status_changed';
  65.         }
  66.         // Compare first location ID (primary location)
  67.         if (!empty($currentLocationIds) && !empty($sessionLocationIds)) {
  68.             if ($currentLocationIds[0] !== $sessionLocationIds[0]) {
  69.                 $needsUpdate true;
  70.                 $reason 'primary_location_changed';
  71.             }
  72.         } elseif (empty($currentLocationIds) !== empty($sessionLocationIds)) {
  73.             $needsUpdate true;
  74.             $reason 'location_availability_changed';
  75.         }
  76.         return new JsonResponse([
  77.             'valid' => !$needsUpdate,
  78.             'needs_update' => $needsUpdate,
  79.             'reason' => $reason,
  80.             'session_city' => $sessionCity,
  81.             'current_city' => $currentCity,
  82.             'session_is_in_portugal' => $sessionIsInPortugal,
  83.             'current_is_in_portugal' => $currentIsInPortugal
  84.         ]);
  85.     }
  86.     #[Route('/geo-location/accept'methods: ['GET'])]
  87.     public function getLocation(Request $request): JsonResponse
  88.     {
  89.         $encryptionKey getenv('ANALYTICS_ENCRYPTION_KEY');
  90.         if (!$encryptionKey) {
  91.             throw new \RuntimeException("Missing ANALYTICS_ENCRYPTION_KEY");
  92.         }
  93.         $lon $request->query->get('lon');
  94.         $lat $request->query->get('lat');
  95.         $distance $request->query->get('distance');
  96.         if (!$lon || !$lat) {
  97.             return new JsonResponse(['error' => 'Missing parameters: lon and lat'], 400);
  98.         }
  99.         $distanceThreshold $distance ? (float)$distance 0;
  100.         if ($distanceThreshold 100000) {
  101.             return new JsonResponse(['error' => 'Distance threshold exceeds the maximum allowed value of 100 km'], 400);
  102.         }
  103.         $location $this->geoLocationService->getAllLocationsByProximity((float)$lon, (float)$lat$distanceThreshold);
  104.         if (empty($location['userLocation'])) {
  105.             $location['userLocation'] = new StdGeoMap();
  106.             $location['userLocation']->setDistrictName('Fora de Portugal');
  107.             $location['userLocation']->setMunicipalityName('Fora de Portugal');
  108.         }
  109.         $this->stdTrackingService->addPreciseLocationToUser(
  110.             Functions::encryptSha256($request->getClientIp(), getenv('ANALYTICS_ENCRYPTION_KEY')),
  111.             $location['userLocation']->getMunicipalityName(), $location['userLocation']->getDistrictName());
  112.         $closeLocationIds = [];
  113.         if (isset($location['closeLocations']) && is_array($location['closeLocations'])) {
  114.             foreach ($location['closeLocations'] as $closeLocation) {
  115.                 if ($closeLocation && method_exists($closeLocation'getId')) {
  116.                     $closeLocationIds[] = $closeLocation->getDomainValue()->getId();
  117.                 }
  118.             }
  119.         }
  120.         if($location['userLocation']->getDistrictName() != 'Fora de Portugal') {
  121.             $request->getSession()->set('isInPortugal'true);
  122.             $request->getSession()->set('country''Portugal');
  123.             $request->getSession()->set('city'$location['userLocation']->getMunicipalityName());
  124.             $request->getSession()->set('close_location_ids'$closeLocationIds);
  125.         } else {
  126.             $request->getSession()->set('isInPortugal'false);
  127.         }
  128.         return new JsonResponse($closeLocationIds);
  129.     }
  130. }