|
1 | 1 | <?php
|
| 2 | +declare(strict_types=1); |
2 | 3 | /*
|
3 | 4 | * This file is part of the CleverAge/EAVProcessBundle package.
|
4 | 5 | *
|
@@ -34,7 +35,7 @@ class EAVReaderTask extends AbstractEAVQueryTask implements IterableTaskInterfac
|
34 | 35 | /** @var LoggerInterface */
|
35 | 36 | protected $logger;
|
36 | 37 |
|
37 |
| - /** @var \Iterator */ |
| 38 | + /** @var \Iterator|\Countable */ |
38 | 39 | protected $iterator;
|
39 | 40 |
|
40 | 41 | /** @var bool */
|
@@ -82,20 +83,14 @@ public function execute(ProcessState $state): void
|
82 | 83 | }
|
83 | 84 | }
|
84 | 85 |
|
| 86 | + $init = false; |
85 | 87 | 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; |
95 | 90 | }
|
96 | 91 |
|
97 | 92 | // Handle empty results
|
98 |
| - if (0 === $this->iterator->count()) { |
| 93 | + if ($init && !$this->iterator->valid()) { |
99 | 94 | $logContext = $this->getLogContext($state);
|
100 | 95 | $this->logger->log($options['empty_log_level'], 'Empty resultset for query', $logContext);
|
101 | 96 | $state->setSkipped(true);
|
@@ -184,4 +179,20 @@ protected function getLogContext(ProcessState $state): array
|
184 | 179 |
|
185 | 180 | return $logContext;
|
186 | 181 | }
|
| 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 | + } |
187 | 198 | }
|
0 commit comments