-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAbstractIterableOutputTask.php
More file actions
123 lines (107 loc) · 3.26 KB
/
Copy pathAbstractIterableOutputTask.php
File metadata and controls
123 lines (107 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (C) 2017-2021 Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Task;
use CleverAge\ProcessBundle\Model\IterableTaskInterface;
use CleverAge\ProcessBundle\Model\ProcessState;
use CleverAge\ProcessBundle\Model\AbstractConfigurableTask;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Base class to handle output iterations
*
* Extends this class and implement `initializeIterator()` to provide data that will be iterated upon.
*
* ##### Task reference
*
* * **Abstract task**
* * **Iterable task**
*
* @example "Resources/examples/task/abstract_iterable_output_task.php" A simple implementation
*
* @author Valentin Clavreul <vclavreul@clever-age.com>
* @author Vincent Chalnot <vchalnot@clever-age.com>
*/
abstract class AbstractIterableOutputTask extends AbstractConfigurableTask implements IterableTaskInterface
{
/** @var \Iterator */
protected $iterator;
/**
* {@inheritDoc}
*
* @internal
*/
public function execute(ProcessState $state)
{
$this->handleIteratorFromInput($state);
$state->addErrorContextValue('iterator_key', $this->iterator->key());
if ($this->iterator->valid()) {
$state->setOutput($this->iterator->current());
} else {
$state->setSkipped(true);
$this->iterator = null;
}
}
/**
* {@inheritDoc}
*
* @internal
*/
public function next(ProcessState $state)
{
if (!$this->iterator) {
return false;
}
$this->iterator->next();
$state->removeErrorContext('iterator_key');
if (!$this->iterator->valid()) {
// Reset the iterator to allow the following iteration
$this->iterator = null;
return false;
}
return true;
}
/**
* Create or recreate an iterator from input
*
* @param ProcessState $state
*/
protected function handleIteratorFromInput(ProcessState $state)
{
if ($this->iterator instanceof \Iterator) {
if ($this->iterator->valid()) {
return; // No action needed, execution is in progress
}
// Cleanup invalid iterator => prepare for new iteration cycle
$this->iterator = null;
}
// This should never be reached
if (null !== $this->iterator) {
throw new \UnexpectedValueException(
"At this point iterator should have been null, maybe it's a wrong type..."
);
}
$this->iterator = $this->initializeIterator($state);
}
/**
* Allow to not implement this method, not required by most tasks, removing inheritance would break back-compat
*
* @inheritDoc
*/
protected function configureOptions(OptionsResolver $resolver)
{
}
/**
* @param ProcessState $state
*
* @return \Iterator
*/
abstract protected function initializeIterator(ProcessState $state): \Iterator;
}