diff --git a/src/CoreShop/Component/Pimcore/BatchProcessing/DataObjectBatchListing.php b/src/CoreShop/Component/Pimcore/BatchProcessing/DataObjectBatchListing.php new file mode 100644 index 0000000000..e1e40e12d6 --- /dev/null +++ b/src/CoreShop/Component/Pimcore/BatchProcessing/DataObjectBatchListing.php @@ -0,0 +1,165 @@ +list = $list; + $this->batchSize = $batchSize; + } + + /** + * {@inheritdoc} + */ + public function current() + { + return $this->items[$this->index]; + } + + /** + * {@inheritdoc} + */ + public function next() + { + $this->index++; + + if ($this->index >= $this->batchSize) { + $this->index = 0; + $this->loop++; + + $this->load(); + } + } + + /** + * {@inheritdoc} + */ + public function key() + { + return ($this->index + 1) * ($this->loop + 1); + } + + /** + * {@inheritdoc} + */ + public function valid() + { + return isset($this->items[$this->index]); + } + + /** + * {@inheritdoc} + */ + public function rewind() + { + $this->index = 0; + $this->loop = 0; + $this->currentLoopLoaded = -1; + + $this->load(); + } + + public function count() + { + if (!$this->total) { + $dao = $this->list->getDao(); + + if (!method_exists($dao, 'getTotalCount')) { + throw new \InvalidArgumentException(sprintf('%s listing class does not support count.', + get_class($this->list))); + } + + $this->total = $dao->getTotalCount(); + } + + return $this->total; + } + + /** + * Load all items based on current state. + */ + private function load() + { + if (null === $this->ids) { + $dao = $this->list->getDao(); + + if (!method_exists($dao, 'loadIdList')) { + throw new \InvalidArgumentException(sprintf('%s listing class does not support loadIdList.', + get_class($this->list))); + } + + $this->ids = $dao->loadIdList(); + } + + if ($this->currentLoopLoaded !== $this->loop) { + $items = []; + + for ($i = 0; $i < $this->batchSize; $i++) { + $idOffset = ($this->loop * $this->batchSize) + $i; + + $itemId = $this->ids[$idOffset]; + + $items[$i] = DataObject::getById($itemId); + } + + $this->items = $items; + $this->currentLoopLoaded = $this->loop; + } + } +}