<?php
namespace App\Controller;
use App\Entity\Location\City;
use App\Entity\Location\Subway\Line;
use App\Entity\Profile\Profile;
use App\Entity\Profile\ProfileService;
use App\Entity\Service;
use App\Repository\ProfileRepository;
use App\Service\ProfileAvatar;
use App\Service\ProfileRecommendationService;
use App\Service\ResponsiveAssetsService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class ProfileRecommendationController extends AbstractController
{
use ProfileMinPriceTrait;
public function getRecommendations(
City $city, int $count, string $imageSize, string $exclude, int $gender, Request $request, ProfileAvatar $profileAvatar,
ResponsiveAssetsService $responsiveAssetsService, ProfileRecommendationService $profileRecommendationService,
KernelInterface $kernel,
): Response
{
$isXhr = $request->isXmlHttpRequest();
$data = json_decode($request->getContent(), true);
$data = is_array($data) ? $data : [];
$exclude = array_values(array_filter(array_map('intval', explode(',', $exclude))));
$filters = $isXhr ? ($data['filter'] ?? []) : [];
$viewedProfiles = $isXhr ? array_slice($data['viewed_profiles'] ?? [], -10) : [];
$viewedProfiles = array_values(array_map('intval', $viewedProfiles));
$viewedRecommendations = $isXhr ? ($data['viewed_recommendations'] ?? []) : [];
$locale = $request->getLocale() ?: 'en';
$profiles = $profileRecommendationService->getRecommendations($city, $filters, $viewedProfiles, $viewedRecommendations, $exclude, $count, $gender);
$profiles = array_map(function (Profile $profile) use ($profileAvatar, $responsiveAssetsService, $city, $imageSize, $request, $locale): array {
$avatar = $profileAvatar->getAvatar($profile);
return [
'id' => $profile->getId(),
'name' => $profile->getName()->getTranslation('ru'),
'description' => $profile->getDescription()->getTranslation('ru'),
'url' => $this->generateUrl('profile_preview.page', ['city' => $city->getUriIdentity(), 'profile' => $profile->getUriIdentity()]),
'image' => $avatar
? $responsiveAssetsService->getResponsiveImageUrl($profileAvatar->getAvatar($profile)->getPath(), 'profile_media', $imageSize, 'jpg')
: '',
'approved' => $profile->isApproved(),
'station' => $profile->getStations()->count() ? [
'name' => $profile->getPrimaryStation()->getName()->getTranslation($locale),
'lines' => array_map(function (Line $line): array {
return [
'name' => $line->name(),
'color' => $line->color(),
];
}, $profile->getPrimaryStation()->lines()->toArray()),
] : null,
'age' => $profile->getPersonParameters()->getAge(),
'breast_size' => $profile->getPersonParameters()->getBreastSize(),
'height' => $profile->getPersonParameters()->getHeight(),
'weight' => $profile->getPersonParameters()->getWeight(),
'phone' => $profile->getPhoneNumber(),
'price' => $this->getMinPrice($profile->getApartmentsPricing(), $profile->getTakeOutPricing()),
'prices' => [
'in' => [
'has' => $profile->getApartmentsPricing()->isProvided(),
'1h' => $profile->getApartmentsPricing()->getOneHourPrice(),
'2h' => $profile->getApartmentsPricing()->getTwoHoursPrice(),
'night' => $profile->getApartmentsPricing()->getNightPrice(),
],
'out' => [
'has' => $profile->getTakeOutPricing()->isProvided(),
'1h' => $profile->getTakeOutPricing()->getOneHourPrice(),
'2h' => $profile->getTakeOutPricing()->getTwoHoursPrice(),
'night' => $profile->getTakeOutPricing()->getNightPrice(),
]
],
'services' => array_map(function (ProfileService $service) {
return [
'uri' => $service->getService()->getUriIdentity(),
'name' => $service->getService()->getName(),
'group' => $service->getService()->getGroup(),
];
}, $profile->getProvidedServices()->toArray()),
'has_video' => $profile->hasVideo(),
'has_selfie' => $profile->hasSelfie(),
'has_comments' => $profile->isCommented(),
];
}, $profiles);
//dump($profiles);
$response = [
'profiles' => $profiles,
'clear_viewed_recommendations' => $profileRecommendationService->isNeededToClearViewedRecommendations(),
];
if ($kernel->getEnvironment() == 'dev' || $kernel->getEnvironment() == 'review') {
$viewedProfilesDebug = $this->render('Recommendations/_profile_recommendations_debug_profile_data_table.html.twig', [
'viewed_profiles' => $profileRecommendationService->viewedProfiles(),
]);
$response = array_merge($response, [
'viewed_profiles_debug' => $viewedProfilesDebug->getContent(),
'coeffs_debug' => $profileRecommendationService->getCoeffsDataDebug(),
'query_debug' => $profileRecommendationService->getQueryDataDebug(),
'scores' => $profileRecommendationService->getScoreDataDebug(),
'viewed_recommendations_debug' => $profileRecommendationService->getViewedRecommendationsDataDebug(),
]);
}
return $isXhr ? new JsonResponse($response, Response::HTTP_OK) : $this->render('Recommendations/_profile_recommendations.partial.html.twig', $response);;
}
}