-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathStepAggregator.php
More file actions
196 lines (163 loc) · 4.44 KB
/
Copy pathStepAggregator.php
File metadata and controls
196 lines (163 loc) · 4.44 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Exception;
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
use Ddeboer\DataImport\Reader;
use Ddeboer\DataImport\Result;
use Ddeboer\DataImport\Step;
use Ddeboer\DataImport\Step\PriorityStep;
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Writer;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
/**
* A mediator between a reader and one or more writers and converters
*
* @author David de Boer <david@ddeboer.nl>
*/
class StepAggregator implements Workflow, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var Reader
*/
private $reader;
/**
* Identifier for the Import/Export
*
* @var string|null
*/
private $name = null;
/**
* @var boolean
*/
private $skipItemOnFailure = false;
/**
* @var \SplPriorityQueue
*/
private $steps;
/**
* @var Writer[]
*/
private $writers = [];
/**
* @var boolean
*/
protected $shouldStop = false;
/**
* @param Reader $reader
* @param string $name
*/
public function __construct(Reader $reader, $name = null)
{
$this->name = $name;
$this->reader = $reader;
// Defaults
$this->logger = new NullLogger();
$this->steps = new \SplPriorityQueue();
}
/**
* Add a step to the current workflow
*
* @param Step $step
* @param integer|null $priority
*
* @return $this
*/
public function addStep(Step $step, $priority = null)
{
$priority = null === $priority && $step instanceof PriorityStep ? $step->getPriority() : $priority;
$priority = null === $priority ? 0 : $priority;
$this->steps->insert($step, $priority);
return $this;
}
/**
* Add a new writer to the current workflow
*
* @param Writer $writer
*
* @return $this
*/
public function addWriter(Writer $writer)
{
array_push($this->writers, $writer);
return $this;
}
/**
* {@inheritdoc}
*/
public function process()
{
$count = 0;
$exceptions = new \SplObjectStorage();
$startTime = new \DateTime;
foreach ($this->writers as $writer) {
$writer->prepare();
}
if (is_callable('pcntl_signal')) {
pcntl_signal(SIGTERM, array($this, 'stop'));
pcntl_signal(SIGINT, array($this, 'stop'));
}
// Read all items
foreach ($this->reader as $index => $item) {
if (is_callable('pcntl_signal_dispatch')) {
pcntl_signal_dispatch();
}
if ($this->shouldStop) {
break;
}
try {
foreach (clone $this->steps as $step) {
if (false === $step->process($item)) {
continue 2;
}
}
if (!is_array($item) && !($item instanceof \ArrayAccess && $item instanceof \Traversable)) {
throw new UnexpectedTypeException($item, 'array');
}
foreach ($this->writers as $writer) {
$writer->writeItem($item);
}
} catch(Exception $e) {
if (!$this->skipItemOnFailure) {
throw $e;
}
$exceptions->attach($e, $index);
$this->logger->error($e->getMessage());
}
$count++;
}
foreach ($this->writers as $writer) {
$writer->finish();
}
return new Result($this->name, $startTime, new \DateTime, $count, $exceptions);
}
/**
* Stops processing and force return Result from process() function
*/
public function stop()
{
$this->shouldStop = true;
}
/**
* Sets the value which determines whether the item should be skipped when error occures
*
* @param boolean $skipItemOnFailure When true skip current item on process exception and log the error
*
* @return $this
*/
public function setSkipItemOnFailure($skipItemOnFailure)
{
$this->skipItemOnFailure = $skipItemOnFailure;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
}