Skip to content

Commit

Permalink
#4 Fix $iterator on DoctrineReaderTask that doesn't work with iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
njoubert-cleverage committed Dec 11, 2024
1 parent 9c8feb9 commit 3eb7b53
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Task/EntityManager/DoctrineReaderTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class DoctrineReaderTask extends AbstractDoctrineQueryTask implements IterableTaskInterface
{
protected ?iterable $iterator = null;
protected ?\IteratorIterator $iterator = null;

public function __construct(
protected LoggerInterface $logger,
Expand All @@ -41,18 +41,18 @@ public function __construct(
*/
public function next(ProcessState $state): bool
{
if (!is_iterable($this->iterator)) {
if (!$this->iterator instanceof \IteratorIterator) {
return false;
}
next($this->iterator);
$this->iterator->next();

return false !== current($this->iterator);
return $this->iterator->valid();
}

public function execute(ProcessState $state): void
{
$options = $this->getOptions($state);
if (!is_iterable($this->iterator)) {
if (!$this->iterator instanceof \IteratorIterator) {
/** @var class-string $class */
$class = $options['class_name'];
$entityManager = $this->doctrine->getManagerForClass($class);
Expand All @@ -62,8 +62,7 @@ public function execute(ProcessState $state): void
$repository = $entityManager->getRepository($class);
$this->initIterator($repository, $options);
}

$result = current($this->iterator);
$result = $this->iterator->current();

// Handle empty results
if (false === $result) {
Expand All @@ -77,7 +76,7 @@ public function execute(ProcessState $state): void
return;
}

$state->setOutput(reset($result));
$state->setOutput($result);
}

protected function initIterator(EntityRepository $repository, array $options): void
Expand All @@ -90,8 +89,7 @@ protected function initIterator(EntityRepository $repository, array $options): v
$options['offset']
);

$this->iterator = $qb->getQuery()
->toIterable();
next($this->iterator); // Move to first element
$this->iterator = new \IteratorIterator($qb->getQuery()->toIterable());
$this->iterator->rewind();
}
}

0 comments on commit 3eb7b53

Please sign in to comment.