vendor/elements/demi-bundle/src/Elements/Demi/Accommodation/Search/Listing.php line 189

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\Accommodation\Search;
  11. use Elements\Demi\Accommodation\Search\Service\Vacancies\AbstractAdapter;
  12. use Elements\Demi\Accommodation\Search\Service\Vacancies\Local;
  13. use Elements\Demi\Dependency\DependencyContainer;
  14. use Pimcore\Model\Paginator\PaginateListingInterface;
  15. class Listing implements \Countable\IteratorPaginateListingInterface
  16. {
  17.     public const RETURNTYPE_ACCOMMODATIONS 'Accommodations';
  18.     public const RETURNTYPE_PRODUCTS 'Products';
  19.     public const RETURNTYPE_PACKAGES 'Packages';
  20.     protected string $returnType self::RETURNTYPE_ACCOMMODATIONS;
  21.     protected ?ResultSet $resultSet null;
  22.     protected ?Parameter $parameter null;
  23.     protected int $preloadPages 0;
  24.     protected int $pageSize 10;
  25.     protected string $orderKey;
  26.     protected string $order;
  27.     protected int $count 0;
  28.     protected ?int $totalCount null;
  29.     protected ?int $requestTime null;
  30.     protected ?AbstractAdapter $adapter null;
  31.     protected ?DependencyContainer $dc null;
  32.     protected ?array $pagedAccommodations null;
  33.     protected bool $freeCancellationInList false;
  34.     public function __construct(DependencyContainer $dcAbstractAdapter $adapter)
  35.     {
  36.         $this->adapter $adapter;
  37.         $this->dc $dc;
  38.     }
  39.     public function getParameter(): ?Parameter
  40.     {
  41.         return !$this->parameter ? throw new \Exception('Parameter not set.') : $this->parameter;
  42.     }
  43.     public function setParameter(Parameter $parameter): void
  44.     {
  45.         $this->parameter $parameter;
  46.         $this->adapter->setSearchParameter($parameter);
  47.     }
  48.     public function setReturnType(string $returnType): void
  49.     {
  50.         $this->returnType $returnType;
  51.     }
  52.     public function getReturnType(): string
  53.     {
  54.         return $this->returnType;
  55.     }
  56.     /**
  57.      * @param ResultSet\Accommodation[] $pagedAccommodations
  58.      */
  59.     public function setPagedAccommodations($pagedAccommodations)
  60.     {
  61.         $this->pagedAccommodations $pagedAccommodations;
  62.     }
  63.     /**
  64.      * @return ResultSet\Accommodation[]|null
  65.      */
  66.     public function getPagedAccommodations(): ?array
  67.     {
  68.         return $this->pagedAccommodations;
  69.     }
  70.     /**
  71.      * loads the result into the intern resultSet
  72.      */
  73.     public function load()
  74.     {
  75.         $resultSet $this->adapter->searchVacancies();
  76.         if (method_exists($this->adapter'getRequestTime')) {
  77.             $this->requestTime $this->adapter->getRequestTime();
  78.         }
  79.         $this->pagedAccommodations $resultSet->getAccommodationPointer(); //for iterator
  80.         $this->count = (int) $this->adapter->count();
  81.         $this->setTotalCount((int) $this->adapter->getTotalCount());
  82.         $this->resultSet $resultSet;
  83.     }
  84.     /**
  85.      * @return int
  86.      */
  87.     public function count(): int
  88.     {
  89.         $this->checkLoaded();
  90.         return $this->adapter->count();
  91.     }
  92.     /**
  93.      * checks if the resultset is filled
  94.      */
  95.     public function checkLoaded()
  96.     {
  97.         $this->getAdapter()->setSearchParameter($this->getParameter());
  98.         if ($this->resultSet === null) {
  99.             $this->load();
  100.         }
  101.     }
  102.     /**
  103.      * @param int $page
  104.      * @param int $itemCountPerPage
  105.      *
  106.      * @return array
  107.      */
  108.     public function getItems($page$itemCountPerPage)
  109.     {
  110.         $this->checkLoaded();
  111.         if ($this->returnType == self::RETURNTYPE_ACCOMMODATIONS) {
  112.             //$this->pagedAccommodations = $this->resultSet->getAccommodations($page, $itemCountPerPage);
  113.             $this->pagedAccommodations $this->resultSet->getAccommodations();
  114.             return $this->pagedAccommodations;
  115.         }
  116.         if ($this->returnType == self::RETURNTYPE_PRODUCTS) {
  117.             //$this->pagedAccommodations = $this->resultSet->getEachProductSeperated($page, $itemCountPerPage);
  118.             $this->pagedAccommodations $this->resultSet->getEachProductSeperated();
  119.             return $this->pagedAccommodations;
  120.         }
  121.         if ($this->returnType == self::RETURNTYPE_PACKAGES) {
  122.             $this->pagedAccommodations $this->resultSet->getHousePackages($page$itemCountPerPage);
  123.             if ($this->adapter instanceof Local) {
  124.                 $searchParams $this->adapter->getSearchParameter();
  125.                 $orderKeyes = (array) $searchParams->getOrderKey();
  126.                 $sortFactoryKeys $this->adapter->getSortFactoryKeys();
  127.                 foreach ($orderKeyes as $orderKey) {
  128.                     if (in_array($orderKey$sortFactoryKeys)) {
  129.                         $obj $this->adapter->getSortFactoryByKey($orderKey);
  130.                         if (!$obj instanceof \Elements\Demi\Accommodation\Search\Service\Vacancies\Local\SortFactory) {
  131.                             continue;
  132.                         }
  133.                         $obj->sortHousePackages($this->pagedAccommodations);
  134.                     }
  135.                 }
  136.             }
  137.             return $this->pagedAccommodations;
  138.         }
  139.         return [];
  140.     }
  141.     public function getPaginatorAdapter(): Listing
  142.     {
  143.         return $this;
  144.     }
  145.     /**
  146.      * Methods for Iterator
  147.      */
  148.     public function rewind()
  149.     {
  150.         $this->checkLoaded();
  151.         if ($this->pagedAccommodations === null) {
  152.             $this->pagedAccommodations $this->resultSet->getAccommodations();
  153.         }
  154.         reset($this->pagedAccommodations);
  155.     }
  156.     /**
  157.      * @return mixed
  158.      */
  159.     public function current()
  160.     {
  161.         $this->checkLoaded();
  162.         return current($this->pagedAccommodations);
  163.     }
  164.     /**
  165.      * @return mixed
  166.      */
  167.     public function key()
  168.     {
  169.         $this->checkLoaded();
  170.         return key($this->pagedAccommodations);
  171.     }
  172.     /**
  173.      * @return mixed
  174.      */
  175.     public function next()
  176.     {
  177.         $this->checkLoaded();
  178.         return next($this->pagedAccommodations);
  179.     }
  180.     /**
  181.      * @return bool
  182.      */
  183.     public function valid(): bool
  184.     {
  185.         return $this->current() !== false;
  186.     }
  187.     /**
  188.      * dummy
  189.      */
  190.     public function setLimit()
  191.     {
  192.     }
  193.     /**
  194.      * dummy
  195.      */
  196.     public function setOffset()
  197.     {
  198.     }
  199.     /**
  200.      * @param ResultSet $resultSet
  201.      */
  202.     public function setResultSet(ResultSet $resultSet): void
  203.     {
  204.         $this->resultSet $resultSet;
  205.     }
  206.     /**
  207.      * @return ResultSet
  208.      */
  209.     public function getResultSet(): ?ResultSet
  210.     {
  211.         $this->checkLoaded();
  212.         return $this->resultSet;
  213.     }
  214.     public function setOrderKey(string $orderKey): void
  215.     {
  216.         $this->orderKey $orderKey;
  217.     }
  218.     public function setOrder(string $order): void
  219.     {
  220.         $this->order $order;
  221.     }
  222.     public function setPageSize(int $pageSize): void
  223.     {
  224.         $this->pageSize $pageSize;
  225.     }
  226.     public function getPageSize(): int
  227.     {
  228.         return $this->pageSize;
  229.     }
  230.     public function setCount(?int $count): void
  231.     {
  232.         $this->count $count;
  233.     }
  234.     public function getCount(): ?int
  235.     {
  236.         return $this->count;
  237.     }
  238.     public function getAdapter(): ?AbstractAdapter
  239.     {
  240.         return $this->adapter;
  241.     }
  242.     public function setRequestTime(?int $requestTime): void
  243.     {
  244.         $this->requestTime $requestTime;
  245.     }
  246.     public function getRequestTime(): ?int
  247.     {
  248.         return $this->requestTime;
  249.     }
  250.     public function setTotalCount(?int $totalCount): void
  251.     {
  252.         $this->totalCount $totalCount;
  253.     }
  254.     public function getTotalCount(): ?int
  255.     {
  256.         return $this->totalCount;
  257.     }
  258. }