src/Controller/ProfileRecommendationController.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Location\City;
  4. use App\Entity\Location\Subway\Line;
  5. use App\Entity\Profile\Profile;
  6. use App\Entity\Profile\ProfileService;
  7. use App\Entity\Service;
  8. use App\Repository\ProfileRepository;
  9. use App\Service\ProfileAvatar;
  10. use App\Service\ProfileRecommendationService;
  11. use App\Service\ResponsiveAssetsService;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\KernelInterface;
  17. class ProfileRecommendationController extends AbstractController
  18. {
  19.     use ProfileMinPriceTrait;
  20.     public function getRecommendations(
  21.         City $cityint $countstring $imageSizestring $excludeint $genderRequest $requestProfileAvatar $profileAvatar,
  22.         ResponsiveAssetsService $responsiveAssetsServiceProfileRecommendationService $profileRecommendationService,
  23.         KernelInterface $kernel,
  24.     ): Response
  25.     {
  26.         $isXhr $request->isXmlHttpRequest();
  27.         $data json_decode($request->getContent(), true);
  28.         $data is_array($data) ? $data : [];
  29.         $exclude array_values(array_filter(array_map('intval'explode(','$exclude))));
  30.         $filters $isXhr ? ($data['filter'] ?? []) : [];
  31.         $viewedProfiles $isXhr array_slice($data['viewed_profiles'] ?? [], -10) : [];
  32.         $viewedProfiles array_values(array_map('intval'$viewedProfiles));
  33.         $viewedRecommendations $isXhr ? ($data['viewed_recommendations'] ?? []) : [];
  34.         $locale $request->getLocale() ?: 'en';
  35.         $profiles $profileRecommendationService->getRecommendations($city$filters$viewedProfiles$viewedRecommendations$exclude$count$gender);
  36.         $profiles array_map(function (Profile $profile) use ($profileAvatar$responsiveAssetsService$city$imageSize$request$locale): array {
  37.             $avatar $profileAvatar->getAvatar($profile);
  38.             return [
  39.                 'id' => $profile->getId(),
  40.                 'name' => $profile->getName()->getTranslation('ru'),
  41.                 'description' => $profile->getDescription()->getTranslation('ru'),
  42.                 'url' => $this->generateUrl('profile_preview.page', ['city' => $city->getUriIdentity(), 'profile' => $profile->getUriIdentity()]),
  43.                 'image' => $avatar
  44.                     $responsiveAssetsService->getResponsiveImageUrl($profileAvatar->getAvatar($profile)->getPath(), 'profile_media'$imageSize'jpg')
  45.                     : '',
  46.                 'approved' => $profile->isApproved(),
  47.                 'station' => $profile->getStations()->count() ? [
  48.                     'name' => $profile->getPrimaryStation()->getName()->getTranslation($locale),
  49.                     'lines' => array_map(function (Line $line): array {
  50.                         return [
  51.                             'name' => $line->name(),
  52.                             'color' => $line->color(),
  53.                         ];
  54.                     }, $profile->getPrimaryStation()->lines()->toArray()),
  55.                 ] : null,
  56.                 'age' => $profile->getPersonParameters()->getAge(),
  57.                 'breast_size' => $profile->getPersonParameters()->getBreastSize(),
  58.                 'height' => $profile->getPersonParameters()->getHeight(),
  59.                 'weight' => $profile->getPersonParameters()->getWeight(),
  60.                 'phone' => $profile->getPhoneNumber(),
  61.                 'price' => $this->getMinPrice($profile->getApartmentsPricing(), $profile->getTakeOutPricing()),
  62.                 'prices' => [
  63.                     'in' => [
  64.                         'has' => $profile->getApartmentsPricing()->isProvided(),
  65.                         '1h' => $profile->getApartmentsPricing()->getOneHourPrice(),
  66.                         '2h' => $profile->getApartmentsPricing()->getTwoHoursPrice(),
  67.                         'night' => $profile->getApartmentsPricing()->getNightPrice(),
  68.                     ],
  69.                     'out' => [
  70.                         'has' => $profile->getTakeOutPricing()->isProvided(),
  71.                         '1h' => $profile->getTakeOutPricing()->getOneHourPrice(),
  72.                         '2h' => $profile->getTakeOutPricing()->getTwoHoursPrice(),
  73.                         'night' => $profile->getTakeOutPricing()->getNightPrice(),
  74.                     ]
  75.                 ],
  76.                 'services' => array_map(function (ProfileService $service) {
  77.                     return [
  78.                         'uri' => $service->getService()->getUriIdentity(),
  79.                         'name' => $service->getService()->getName(),
  80.                         'group' => $service->getService()->getGroup(),
  81.                     ];
  82.                 }, $profile->getProvidedServices()->toArray()),
  83.                 'has_video' => $profile->hasVideo(),
  84.                 'has_selfie' => $profile->hasSelfie(),
  85.                 'has_comments' => $profile->isCommented(),
  86.             ];
  87.         }, $profiles);
  88.         //dump($profiles);
  89.         $response = [
  90.             'profiles' => $profiles,
  91.             'clear_viewed_recommendations' => $profileRecommendationService->isNeededToClearViewedRecommendations(),
  92.         ];
  93.         if ($kernel->getEnvironment() == 'dev' || $kernel->getEnvironment() == 'review') {
  94.             $viewedProfilesDebug $this->render('Recommendations/_profile_recommendations_debug_profile_data_table.html.twig', [
  95.                 'viewed_profiles' => $profileRecommendationService->viewedProfiles(),
  96.             ]);
  97.             $response array_merge($response, [
  98.                 'viewed_profiles_debug' => $viewedProfilesDebug->getContent(),
  99.                 'coeffs_debug' => $profileRecommendationService->getCoeffsDataDebug(),
  100.                 'query_debug' => $profileRecommendationService->getQueryDataDebug(),
  101.                 'scores' => $profileRecommendationService->getScoreDataDebug(),
  102.                 'viewed_recommendations_debug' => $profileRecommendationService->getViewedRecommendationsDataDebug(),
  103.             ]);
  104.         }
  105.         return $isXhr ? new JsonResponse($responseResponse::HTTP_OK) : $this->render('Recommendations/_profile_recommendations.partial.html.twig'$response);;
  106.     }
  107. }