src/Aqarmap/Bundle/SearchBundle/Controller/Api/SearchResultsController.php line 174

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\SearchBundle\Controller\Api;
  3. use Aqarmap\Bundle\ListingBundle\Builder\BuilderInterface;
  4. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  5. use Aqarmap\Bundle\ListingBundle\Entity\PropertyType;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Section;
  7. use Aqarmap\Bundle\ListingBundle\Repository\LocationRepository;
  8. use Aqarmap\Bundle\ListingBundle\Repository\PropertyTypeRepository;
  9. use Aqarmap\Bundle\ListingBundle\Repository\SectionRepository;
  10. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  11. use Aqarmap\Bundle\MainBundle\Constant\CustomParagraphPlaceTypes;
  12. use Aqarmap\Bundle\MainBundle\Controller\Api\BaseController;
  13. use Aqarmap\Bundle\MainBundle\Repository\CustomParagraphRepository;
  14. use Aqarmap\Bundle\MainBundle\Service\MenuBuilderService;
  15. use Aqarmap\Bundle\SearchBundle\Services\SEOListingSearchService;
  16. use FOS\RestBundle\Controller\Annotations as Rest;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class SearchResultsController extends BaseController
  21. {
  22.     /**
  23.      * @Rest\Get("/api/v2/search/slug-resolver", options={"i18n" = false}, name="aqarmap_api_search_slug_resolver")
  24.      *
  25.      * @Rest\View(serializerGroups = {"SlugResolver"})
  26.      *
  27.      * @Rest\QueryParam(name="section", default=null, description="Section slug")
  28.      * @Rest\QueryParam(name="propertyType", default=null, description="Property Type slug")
  29.      * @Rest\QueryParam(name="locations", default=null, description="Locations slugs. As array.")
  30.      *
  31.      * @Cache(expires="+7 days", maxage="+7 days", smaxage="+7 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  32.      */
  33.     public function resolveSearchPageSlugs(Request $requestSectionRepository $sectionRepoPropertyTypeRepository $propertyTypeRepoLocationRepository $locationRepo): array
  34.     {
  35.         $locationsSlugs = (array) $request->query->get('locations', []);
  36.         $data = [
  37.             'section' => $sectionRepo->findOneBy(['slug' => $request->query->get('section')]),
  38.             'propertyType' => $propertyTypeRepo->findOneBy(['slug' => $request->query->get('propertyType')]),
  39.             'locations' => [],
  40.         ];
  41.         if (!empty($locationsSlugs) && $this->validLocationSlugArray($locationsSlugs)) {
  42.             $data['locations'] = $locationRepo->getLocationsBySlugs($locationsSlugs);
  43.         }
  44.         return $data;
  45.     }
  46.     /**
  47.      * Legazy.
  48.      *
  49.      * @Rest\Get("/api/v2/search/properties-chips-legazy", options={"i18n" = false}, name="aqarmap_api_properties_chips_legazy")
  50.      *
  51.      * @Rest\View()
  52.      *
  53.      * @ParamConverter("location", options={"mapping": {"location": "id"}}, isOptional="true", converter="querystring")
  54.      * @ParamConverter("propertyType", options={"mapping": {"propertyType": "id"}}, isOptional="true", converter="querystring")
  55.      * @ParamConverter("section", options={"mapping": {"section": "id"}}, isOptional="true", converter="querystring")
  56.      *
  57.      * @Cache(expires="+1 days", maxage="+1 days", smaxage="+1 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  58.      */
  59.     public function getPropertiesChipsLegazy(Request $request, ?Location $location, ?PropertyType $propertyType, ?Section $sectionMenuBuilderService $menuBuilderService)
  60.     {
  61.         $locale $request->getLocale();
  62.         $data = [];
  63.         if ($section && $propertyType) {
  64.             $criteria = [
  65.                 'location' => $location,
  66.                 'propertyType' => $propertyType,
  67.                 'section' => $section,
  68.                 'locale' => $locale,
  69.             ];
  70.             $data $menuBuilderService->getSearchResultPropertyChips($criteria);
  71.         }
  72.         return $data;
  73.     }
  74.     /**
  75.      * @Rest\Get("/api/v2/search/properties-chips", options={"i18n" = false}, name="aqarmap_api_properties_chips")
  76.      *
  77.      * @Rest\View()
  78.      *
  79.      * @Cache(expires="+1 days", maxage="+1 days", smaxage="+1 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  80.      */
  81.     public function getPropertiesChips(Request $requestListingManager $listingManagerPropertyTypeRepository $propertyTypeRepository)
  82.     {
  83.         $result = [];
  84.         $types $listingManager->getPropertiesChipsElasticResponse($request);
  85.         $typesArray = [];
  86.         foreach ($types as $id => $doc_count) {
  87.             $typesArray[$id] = $doc_count;
  88.         }
  89.         arsort($typesArray);
  90.         $propertyTypes $propertyTypeRepository->findPropertyTypes(array_keys($typesArray));
  91.         foreach ($propertyTypes as $propertyType) {
  92.             $id $propertyType->getId();
  93.             $result[] = [
  94.                 'id' => $id,
  95.                 'title' => $propertyType->getTitle(),
  96.                 'slug' => $propertyType->getSlug(),
  97.                 'searchResultCount' => $typesArray[$id] ?? 0,
  98.                 'level' => $propertyType->getLevel(),
  99.             ];
  100.         }
  101.         return $result;
  102.     }
  103.     /**
  104.      * @Rest\Get("/api/v2/search/{section}/{propertyType}/{location}/page-metadata", options={"i18n"=false}, name="aqarmap_api_search_page_metadata")
  105.      *
  106.      * @Rest\View(serializerGroups={"Default"})
  107.      */
  108.     public function getSeoData(
  109.         Section $section,
  110.         PropertyType $propertyType,
  111.         Location $location,
  112.         CustomParagraphRepository $customParagraphRepository,
  113.         SEOListingSearchService $seoListingSearchService
  114.     ) {
  115.         $data['customParagraph'] = $customParagraphRepository->get([
  116.             'location' => $location->getId(),
  117.             'propertyType' => $propertyType->getId(),
  118.             'section' => $section->getId(),
  119.             'place' => CustomParagraphPlaceTypes::SEARCH_RESULTS,
  120.         ])->getQuery()->setMaxResults(1)->getOneOrNullResult();
  121.         $data['longTail'] = $seoListingSearchService->retrieveLongTail($location$section$propertyType);
  122.         return [
  123.             'data' => $data,
  124.         ];
  125.     }
  126.     /**
  127.      * @Rest\Get("/api/v2/custom-paragraph", options={"i18n"=false}, name="aqarmap_api_custom_paragraph")
  128.      *
  129.      * @ParamConverter("location", options={"mapping": {"location": "id"}}, isOptional="true", converter="querystring")
  130.      * @ParamConverter("propertyType", options={"mapping": {"propertyType": "id"}}, isOptional="false", converter="querystring")
  131.      * @ParamConverter("section", options={"mapping": {"section": "id"}}, isOptional="true", converter="querystring")
  132.      *
  133.      * @Rest\View(serializerGroups={"Default"})
  134.      */
  135.     public function getCustomParagraph(
  136.         Request $request,
  137.         ?Location $location,
  138.         Section $section,
  139.         ?PropertyType $propertyType,
  140.         CustomParagraphRepository $customParagraphRepository
  141.     ) {
  142.         return $customParagraphRepository->get([
  143.             'location' => $location $location->getId() : null,
  144.             'propertyType' => $propertyType $propertyType->getId() : null,
  145.             'section' => $section->getId(),
  146.             'place' => $request->query->get('place') ?: CustomParagraphPlaceTypes::SEARCH_RESULTS,
  147.         ])->getQuery()->setMaxResults(1)->getOneOrNullResult();
  148.     }
  149.     /**
  150.      * @Rest\Get("/api/v2/location/{location}/listing", options={"i18n" = false}, name="aqarmap_api_location_listing_v2")
  151.      *
  152.      * @Rest\QueryParam(name="id", default=null, description="Location id")
  153.      *
  154.      * @Rest\View(serializerGroups = {"locationListingV2"})
  155.      */
  156.     public function getLocationListing(Location $location)
  157.     {
  158.         return $location;
  159.     }
  160.     private function validLocationSlugArray($array): bool
  161.     {
  162.         return \count($array) > && array_reduce($array, fn ($carry$item) => $carry && \is_string($item) && '' !== $itemtrue);
  163.     }
  164.     /**
  165.      * @Rest\Get("/api/v4/search/ssr-data", options={"i18n" = false, "expose" = false}, name="aqarmap_search_results_ssr_data")
  166.      *
  167.      * @Rest\QueryParam(name="section", default=null, description="Section slug")
  168.      * @Rest\QueryParam(name="propertyType", default=null, description="Property Type slug")
  169.      * @Rest\QueryParam(name="locations", default=null, description="Array of Locations slugs.")
  170.      *
  171.      * @Rest\View(serializerGroups = {"SlugResolver"})
  172.      */
  173.     public function singlePageSSRData(Request $requestSectionRepository $sectionRepositoryPropertyTypeRepository $propertyTypeRepositoryLocationRepository $locationRepositoryBuilderInterface $searchDescriptionBuilder): array
  174.     {
  175.         $sectionSlug $request->get('section');
  176.         if (!$sectionSlug) {
  177.             throw new \InvalidArgumentException('Section is required');
  178.         }
  179.         $section $sectionRepository->findOneBy(['slug' => $sectionSlug]);
  180.         $propertyTypeSlug $request->get('propertyType');
  181.         if (!$propertyTypeSlug) {
  182.             throw new \InvalidArgumentException('propertyType is required');
  183.         }
  184.         $propertyType $propertyTypeRepository->findOneBy(['slug' => $propertyTypeSlug]);
  185.         $locationsSlugs = (array) $request->get('locations', []);
  186.         if (!$locationsSlugs) {
  187.             throw new \InvalidArgumentException('locations is required');
  188.         }
  189.         $locations $locationRepository->getLocationsBySlugs($locationsSlugs);
  190.         // Page Description
  191.         $searchDescriptionBuilder
  192.             ->setSection($section)
  193.             ->setPropertyType($propertyType)
  194.             ->setLocation(reset($locations))
  195.         ;
  196.         return [
  197.             'section' => $section,
  198.             'propertyType' => $propertyType,
  199.             'locations' => $locations,
  200.             'pageTitle' => '',
  201.             'metaDescription' => $searchDescriptionBuilder->format()->get(),
  202.             'metaKeywords' => '',
  203.             'canonicalUrl' => '',
  204.             'breadcrumbs' => [
  205.                 [
  206.                     'title' => '',
  207.                     'url' => '',
  208.                 ],
  209.             ],
  210.             'propertyTypesChips' => [
  211.                 [
  212.                     'title' => '',
  213.                     'url' => '',
  214.                 ],
  215.             ],
  216.         ];
  217.     }
  218. }