1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mathielen\ImportEngine\Import\Run; |
4
|
|
|
|
5
|
|
|
use Mathielen\DataImport\Event\ImportProcessEvent; |
6
|
|
|
use Mathielen\ImportEngine\Import\Import; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
use Mathielen\DataImport\Event\ImportItemEvent; |
10
|
|
|
|
11
|
|
|
class ImportRunEventSubscriber implements EventSubscriberInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Import |
15
|
|
|
*/ |
16
|
|
|
private $import; |
17
|
|
|
|
18
|
|
|
private $statistics; |
19
|
|
|
|
20
|
|
|
private $isDryRun; |
21
|
|
|
|
22
|
13 |
|
public function __construct(Import $import, $isDryRun = false) |
23
|
|
|
{ |
24
|
13 |
|
$this->isDryRun = $isDryRun; |
25
|
13 |
|
$this->import = $import; |
26
|
13 |
|
$this->statistics = array( |
27
|
|
|
'processed' => 0, |
28
|
|
|
'written' => 0, |
29
|
|
|
'skipped' => 0, |
30
|
|
|
'invalid' => 0, |
31
|
|
|
); |
32
|
13 |
|
} |
33
|
|
|
|
34
|
13 |
|
public static function getSubscribedEvents() |
35
|
|
|
{ |
36
|
|
|
return array( |
37
|
13 |
|
ImportProcessEvent::AFTER_PREPARE => array('onImportPrepare', 999999), |
38
|
13 |
|
ImportItemEvent::AFTER_READ => array('onAfterRead', 0), |
39
|
13 |
|
ImportItemEvent::AFTER_FILTER => array('onAfterFilter', 0), |
40
|
13 |
|
ImportItemEvent::AFTER_CONVERSION => array('onAfterConversion', 0), |
41
|
13 |
|
ImportItemEvent::AFTER_CONVERSIONFILTER => array('onAfterConversionFilter', 0), |
42
|
13 |
|
ImportItemEvent::AFTER_VALIDATION => array('onAfterValidate', 0), |
43
|
13 |
|
ImportItemEvent::AFTER_WRITE => array('onAfterWrite', 0), |
44
|
|
|
ImportProcessEvent::AFTER_FINISH => array('onImportFinish', 999999), |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
13 |
|
public function onImportPrepare(ImportProcessEvent $event) |
49
|
|
|
{ |
50
|
13 |
|
$event->setContext($this->import); |
51
|
13 |
|
} |
52
|
|
|
|
53
|
13 |
|
public function onImportFinish(ImportProcessEvent $event, $eventName, EventDispatcherInterface $eventDispatcher) |
54
|
|
|
{ |
55
|
13 |
|
if (!$this->isDryRun) { |
56
|
11 |
|
$this->import->getRun()->finish(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
//remove the subscriber when its done |
60
|
13 |
|
$eventDispatcher->removeSubscriber($this); |
61
|
13 |
|
} |
62
|
|
|
|
63
|
13 |
|
public function onAfterRead(ImportItemEvent $event) |
64
|
|
|
{ |
65
|
13 |
|
++$this->statistics['processed']; |
66
|
|
|
|
67
|
13 |
|
$this->import->getRun()->setStatistics($this->statistics); |
68
|
13 |
|
} |
69
|
|
|
|
70
|
13 |
View Code Duplication |
public function onAfterFilter(ImportItemEvent $event) |
|
|
|
|
71
|
|
|
{ |
72
|
13 |
|
if (!$event->getCurrentResult()) { |
73
|
1 |
|
++$this->statistics['skipped']; |
74
|
|
|
|
75
|
1 |
|
$this->import->getRun()->setStatistics($this->statistics); |
76
|
|
|
} |
77
|
13 |
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function onAfterConversion(ImportItemEvent $event) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!$event->getCurrentResult()) { |
82
|
|
|
++$this->statistics['skipped']; |
83
|
|
|
|
84
|
|
|
$this->import->getRun()->setStatistics($this->statistics); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
View Code Duplication |
public function onAfterConversionFilter(ImportItemEvent $event) |
|
|
|
|
89
|
|
|
{ |
90
|
12 |
|
if (!$event->getCurrentResult()) { |
91
|
|
|
++$this->statistics['skipped']; |
92
|
|
|
|
93
|
|
|
$this->import->getRun()->setStatistics($this->statistics); |
94
|
|
|
} |
95
|
12 |
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function onAfterValidate(ImportItemEvent $event) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if (!$event->getCurrentResult()) { |
100
|
|
|
++$this->statistics['invalid']; |
101
|
|
|
|
102
|
|
|
$this->import->getRun()->setStatistics($this->statistics); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
12 |
|
public function onAfterWrite(ImportItemEvent $event) |
107
|
|
|
{ |
108
|
12 |
|
++$this->statistics['written']; |
109
|
|
|
|
110
|
12 |
|
$this->import->getRun()->setStatistics($this->statistics); |
111
|
12 |
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.