|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mathielen\DataImport; |
|
4
|
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Result; |
|
6
|
|
|
use Ddeboer\DataImport\Workflow as OriginalWorkflow; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Mathielen\DataImport\Event\ImportProcessEvent; |
|
9
|
|
|
use Mathielen\DataImport\Event\ImportItemEvent; |
|
10
|
|
|
use Ddeboer\DataImport\Exception\ExceptionInterface; |
|
11
|
|
|
|
|
12
|
|
|
class EventDispatchableWorkflow extends OriginalWorkflow |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var EventDispatcherInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $eventDispatcher; |
|
18
|
|
|
|
|
19
|
14 |
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
20
|
|
|
{ |
|
21
|
14 |
|
$this->eventDispatcher = $eventDispatcher; |
|
22
|
14 |
|
} |
|
23
|
|
|
|
|
24
|
14 |
|
public function process() |
|
25
|
|
|
{ |
|
26
|
|
|
//if no eventdispatcher has been set, use original functionality |
|
27
|
14 |
|
if (!$this->eventDispatcher) { |
|
28
|
|
|
return parent::process(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
14 |
|
$count = 0; |
|
32
|
14 |
|
$exceptions = array(); |
|
33
|
14 |
|
$startTime = new \DateTime(); |
|
34
|
14 |
|
$importProcessEvent = new ImportProcessEvent(); |
|
35
|
|
|
|
|
36
|
|
|
//Prepare |
|
37
|
14 |
|
$this->prepare($importProcessEvent); |
|
38
|
|
|
|
|
39
|
|
|
// Read all items |
|
40
|
14 |
|
foreach ($this->reader as $item) { |
|
41
|
|
|
//only create event once for every item-event for performance reasons |
|
42
|
14 |
|
$event = $importProcessEvent->newItemEvent($item); |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
14 |
|
$this->processRead($item, $event); |
|
46
|
|
|
|
|
47
|
|
|
// Apply filters before conversion |
|
48
|
14 |
|
if (!$this->processFilter($item, $event)) { |
|
49
|
2 |
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Convert item |
|
53
|
13 |
|
if (!$convertedItem = $this->processConvert($item, $event)) { |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Apply filters after conversion |
|
58
|
13 |
|
if (!$this->processConvertFilter($convertedItem, $event)) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
13 |
|
$this->processWrite($convertedItem, $item, $event); |
|
63
|
|
|
} catch (ExceptionInterface $e) { |
|
64
|
|
|
if ($this->skipItemOnFailure) { |
|
65
|
|
|
$exceptions[] = $e; |
|
66
|
|
|
$this->logger->error($e->getMessage()); |
|
67
|
|
|
} else { |
|
68
|
|
|
throw $e; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
13 |
|
++$count; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
//Finish |
|
76
|
14 |
|
$this->finish($importProcessEvent); |
|
77
|
|
|
|
|
78
|
14 |
|
return new Result($this->name, $startTime, new \DateTime(), $count, $exceptions); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
14 |
|
private function prepare(ImportProcessEvent $importProcessEvent) |
|
82
|
|
|
{ |
|
83
|
|
|
//Prepare writers |
|
84
|
14 |
|
foreach ($this->writers as $writer) { |
|
85
|
12 |
|
$writer->prepare(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
//Send global event |
|
89
|
14 |
|
$this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_PREPARE, $importProcessEvent); |
|
|
|
|
|
|
90
|
14 |
|
} |
|
91
|
|
|
|
|
92
|
14 |
|
private function finish(ImportProcessEvent $importProcessEvent) |
|
93
|
|
|
{ |
|
94
|
|
|
// Finish writers |
|
95
|
14 |
|
foreach ($this->writers as $writer) { |
|
96
|
12 |
|
$writer->finish(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//Send global event |
|
100
|
14 |
|
$this->eventDispatcher->dispatch(ImportProcessEvent::AFTER_FINISH, $importProcessEvent); |
|
|
|
|
|
|
101
|
14 |
|
} |
|
102
|
|
|
|
|
103
|
14 |
|
protected function processRead(array $item, ImportItemEvent $event) |
|
104
|
|
|
{ |
|
105
|
14 |
|
$this->eventDispatcher->dispatch(ImportItemEvent::AFTER_READ, $event); |
|
|
|
|
|
|
106
|
14 |
|
} |
|
107
|
|
|
|
|
108
|
14 |
View Code Duplication |
protected function processFilter(array $item, ImportItemEvent $event) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
14 |
|
$filterResult = $this->filterItem($item, $this->filters); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
14 |
|
$this->eventDispatcher->dispatch(ImportItemEvent::AFTER_FILTER, $event->setCurrentResult($filterResult)); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
14 |
|
return $filterResult; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
13 |
|
protected function processConvert(array $item, ImportItemEvent $event) |
|
118
|
|
|
{ |
|
119
|
13 |
|
$convertedItem = $this->convertItem($item); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
13 |
|
$this->eventDispatcher->dispatch(ImportItemEvent::AFTER_FILTER, $event->setCurrentResult($convertedItem)); |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
13 |
|
return $convertedItem; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
13 |
View Code Duplication |
protected function processConvertFilter(array $item, ImportItemEvent $event) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
13 |
|
$filterResult = $this->filterItem($item, $this->afterConversionFilters); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
13 |
|
$this->eventDispatcher->dispatch(ImportItemEvent::AFTER_CONVERSIONFILTER, $event->setCurrentResult($filterResult)); |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
13 |
|
return $filterResult; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
13 |
|
protected function processWrite(array $convertedItem, array $item, ImportItemEvent $event) |
|
136
|
|
|
{ |
|
137
|
13 |
|
foreach ($this->writers as $writer) { |
|
138
|
12 |
|
$writer->writeItem($convertedItem, $item); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
13 |
|
$this->eventDispatcher->dispatch(ImportItemEvent::AFTER_WRITE, $event); |
|
|
|
|
|
|
142
|
13 |
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: