Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 9b566be

Browse files
Improving EAVReaderTask with proper iterator initialization
1 parent 66a52a8 commit 9b566be

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Task/EAVReaderTask.php

+22-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/*
34
* This file is part of the CleverAge/EAVProcessBundle package.
45
*
@@ -34,7 +35,7 @@ class EAVReaderTask extends AbstractEAVQueryTask implements IterableTaskInterfac
3435
/** @var LoggerInterface */
3536
protected $logger;
3637

37-
/** @var \Iterator */
38+
/** @var \Iterator|\Countable */
3839
protected $iterator;
3940

4041
/** @var bool */
@@ -82,20 +83,14 @@ public function execute(ProcessState $state): void
8283
}
8384
}
8485

86+
$init = false;
8587
if (null === $this->iterator) {
86-
$paginator = $this->getPaginator($state);
87-
$this->iterator = $paginator->getIterator();
88-
89-
// Log the data count
90-
if ($this->getOption($state, 'log_count')) {
91-
$count = \count($paginator);
92-
$logContext = $this->getLogContext($state);
93-
$this->logger->info("{$count} items found with current query", $logContext);
94-
}
88+
$this->initIterator($state);
89+
$init = true;
9590
}
9691

9792
// Handle empty results
98-
if (0 === $this->iterator->count()) {
93+
if ($init && !$this->iterator->valid()) {
9994
$logContext = $this->getLogContext($state);
10095
$this->logger->log($options['empty_log_level'], 'Empty resultset for query', $logContext);
10196
$state->setSkipped(true);
@@ -184,4 +179,20 @@ protected function getLogContext(ProcessState $state): array
184179

185180
return $logContext;
186181
}
182+
183+
/**
184+
* @param ProcessState $state
185+
*/
186+
protected function initIterator(ProcessState $state): void
187+
{
188+
$paginator = $this->getPaginator($state);
189+
$this->iterator = $paginator->getIterator();
190+
191+
// Log the data count
192+
if ($this->getOption($state, 'log_count')) {
193+
$count = $paginator->count();
194+
$logContext = $this->getLogContext($state);
195+
$this->logger->info("{$count} items found with current query", $logContext);
196+
}
197+
}
187198
}

0 commit comments

Comments
 (0)