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

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\SearchBundle\Controller\Api;
  3. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  4. use Aqarmap\Bundle\ListingBundle\Entity\PropertyType;
  5. use Aqarmap\Bundle\ListingBundle\Entity\Section;
  6. use Aqarmap\Bundle\ListingBundle\Repository\LocationRepository;
  7. use Aqarmap\Bundle\ListingBundle\Repository\PropertyTypeRepository;
  8. use Aqarmap\Bundle\ListingBundle\Repository\SectionRepository;
  9. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  10. use Aqarmap\Bundle\MainBundle\Constant\CustomParagraphPlaceTypes;
  11. use Aqarmap\Bundle\MainBundle\Controller\Api\BaseController;
  12. use Aqarmap\Bundle\MainBundle\Repository\CustomParagraphRepository;
  13. use Aqarmap\Bundle\MainBundle\Service\MenuBuilderService;
  14. use Aqarmap\Bundle\SearchBundle\Services\SEOListingSearchService;
  15. use FOS\RestBundle\Controller\Annotations as Rest;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class SearchResultsController extends BaseController
  20. {
  21.     /**
  22.      * @Rest\Get("/api/v2/search/slug-resolver", options={"i18n" = false}, name="aqarmap_api_search_slug_resolver")
  23.      *
  24.      * @Rest\View(serializerGroups = {"SlugResolver"})
  25.      *
  26.      * @Rest\QueryParam(name="section", default=null, description="Section slug")
  27.      * @Rest\QueryParam(name="propertyType", default=null, description="Property Type slug")
  28.      * @Rest\QueryParam(name="locations", default=null, description="Locations slugs. As array.")
  29.      *
  30.      * @Cache(expires="+7 days", maxage="+7 days", smaxage="+7 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  31.      */
  32.     public function resolveSearchPageSlugs(Request $requestSectionRepository $sectionRepoPropertyTypeRepository $propertyTypeRepoLocationRepository $locationRepo): array
  33.     {
  34.         $locationsSlugs $request->query->get('locations');
  35.         $data = [
  36.             'section' => $sectionRepo->findOneBy(['slug' => $request->query->get('section')]),
  37.             'propertyType' => $propertyTypeRepo->findOneBy(['slug' => $request->query->get('propertyType')]),
  38.             'locations' => [],
  39.         ];
  40.         if (isset($locationsSlugs) && $this->validLocationSlugArray($locationsSlugs)) {
  41.             $data['locations'] = $locationRepo->getLocationsBySlugs($locationsSlugs);
  42.         }
  43.         return $data;
  44.     }
  45.     /**
  46.      * Legazy.
  47.      *
  48.      * @Rest\Get("/api/v2/search/properties-chips-legazy", options={"i18n" = false}, name="aqarmap_api_properties_chips_legazy")
  49.      *
  50.      * @Rest\View()
  51.      *
  52.      * @ParamConverter("location", options={"mapping": {"location": "id"}}, isOptional="true", converter="querystring")
  53.      * @ParamConverter("propertyType", options={"mapping": {"propertyType": "id"}}, isOptional="true", converter="querystring")
  54.      * @ParamConverter("section", options={"mapping": {"section": "id"}}, isOptional="true", converter="querystring")
  55.      *
  56.      * @Cache(expires="+1 days", maxage="+1 days", smaxage="+1 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  57.      */
  58.     public function getPropertiesChipsLegazy(Request $request, ?Location $location, ?PropertyType $propertyType, ?Section $sectionMenuBuilderService $menuBuilderService)
  59.     {
  60.         $locale $request->getLocale();
  61.         $data = [];
  62.         if ($section && $propertyType) {
  63.             $criteria = [
  64.                 'location' => $location,
  65.                 'propertyType' => $propertyType,
  66.                 'section' => $section,
  67.                 'locale' => $locale,
  68.             ];
  69.             $data $menuBuilderService->getSearchResultPropertyChips($criteria);
  70.         }
  71.         return $data;
  72.     }
  73.     /**
  74.      * @Rest\Get("/api/v2/search/properties-chips", options={"i18n" = false}, name="aqarmap_api_properties_chips")
  75.      *
  76.      * @Rest\View()
  77.      *
  78.      * @Cache(expires="+1 days", maxage="+1 days", smaxage="+1 days", public=true, vary={"Accept-Language", "X-Accept-Version", "Accept"})
  79.      */
  80.     public function getPropertiesChips(Request $request, ?Location $location, ?PropertyType $propertyType, ?Section $sectionListingManager $listingManagerPropertyTypeRepository $propertyTypeRepository)
  81.     {
  82.         $result = [];
  83.         $types $listingManager->getPropertiesChipsElasticResponse($request);
  84.         $typesArray = [];
  85.         foreach ($types as $id => $doc_count) {
  86.             $typesArray[$id] = $doc_count;
  87.         }
  88.         arsort($typesArray);
  89.         $propertyTypes $propertyTypeRepository->findPropertyTypes(array_keys($typesArray));
  90.         foreach ($propertyTypes as $propertyType) {
  91.             $id $propertyType->getId();
  92.             $result[] = [
  93.                 'id' => $id,
  94.                 'title' => $propertyType->getTitle(),
  95.                 'slug' => $propertyType->getSlug(),
  96.                 'searchResultCount' => $typesArray[$id] ?? 0,
  97.                 'level' => $propertyType->getLevel(),
  98.             ];
  99.         }
  100.         return $result;
  101.     }
  102.     /**
  103.      * @Rest\Get("/api/v2/search/{section}/{propertyType}/{location}/page-metadata", options={"i18n"=false}, name="aqarmap_api_search_page_metadata")
  104.      *
  105.      * @Rest\View(serializerGroups={"Default"})
  106.      */
  107.     public function getSeoData(
  108.         Section $section,
  109.         PropertyType $propertyType,
  110.         Location $location,
  111.         CustomParagraphRepository $customParagraphRepository,
  112.         SEOListingSearchService $seoListingSearchService
  113.     ) {
  114.         $data['customParagraph'] = $customParagraphRepository->get([
  115.             'location' => $location->getId(),
  116.             'propertyType' => $propertyType->getId(),
  117.             'section' => $section->getId(),
  118.             'place' => CustomParagraphPlaceTypes::SEARCH_RESULTS,
  119.         ])->getQuery()->setMaxResults(1)->getOneOrNullResult();
  120.         $data['longTail'] = $seoListingSearchService->retrieveLongTail($location$section$propertyType);
  121.         return [
  122.             'data' => $data,
  123.         ];
  124.     }
  125.     /**
  126.      * @Rest\Get("/api/v2/custom-paragraph", options={"i18n"=false}, name="aqarmap_api_custom_paragraph")
  127.      *
  128.      * @ParamConverter("location", options={"mapping": {"location": "id"}}, isOptional="true", converter="querystring")
  129.      * @ParamConverter("propertyType", options={"mapping": {"propertyType": "id"}}, isOptional="false", converter="querystring")
  130.      * @ParamConverter("section", options={"mapping": {"section": "id"}}, isOptional="true", converter="querystring")
  131.      *
  132.      * @Rest\View(serializerGroups={"Default"})
  133.      */
  134.     public function getCustomParagraph(
  135.         Request $request,
  136.         ?Location $location,
  137.         Section $section,
  138.         ?PropertyType $propertyType,
  139.         CustomParagraphRepository $customParagraphRepository
  140.     ) {
  141.         return $customParagraphRepository->get([
  142.             'location' => $location $location->getId() : null,
  143.             'propertyType' => $propertyType $propertyType->getId() : null,
  144.             'section' => $section->getId(),
  145.             'place' => $request->query->get('place') ?: CustomParagraphPlaceTypes::SEARCH_RESULTS,
  146.         ])->getQuery()->setMaxResults(1)->getOneOrNullResult();
  147.     }
  148.     /**
  149.      * @Rest\Get("/api/v2/location/{location}/listing", options={"i18n" = false}, name="aqarmap_api_location_listing_v2")
  150.      *
  151.      * @Rest\QueryParam(name="id", default=null, description="Location id")
  152.      *
  153.      * @Rest\View(serializerGroups = {"locationListingV2"})
  154.      */
  155.     public function getLocationListing(Location $location)
  156.     {
  157.         return $location;
  158.     }
  159.     private function validLocationSlugArray($array): bool
  160.     {
  161.         return \count($array) > && array_reduce($array, fn ($carry$item) => $carry && \is_string($item) && '' !== $itemtrue);
  162.     }
  163. }