src/Aqarmap/Bundle/SearchBundle/Controller/Api/V4/ListingSearchController.php line 592
], $response->getStatusCode());
}
#[Rest\Get('/api/v4/listings', options: ['i18n' => false], name: 'aqarmap_api_get_listings')]
#[Rest\Get('/api/v4/listings/search', options: ['i18n' => false], name: 'aqarmap_api_listings_search_v4')]
public function getListings(Request $request, ListingRepository $listingRepository, ListingManager $listingManager, CacheInterface $cache, MessageBusInterface $messageBus)
{
/** @var UserInterface $user */
$user = $this->getUser();
$hasSearchScoringRole = $user && $user->hasRole('ROLE_SEARCH_SCORING');
if (!$hasSearchScoringRole && ($request->get('esdebug') || $request->get('scoredebug'))) {
$request->query->remove('esdebug');
$request->query->remove('scoredebug');
}
if ($request->get('location')) {
$request->query->set('locations', explode(',', $request->query->get('location', '')));
}
$cacheKey = sprintf('api_listings_search_v4_%s_%s', $request->getLocale(), md5(http_build_query($request->query->all())));
$cachedSerialize = $cache->getItem($cacheKey);
if (!$cachedSerialize->isHit() || empty($cachedSerialize->get())) {
$listingsElasticResponse = $listingManager->getListingsElasticResponse($request);
$listingsQueryBuilder = $listingRepository->getListingsByIds(array_column($listingsElasticResponse['items'], 'id'));
if ('aqarmap_api_listings_search_v4' === $request->attributes->get('_route')) {
$listingsElasticResponse['pagination']['totalPages'] = (float) $listingsElasticResponse['pagination']['totalPages'];
}
$listings = $listingsQueryBuilder->getQuery()->getResult();
$data = [
'default' => $this->mapPaginatedBody($listingsElasticResponse, $listings),
'related' => [],
];
$data = $this->serializer->serialize(
$data,
'json',
SerializationContext::create()
->setGroups('SearchV4')
->enableMaxDepthChecks()
);
$cachedSerialize->set($data);
$cachedSerialize->expiresAfter(3600 * 3);
$cache->save($cachedSerialize);
}
return new Response($cachedSerialize->get());
}
#[Rest\Get('/api/v4/listings/debug', options: ['i18n' => false], name: 'aqarmap_api_get_listings_debug')]
public function getListingsEsDebug(Request $request, ListingManager $listingManager)
{
/** @var UserInterface $user */
$user = $this->getUser();
if (!($user && $user->hasRole('ROLE_SEARCH_SCORING'))) {
throw new UnauthorizedHttpException();
}
$request->query->set('esdebug', 1);
if ($request->get('location')) {
$request->query->set('locations', explode(',', $request->query->get('location', '')));
}
$listingsDebugElasticResponse = $listingManager->getListingsDebugElasticResponse($request);
if ('aqarmap_api_listings_search_v4' === $request->attributes->get('_route')) {
$listingsDebugElasticResponse['pagination']['totalPages'] = (float) $listingsDebugElasticResponse['pagination']['totalPages'];
}
$data = [
'default' => $this->mapPaginatedBody($listingsDebugElasticResponse, $listingsDebugElasticResponse['items']),
];
return new JsonResponse($data);
}
#[Rest\Get('/api/v4/listings/search/ssr-data', options: ['i18n' => false], name: 'aqarmap_api_get_listings_search_ssr-data')]
public function getListingsSearchSSRData(Request $request, ListingManager $listingManager, CacheInterface $cache, SectionService $sectionService)
{
$cacheKey = sprintf('api_listings_search_ssr_data_%s_%s', $request->getLocale(), md5(http_build_query($request->query->all())));
$cachedResponse = $cache->getItem($cacheKey);
if (!$cachedResponse->isHit() || empty($cachedResponse->get())) {
$customParagraph = $locationChildren = $slugResolver = $faqData = $locationParents = [];
if ($request->get('location')) {
$request->query->set('locations', explode(',', $request->query->get('location', '')));
}
$location = $request->get('locations') ? $request->get('locations')[0] : null;
$location = $this->locationRepository->findOneBy(['slug' => $location]);
$section = $this->sectionRepository->findOneBy(['slug' => $request->query->get('section')]);
$propertyType = $this->propertyTypeRepository->findOneBy(['slug' => $request->query->get('propertyType')]);
if ($location) {
$locationChildren = $listingManager->getSerializedLocationChildren($location);
$locationParents = $listingManager->getLocationParents($location, $request->getLocale());
}
if ($section && $propertyType && $location) {
$longTail = $this->seoListingSearchService->getSerializedLongTailData($section, $propertyType, $location) ?? [];
}
if ($section && $propertyType) {
$byOwnerOnly = filter_var($request->get('byOwnerOnly'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
$customParagraph = $listingManager->getSerializedCustomParagraph($section, $propertyType, $location, $byOwnerOnly) ?? [];
$slugResolver = $listingManager->getSerializedResolvedSlugs($section, $propertyType, (array) $request->query->get('locations', []));
$faqData = $listingManager->getFaqs($section, $propertyType, $location, $request->getLocale());
}
$response = [
'locationChildren' => !empty($locationChildren) ? json_decode((string) $locationChildren, true) : [],
'longTail' => !empty($longTail) ? json_decode((string) $longTail, true) : [],
'customParagraph' => !empty($customParagraph) ? json_decode((string) $customParagraph, true) : [],
'slugResolver' => !empty($slugResolver) ? json_decode((string) $slugResolver, true) : [],
'faqData' => $faqData,
'locationParents' => $locationParents,
'sections' => json_decode((string) $sectionService->getSerializedSections(), true),
'propertyTypeChips' => $listingManager->getListingPropertyTypesChips($request, $location, $section, $propertyType),
];
$cachedResponse->set(json_encode($response));
$cachedResponse->expiresAfter(3600 * 3);
$cache->save($cachedResponse);
}
return new JsonResponse(json_decode((string) $cachedResponse->get(), true));
}
#[Rest\Get('/api/v4/listings/trigger-search', options: ['i18n' => false], name: 'aqarmap_api_trigger_search_listings_v4')]
public function triggerSearchListings(Request $request, MessageBusInterface $messageBus)
{
$user = $this->getUser();
if ($request->get('location')) {
$request->query->set('locations', explode(',', $request->query->get('location', '')));
}
if ($request->get('keywordSearch')) {
$this->eventDispatcher->dispatch(new SearchTriggerEvent($request));
}
if ($user) {
$messageBus->dispatch(new Search($request->query, $user));
}
return new JsonResponse([
'statusCode' => Response::HTTP_OK,
'statusMessage' => 'Search triggered successfully!',
]);
}
private function mapPaginatedBody(array $result, array $listings): array
{
if (!$result['pagination']) {
return [
'statusCode' => $this->getStatusCode(),
'statusMessage' => $this->getStatusMessage(),
'paginate' => [],
'data' => [],
'errors' => $this->getErrors(),
];
}
return [
'statusCode' => $this->getStatusCode(),
'statusMessage' => $this->getStatusMessage(),
'paginate' => $result['pagination'],
'data' => $listings,
'errors' => $this->getErrors(),
];
}
}