src/Controller/ProfileRecommendationController.php line 53

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