-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataflow.php
More file actions
144 lines (114 loc) · 3.62 KB
/
Copy pathDataflow.php
File metadata and controls
144 lines (114 loc) · 3.62 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
<?php
declare(strict_types=1);
namespace CodeRhapsodie\DataflowBundle\DataflowType\Dataflow;
use CodeRhapsodie\DataflowBundle\DataflowType\Result;
use CodeRhapsodie\DataflowBundle\DataflowType\Writer\WriterInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
class Dataflow implements DataflowInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/** @var callable[] */
private array $steps = [];
/** @var WriterInterface[] */
private array $writers = [];
private ?\Closure $customExceptionIndex = null;
private ?\DateTimeInterface $dateTime = null;
/**
* @var \Closure[]
*/
private array $afterItemProcessors = [];
public function __construct(private iterable $reader, private ?string $name)
{
}
/**
* @return $this
*/
public function addStep(callable $step): self
{
$this->steps[] = $step;
return $this;
}
/**
* @return $this
*/
public function addWriter(WriterInterface $writer): self
{
$this->writers[] = $writer;
return $this;
}
/**
* @return $this
*/
public function setCustomExceptionIndex(callable $callable): self
{
$this->customExceptionIndex = \Closure::fromCallable($callable);
return $this;
}
/**
* @param array<callable> $processors
*/
public function setAfterItemProcessors(array $processors): self
{
$this->afterItemProcessors = array_map(static fn (callable $callable) => \Closure::fromCallable($callable), $processors);
return $this;
}
public function process(): Result
{
$count = 0;
$exceptions = [];
$startTime = new \DateTime();
try {
foreach ($this->writers as $writer) {
$writer->prepare();
}
foreach ($this->reader as $index => $item) {
try {
$this->processItem($item);
} catch (\Throwable $e) {
$exceptionIndex = $index;
try {
if (\is_callable($this->customExceptionIndex)) {
$exceptionIndex = (string) ($this->customExceptionIndex)($item, $index);
}
} catch (\Throwable $e2) {
$exceptions[$index] = $e2;
$this->logException($e2, $index);
}
$exceptions[$exceptionIndex] = $e;
$this->logException($e, $exceptionIndex);
}
++$count;
foreach ($this->afterItemProcessors as $afterItemProcessor) {
$afterItemProcessor($index, $item, $count);
}
}
foreach ($this->writers as $writer) {
$writer->finish();
}
} catch (\Throwable $e) {
$exceptions[] = $e;
$this->logException($e);
}
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions);
}
private function processItem(mixed $item): void
{
foreach ($this->steps as $step) {
$item = \call_user_func($step, $item);
if ($item === false) {
return;
}
}
foreach ($this->writers as $writer) {
$writer->write($item);
}
}
private function logException(\Throwable $e, string|int|null $index = null): void
{
if (!isset($this->logger)) {
return;
}
$this->logger->error($e, ['exception' => $e, 'index' => $index]);
}
}