Your IP : 216.73.217.13


Current Path : /var/www/magento.test.indacotrentino.com/www/app/code/Amasty/Base/Utils/
Upload File :
Current File : /var/www/magento.test.indacotrentino.com/www/app/code/Amasty/Base/Utils/BatchLoader.php

<?php

declare(strict_types=1);

/**
 * @author Amasty Team
 * @copyright Copyright (c) Amasty (https://www.amasty.com)
 * @package Magento 2 Base Package
 */

namespace Amasty\Base\Utils;

use Magento\Framework\Data\Collection;

class BatchLoader
{
    public const BATCH_SIZE = 500;

    public function load(Collection $collection, ?int $batchSize = self::BATCH_SIZE): \Generator
    {
        $currentPage = 1;
        $collection->setPageSize($batchSize);
        $collection->setCurPage($currentPage);
        $totalPagesCount = $collection->getLastPageNumber();

        while ($currentPage <= $totalPagesCount) {
            $collection->clear();
            $collection->setCurPage($currentPage);

            foreach ($collection as $item) {
                yield $item;
            }

            $currentPage++;
        }
    }
}