for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace SymfonyBundles\Pattern\BatchProcessing;
/**
* Class BatchProcessing.
*
* @author Dmitry Khaperets <[email protected]>
*/
class BatchProcessing implements BatchProcessingInterface
{
* @var int
protected $count = 0;
* @var array
protected $elements = [];
* @var iterable
protected $iterator;
protected $listeners = [];
protected $batchSize = 0;
* @param int $batchSize
* @param iterable $iterator
public function __construct(int $batchSize, iterable $iterator)
$this->setIterator($iterator);
$this->setBatchSize($batchSize);
}
* {@inheritdoc}
public function setIterator(iterable $iterator): void
$this->iterator = $iterator;
public function addListener(callable $listener): void
$this->listeners[] = $listener;
public function setBatchSize(int $batchSize): void
$this->batchSize = $batchSize;
public function process(): \Generator
foreach ($this->iterator as $element) {
$this->elements[] = $element;
if (++$this->count >= $this->batchSize) {
yield from $this->callListeners();
if ($this->count > 0) {
* @return \Generator
protected function callListeners(): \Generator
foreach ($this->listeners as $listener) {
yield $listener($this->elements);
$this->count = 0;
$this->elements = [];