vendor/elements/demi-bundle/src/Elements/Demi/Deskline/Accommodation/Search/Service/Vacancies/Live.php line 165

Open in your IDE?
  1. <?php
  2. /**
  3.  * Elements DeMI
  4.  *
  5.  * This source file is available under the elements DeMI license version 1
  6.  *
  7.  *  @copyright  Copyright (c) elements.at New Media Solutions GmbH (https://www.elements.at/)
  8.  *  @license    elements DeMI Lizenz Version 1 (https://www.elements.at/de/demi-lizenz)
  9.  */
  10. namespace Elements\Demi\Deskline\Accommodation\Search\Service\Vacancies;
  11. use Elements\Demi\Accommodation\Search\Parameter;
  12. use Elements\Demi\Accommodation\Search\Service\Vacancies\AbstractAdapter;
  13. use Elements\Demi\Deskline\SearchParameter\AccommodationInterface;
  14. use Elements\Demi\Deskline\SearchResult\Accommodation\Entry;
  15. use Elements\Demi\Deskline\Service\Search\Accommodation;
  16. use Elements\Demi\Model\AccommodationServiceProvider;
  17. use Elements\Demi\Vacancies\Dependency\VacancyDependencyContainer;
  18. class Live extends AbstractAdapter
  19. {
  20.     protected ?\Elements\Demi\Deskline\SearchResult\Accommodation $desklineResult null;
  21.     protected ?int $count null;
  22.     protected string $adapterCachePostFix 'Live';
  23.     protected array $sortOrderMapping = [
  24.         Parameter::DEMI_SORT_ORDER_PRICE_ASC => Parameter::SORT_ORDER_PRICE_ASC,
  25.         Parameter::DEMI_SORT_ORDER_PRICE_DESC => Parameter::SORT_ORDER_PRICE_DESC,
  26.         Parameter::DEMI_SORT_ORDER_RANDOM => Parameter::SORT_ORDER_RANDOM,
  27.         Parameter::DEMI_SORT_ORDER_STARS_ASC => Parameter::SORT_ORDER_STARS_ASC,
  28.         Parameter::DEMI_SORT_ORDER_BOOKABLE_ASC => Parameter::SORT_ORDER_BOOKABLE_ASC,
  29.         Parameter::DEMI_SORT_ORDER_RATING_ASC => Parameter::SORT_ORDER_RATING_ASC,
  30.         Parameter::DEMI_SORT_ORDER_RATINGAVERAGE_ASC => Parameter::SORT_ORDER_RATING_ASC,
  31.         Parameter::DEMI_SORT_ORDER_RATINGAVERAGE_DESC => Parameter::SORT_ORDER_RATING_DESC,
  32.         Parameter::DEMI_SORT_ORDER_CONTENTSCORE => Parameter::SORT_ORDER_BOOKABLE_ASC,
  33.     ];
  34.     public function __construct(VacancyDependencyContainer $dc)
  35.     {
  36.         parent::__construct($dc);
  37.     }
  38.     public function getAdapterCachePostFix(): ?string
  39.     {
  40.         return $this->adapterCachePostFix;
  41.     }
  42.     public function mapDemiSortOrder(mixed $sortOrder): ?string
  43.     {
  44.         if (is_array($sortOrder)) {
  45.             $sortOrder current($sortOrder);
  46.         }
  47.         return $this->sortOrderMapping[$sortOrder] ?? $sortOrder;
  48.     }
  49.     public function doRequest(): void
  50.     {
  51.         $searchParams $this->getSearchParameter();
  52.         $sortOrder $searchParams->getOrderKey();
  53.         $searchType $searchParams->getSearchType();
  54.         $ids $searchParams->getAccoIds();
  55.         $fids $searchParams->getAccoFIds();
  56.         if ($searchParams->getTopOnly() && (empty($fids) && empty($ids))) { //needed for getById for detail. otherwise toponly would select more Accommodations even in detail page
  57.             $topIds $this->getTopIds();
  58.             $searchParams->setAccoFIds($topIds);
  59.         }
  60.         $sortOrder $this->mapDemiSortOrder($sortOrder);
  61.         if (!$sortOrder || $sortOrder == 'custom') {
  62.             $sortOrder Parameter::SORT_ORDER_RANDOM;
  63.         }
  64.         // if searchType ist not set or supported -> fallback to standard
  65.         if (!in_array($searchType, [Parameter::SEARCH_TYPE_STANDARDParameter::SEARCH_TYPE_EACH_PRODUCT_SEPARATED])) {
  66.             $searchType Parameter::SEARCH_TYPE_STANDARD;
  67.         }
  68.         $responseType $searchParams->getResponseType();
  69.         //$responseType = \Elements\Demi\Deskline\Constant\SearchParameter\Accommodation::RESPONSE_TYPE_STANDARD;
  70.         /** @var $desklineParams AccommodationInterface */
  71.         $desklineParams $this->convertSearchParams();
  72.         $page $searchParams->getCurrentPage();
  73.         $searchService = new Accommodation($desklineParams$page$searchParams->getCurrentItemCountPerPage(), $sortOrder$searchType$responseType);
  74.         $time = (round(microtime(true) * 1000));
  75.         $this->desklineResult $searchService->getSearchResult();
  76.         $this->setRequestTime((round(microtime(true) * 1000)) - $time);
  77.         $singleAccoLiveSearch is_array($this->searchParameter->getAccoIds()) && count($this->searchParameter->getAccoIds()) == && !$this->searchParameter->getIsCorridor();
  78.         //filter locally, Deskline does not support search for multiple product ids
  79.         if ((!($singleAccoLiveSearch && in_array('productIds'$this->searchParameter->getIgnoreFiltersInAccoDetailSearch())) && $searchParams->getProductIds() && count($searchParams->getProductIds())) && $this->desklineResult->getEntries()) {
  80.             /**
  81.              * @var $accommodationEntry Entry
  82.              */
  83.             $accommodationEntry $this->desklineResult->getEntries()[0];
  84.             $filteredProducts = [];
  85.             foreach ($accommodationEntry->getProducts() as $product) {
  86.                 if ($product->getProduct()) {
  87.                     $pimcoreId $product->getProduct()->getId();
  88.                     if (in_array($pimcoreId$searchParams->getProductIds())) {
  89.                         $filteredProducts[] = $product;
  90.                     }
  91.                 }
  92.             }
  93.             $accommodationEntry->setProducts($filteredProducts);
  94.             $this->desklineResult->setTotalAmount(count($filteredProducts));
  95.         }
  96.         //can't filter free cancellation in by request, let's do it now
  97.         if (!($singleAccoLiveSearch && in_array('freeCancellation'$this->searchParameter->getIgnoreFiltersInAccoDetailSearch())) && $this->searchParameter->getFreeCancellation() && $this->desklineResult->getEntries()) {
  98.             /**
  99.              * @var $accommodationEntry Entry
  100.              */
  101.             $accommodationEntry $this->desklineResult->getEntries()[0];
  102.             $filteredProducts = [];
  103.             foreach ($accommodationEntry->getProducts() as $product) {
  104.                 if ($product->getProduct() && $product->getHasFreeCancellation()) {
  105.                     $filteredProducts[] = $product;
  106.                 }
  107.             }
  108.             $accommodationEntry->setProducts($filteredProducts);
  109.             $this->desklineResult->setTotalAmount(count($filteredProducts));
  110.         }
  111.         $this->count $searchService->count();
  112.     }
  113.     public function getTopIds(?int $count null): array
  114.     {
  115.         $limit '';
  116.         if (!$count) {
  117.             $limit ' LIMIT ' $count;
  118.         }
  119.         $serviceProviderDummy = new AccommodationServiceProvider();
  120.         $sql 'select fid from ' $this->getDc()->getUsedPimcoreDb() . '.object_' $serviceProviderDummy->getClassId() . ' where isTop = 1 ';
  121.         $sql .= $limit;
  122.         $result $this->getDc()->getDb()?->fetchAll($sql);
  123.         $ids = [];
  124.         foreach ($result as $keyvalue) {
  125.             $ids[] = $keyvalue['fid'];
  126.         }
  127.         return $ids;
  128.     }
  129.     public function searchVacancies(bool $force false): array
  130.     {
  131.         if ($force || $this->desklineResult === null) {
  132.             $this->doRequest();
  133.         }
  134.         return $this->desklineResult->getEntries();
  135.     }
  136.     public function count(): ?int
  137.     {
  138.         if ($this->desklineResult === null) {
  139.             $this->doRequest();
  140.         }
  141.         return $this->count;
  142.     }
  143.     private function convertSearchParams(): array
  144.     {
  145.         return $this->getSearchParameter()->convertToDesklineParams();
  146.     }
  147.     public function getResultBuilder(): ResultBuilder
  148.     {
  149.         $searchParams $this->getSearchParameter();
  150.         return new ResultBuilder($searchParams$this->getDc());
  151.     }
  152. }