src/Controller/MapController.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Location\City;
  4. use App\Entity\Profile\Genders;
  5. use App\Entity\Saloon\Saloon;
  6. use App\Entity\ServiceGroups;
  7. use App\Event\Profile\ProfilesShownEvent;
  8. use App\Form\FilterMapForm;
  9. use App\Repository\CityRepository;
  10. use App\Repository\ProfileRepository;
  11. use App\Repository\ReadModel\ProfileMapReadModel;
  12. use App\Repository\SaloonRepository;
  13. use App\Repository\ServiceRepository;
  14. use App\Service\Features;
  15. use App\Service\ProfileList;
  16. use App\Service\Map\MapFallbackService;
  17. use App\Service\Map\MapContextResolver;
  18. use App\Specification\Profile\ProfileHasMapCoordinates;
  19. use App\Specification\Profile\ProfileIsSuitableForTheMap;
  20. use App\Specification\Profile\ProfileIdINOrderedByINValues;
  21. use App\Specification\Profile\ProfileIsLocated;
  22. use App\Specification\QueryModifier\PossibleSaloonAdBoardPlacement;
  23. use App\Specification\QueryModifier\PossibleSaloonPlacementHiding;
  24. use App\Specification\Saloon\SaloonIsNotHidden;
  25. use App\Specification\QueryModifier\SaloonThumbnail;
  26. use App\Specification\Saloon\SaloonIsActive;
  27. use App\Specification\Saloon\SaloonIsSuitableForTheMap;
  28. use Happyr\DoctrineSpecification\Spec;
  29. use Psr\Cache\CacheItemPoolInterface;
  30. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  31. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  32. use Symfony\Component\Asset\Packages;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. use Symfony\Component\HttpFoundation\JsonResponse;
  35. use Symfony\Component\HttpFoundation\Request;
  36. use Symfony\Component\HttpFoundation\Response;
  37. use Symfony\Contracts\Cache\ItemInterface;
  38. use Symfony\Contracts\Translation\TranslatorInterface;
  39. class MapController extends AbstractController
  40. {
  41.     use ProfileMinPriceTrait;
  42.     const MAP_PROFILES_CACHE_ITEM_NAME = 'map_profiles_';
  43.     public function __construct(
  44.         private ProfileRepository        $profileRepository,
  45.         private CityRepository           $cityRepository,
  46.         private Features                 $features,
  47.         private Packages                 $assetPackage,
  48.         private SaloonRepository         $saloonRepository,
  49.         private ProfileList              $profileList,
  50.         private EventDispatcherInterface $eventDispatcher,
  51.         private ServiceRepository        $serviceRepository,
  52.         private CacheItemPoolInterface   $profilesFilterCache,
  53.         private MapFallbackService       $mapFallbackService,
  54.         private MapContextResolver       $mapContextResolver,
  55.     )
  56.     {
  57.     }
  58.     #[ParamConverter("city", converter: "city_converter")]
  59.     public function page(City $city): Response
  60.     {
  61.         return $this->render('Map/page.html.twig', [
  62.             'cityUriIdentity' => $city->getUriIdentity(),
  63.             'cityLatitude' => $city->getMapCoordinate()->getLatitude(),
  64.             'cityLongitude' => $city->getMapCoordinate()->getLongitude(),
  65.             'multipleCities' => (int)$this->features->multiple_cities(),
  66.         ]);
  67.     }
  68.     public function form(City $city, Request $request): Response
  69.     {
  70.         $form = $this->createForm(FilterMapForm::class, null, [
  71.             'data' => ['city_id' => $city->getId()],
  72.             'map_context' => $this->mapContextResolver->isMainMap($request) ? 'main' : 'listing',
  73.         ]);
  74.         return $this->render('Map/form.html.twig', [
  75.             'form' => $form->createView(),
  76.         ]);
  77.     }
  78.     public function filter(Request $request, TranslatorInterface $translator): Response
  79.     {
  80.         $params = json_decode($request->request->get('form'), true);
  81.         $form = $this->createForm(FilterMapForm::class);
  82.         $formParams = $params;
  83.         unset($formParams['scale']);
  84.         $form->submit($formParams);
  85.         $scale = $request->request->get('scale') ?? 0;
  86.         if ($scale <= 8) {
  87.             $coordsRoundPrecision = 1;
  88.         } else if ($scale <= 14) {
  89.             $coordsRoundPrecision = 2;
  90.         } else {
  91.             $coordsRoundPrecision = 4;
  92.         }
  93.         $city = $this->cityRepository->find($params['city_id']);
  94.         $profiles = $this->profileList->listForMap(
  95.             $city, null, $form->getData(), [
  96.             new ProfileHasMapCoordinates(),
  97.             new ProfileIsSuitableForTheMap(),
  98.         ], true, null,
  99.             ProfileList::ORDER_NONE, [Genders::FEMALE], $coordsRoundPrecision,
  100.         );
  101.         $specs = Spec::andX(
  102.             new SaloonIsSuitableForTheMap(),
  103.             new SaloonThumbnail(),
  104.             ProfileIsLocated::withinCity($city),
  105.             new ProfileHasMapCoordinates(),
  106.         );
  107.         $saloons = $this->saloonRepository->listForMapMatchingSpec($specs, $coordsRoundPrecision);
  108.         $rowConverter = function ($row) {
  109.             $row = array_values($row);
  110.             //id
  111.             $row[0] = array_map('intval', explode(',', $row[0]));
  112.             //coords
  113.             $row[3] = array_map('floatval', explode(',', $row[$row[2] == 1 ? 1 : 3]));
  114.             //is_masseur
  115.             if (isset($row[4])) {
  116.                 $row[4] = array_map('intval', explode(',', $row[4]));
  117.             }
  118.             array_splice($row, 1, 1);
  119.             return $row;
  120.         };
  121.         $out = [
  122.             'profiles' => array_map($rowConverter, $profiles),
  123.             'saloons' => array_map($rowConverter, $saloons),
  124.         ];
  125.         if ([] === $profiles && [] === $saloons && $form->isValid()
  126.             && $this->mapContextResolver->isMainMap($request, $params)
  127.         ) {
  128.             $out['profiles'] = $this->mapFallbackService->forLegacy($city, $coordsRoundPrecision);
  129.         }
  130.         return $this->json($out, headers: ['Cache-Control' => 'private, no-store']);
  131.     }
  132.     public function detail(Request $request, TranslatorInterface $translator): Response
  133.     {
  134.         $services = $this->serviceRepository->allIndexedById();
  135.         $profileIds = ($requestedProfiles = $request->request->get('profiles')) ? explode(',', $requestedProfiles) : [];
  136.         $saloonIds = ($requestedSaloons = $request->request->get('saloons')) ? explode(',', $requestedSaloons) : [];
  137.         $result = !empty($profileIds) ? $this->profileRepository->fetchMapProfilesByIds(new ProfileIdINOrderedByINValues($profileIds)) : [];
  138.         $profiles = [];
  139.         foreach ($result as /** @var ProfileMapReadModel $profile */ $profile) {
  140.             if (!$profile->mapLatitude || !$profile->mapLongitude)
  141.                 continue;
  142.             $path = $profile->avatar['path'];
  143.             $path = str_starts_with($path, '/') ? $path : substr($path, 6, -4);
  144.             $hasApartment = $profile->apartmentOneHourPrice || $profile->apartmentTwoHoursPrice || $profile->apartmentNightPrice;
  145.             $hasTakeout = $profile->takeOutOneHourPrice || $profile->takeOutTwoHoursPrice || $profile->takeOutNightPrice;
  146.             $tags = [];
  147.             if ($hasApartment && !$hasTakeout)
  148.                 $tags[] = 1;
  149.             elseif (!$hasApartment && $hasTakeout)
  150.                 $tags[] = 2;
  151.             elseif ($hasApartment && $hasTakeout)
  152.                 $tags[] = 3;
  153.             foreach ($profile->services as $serviceId) {
  154.                 $service = $services[$serviceId];
  155.                 switch ($service->getUriIdentity()) {
  156.                     case 'seks-klassicheskij':
  157.                     case 'sex':
  158.                         $tags[] = 4;
  159.                         break;
  160.                     case 'seks-analnyij':
  161.                     case 'anal-sex':
  162.                         $tags[] = 5;
  163.                         break;
  164.                     case 'minet-bez-rezinki':
  165.                     case 'blowjob-without-condom':
  166.                         $tags[] = 6;
  167.                         break;
  168.                     case 'kunnilingus':
  169.                     case 'cunnilingus-for-me':
  170.                         $tags[] = 7;
  171.                         break;
  172.                     case 'okonchanie-v-rot':
  173.                     case 'cum-in-mouth':
  174.                         $tags[] = 8;
  175.                         break;
  176.                 }
  177.                 if (ServiceGroups::MASSAGE === $service->getGroup() && false === in_array(9, $tags, true)) {
  178.                     $tags[] = 9;
  179.                 }
  180.             }
  181.             $profiles[] = [
  182.                 1,
  183.                 (float)rtrim(substr($profile->mapLatitude, 0, 7), '0'),
  184.                 (float)rtrim(substr($profile->mapLongitude, 0, 7), '0'),
  185.                 $profile->uriIdentity,
  186.                 $profile->name,
  187.                 $path, //$profile->avatar['path'],
  188.                 //$profile->avatar['type'] ? 'avatar' : 'photo',
  189.                 str_replace(' ', '', $profile->phoneNumber),
  190.                 $profile->station ?? 0,
  191.                 $profile->apartmentOneHourPrice ?? $profile->takeOutOneHourPrice ?? 0,
  192.                 $profile->apartmentTwoHoursPrice ?? $profile->takeOutTwoHoursPrice ?? 0,
  193.                 $profile->apartmentNightPrice ?? $profile->takeOutNightPrice ?? 0,
  194.                 (int)$profile->isApproved,
  195.                 (int)$profile->isMasseur,
  196.                 (int)$profile->hasComments,
  197.                 (int)$profile->hasSelfies,
  198.                 (int)$profile->hasVideos,
  199.                 $profile->age ?? 0,
  200.                 $profile->breastSize ?? 0,
  201.                 $profile->height ?? 0,
  202.                 $profile->weight ?? 0,
  203.                 $tags,
  204.                 $profile->id,
  205.                 (int)$profile->isPaid ?? 0,
  206.             ];
  207.         }
  208.         $result = !empty($saloonIds) ? $this->saloonRepository->matchingSpecRaw(new ProfileIdINOrderedByINValues($saloonIds), null, false) : [];
  209.         $saloons = [];
  210.         foreach ($result as /** @var Saloon $saloon */ $saloon) {
  211.             $photoPath = null !== ($mainPhoto = $saloon->getThumbnail()) ? $mainPhoto->getPath() : '';
  212.             $photoPath = str_starts_with($photoPath, '/') ? $photoPath : str_replace(".jpg", "", substr($photoPath, 6));
  213.             $saloons[] = [
  214.                 2,
  215.                 (float)rtrim(substr($saloon->getMapCoordinate()->getLatitude(), 0, 7), '0'),
  216.                 (float)rtrim(substr($saloon->getMapCoordinate()->getLongitude(), 0, 7), '0'),
  217.                 $saloon->getUriIdentity(),
  218.                 $translator->trans($saloon->getName()),
  219.                 $photoPath,
  220.                 //'thumb',
  221.                 str_replace(' ', '', $saloon->getPhoneNumber()),
  222.                 $saloon->getPrimaryStation()?->getId() ?? 0,
  223.                 $saloon->getApartmentsPricing()->getOneHourPrice() ?? $saloon->getTakeOutPricing()->getOneHourPrice() ?? 0,
  224.                 $saloon->getApartmentsPricing()->getTwoHoursPrice() ?? $saloon->getTakeOutPricing()->getTwoHoursPrice() ?? 0,
  225.                 $saloon->getApartmentsPricing()->getNightPrice() ?? $saloon->getTakeOutPricing()->getNightPrice() ?? 0,
  226.                 //$saloon->getExpressPricing()->isProvided() ? $saloon->getExpressPricing()->getPrice() ?? 0 : 0,
  227.                 (int)($saloon?->getAdBoardPlacement() !== null),
  228.                 $saloon->getId(),
  229.             ];
  230.         }
  231.         return new JsonResponse([
  232.             'profiles' => $profiles,
  233.             'saloons' => $saloons,
  234.         ]);
  235.     }
  236.     public function processProfileShows(Request $request, ProfileRepository $profileRepository): JsonResponse
  237.     {
  238.         $id = $request->query->get('id');
  239.         $profile = $profileRepository->find($id);
  240.         if ($profile) {
  241.             $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'map'), ProfilesShownEvent::NAME);
  242.         }
  243.         return $this->json([]);
  244.     }
  245.     public function cachedMapProfilesResultByIds(ProfileIdINOrderedByINValues $specification)
  246.     {
  247.         $key = sha1(self::MAP_PROFILES_CACHE_ITEM_NAME . implode(',', $specification->getIds()));
  248.         return $this->profilesFilterCache->get($key, function (ItemInterface $item) use ($specification) {
  249.             return $this->profileRepository->fetchMapProfilesByIds($specification);
  250.         });
  251.     }
  252. }