1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Subjects\SubjectExecutor |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
22
|
|
|
|
23
|
|
|
use League\Event\EmitterInterface; |
24
|
|
|
use TechDivision\Import\Utils\BunchKeys; |
25
|
|
|
use TechDivision\Import\Utils\EventNames; |
26
|
|
|
use TechDivision\Import\Callbacks\CallbackVisitorInterface; |
27
|
|
|
use TechDivision\Import\Observers\ObserverVisitorInterface; |
28
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The subject executor instance. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2017 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/techdivision/import |
37
|
|
|
* @link http://www.techdivision.com |
38
|
|
|
*/ |
39
|
|
|
class SubjectExecutor implements SubjectExecutorInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The subject factory instance. |
44
|
|
|
* |
45
|
|
|
* @var \TechDivision\Import\Observers\ObserverVisitorInterface |
46
|
|
|
*/ |
47
|
|
|
protected $observerVisitor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The subject factory instance. |
51
|
|
|
* |
52
|
|
|
* @var \TechDivision\Import\Callbacks\CallbackVisitorInterface |
53
|
|
|
*/ |
54
|
|
|
protected $callbackVisitor; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The subject factory instance. |
58
|
|
|
* |
59
|
|
|
* @var \TechDivision\Import\Subjects\SubjectFactoryInterface |
60
|
|
|
*/ |
61
|
|
|
protected $subjectFactory; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The event emitter instance. |
65
|
|
|
* |
66
|
|
|
* @var \League\Event\EmitterInterface |
67
|
|
|
*/ |
68
|
|
|
protected $emitter; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Initializes the plugin with the application instance. |
72
|
|
|
* |
73
|
|
|
* @param \TechDivision\Import\Callbacks\CallbackVisitorInterface $callbackVisitor The callback visitor instance |
74
|
|
|
* @param \TechDivision\Import\Observers\ObserverVisitorInterface $observerVisitor The observer visitor instance |
75
|
|
|
* @param \TechDivision\Import\Subjects\SubjectFactoryInterface $subjectFactory The subject factory instance |
76
|
|
|
* @param \League\Event\EmitterInterface $emitter The event emitter instance |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
CallbackVisitorInterface $callbackVisitor, |
80
|
|
|
ObserverVisitorInterface $observerVisitor, |
81
|
|
|
SubjectFactoryInterface $subjectFactory, |
82
|
|
|
EmitterInterface $emitter |
83
|
|
|
) { |
84
|
|
|
|
85
|
|
|
// initialize the callback/observer visitors |
86
|
|
|
$this->callbackVisitor = $callbackVisitor; |
87
|
|
|
$this->observerVisitor = $observerVisitor; |
88
|
|
|
$this->subjectFactory = $subjectFactory; |
89
|
|
|
$this->emitter = $emitter; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Executes the passed subject. |
94
|
|
|
* |
95
|
|
|
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject configuration instance |
96
|
|
|
* @param array $matches The bunch matches |
97
|
|
|
* @param string $serial The UUID of the actual import |
98
|
|
|
* @param string $pathname The path to the file to import |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function execute(SubjectConfigurationInterface $subject, array $matches, $serial, $pathname) |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
// initialize the subject and import the bunch |
106
|
|
|
$subjectInstance = $this->subjectFactory->createSubject($subject); |
107
|
|
|
|
108
|
|
|
// load the plug-in an operation configuartion |
109
|
|
|
$pluginConfiguration = $subject->getPluginConfiguration(); |
110
|
|
|
$operationConfiguration = $pluginConfiguration->getOperationConfiguration(); |
111
|
|
|
|
112
|
|
|
// load the subject + plug-in ID as well as th operation name to prepare the events |
113
|
|
|
$subjectName = $subject->getName(); |
114
|
|
|
$pluginName = $pluginConfiguration->getName(); |
115
|
|
|
$operationName = $operationConfiguration->getName(); |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
// invoke the event that has to be fired before the subject's import method will be invoked |
119
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_START, $subjectInstance); |
|
|
|
|
120
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_START), $subjectInstance); |
|
|
|
|
121
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_START), $subjectInstance); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// setup the subject instance |
124
|
|
|
$subjectInstance->setUp($serial); |
125
|
|
|
|
126
|
|
|
// initialize the callbacks/observers |
127
|
|
|
$this->callbackVisitor->visit($subjectInstance); |
128
|
|
|
$this->observerVisitor->visit($subjectInstance); |
129
|
|
|
|
130
|
|
|
// finally import the CSV file |
131
|
|
|
$subjectInstance->import($serial, $pathname); |
132
|
|
|
|
133
|
|
|
// query whether or not, we've to export artefacts |
134
|
|
|
if ($subjectInstance instanceof ExportableSubjectInterface) { |
135
|
|
|
try { |
136
|
|
|
// invoke the event that has to be fired before the subject's export method will be invoked |
137
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_START, $subjectInstance); |
|
|
|
|
138
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_START), $subjectInstance); |
|
|
|
|
139
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_START), $subjectInstance); |
|
|
|
|
140
|
|
|
|
141
|
|
|
// export the artefacts if available |
142
|
|
|
$subjectInstance->export($matches[BunchKeys::FILENAME], $matches[BunchKeys::COUNTER]); |
143
|
|
|
|
144
|
|
|
// invoke the event that has to be fired after the subject's export method has been invoked |
145
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_SUCCESS, $subjectInstance); |
|
|
|
|
146
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_SUCCESS), $subjectInstance); |
|
|
|
|
147
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_SUCCESS), $subjectInstance); |
|
|
|
|
148
|
|
|
} catch (\Exception $e) { |
149
|
|
|
// invoke the event that has to be fired when the subject's export method throws an exception |
150
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_FAILURE, $subjectInstance); |
|
|
|
|
151
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_FAILURE), $subjectInstance); |
|
|
|
|
152
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_FAILURE), $subjectInstance); |
|
|
|
|
153
|
|
|
|
154
|
|
|
// re-throw the exception |
155
|
|
|
throw $e; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// tear down the subject instance |
160
|
|
|
$subjectInstance->tearDown($serial); |
161
|
|
|
|
162
|
|
|
// invoke the event that has to be fired after the subject's import method has been invoked |
163
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_SUCCESS, $subjectInstance); |
|
|
|
|
164
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_SUCCESS), $subjectInstance); |
|
|
|
|
165
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_SUCCESS), $subjectInstance); |
|
|
|
|
166
|
|
|
} catch (\Exception $e) { |
167
|
|
|
// tear down the subject instance |
168
|
|
|
$subjectInstance->tearDown($serial); |
169
|
|
|
|
170
|
|
|
// invoke the event that has to be fired when the subject's import method throws an exception |
171
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_FAILURE, $subjectInstance); |
|
|
|
|
172
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance); |
|
|
|
|
173
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s.%s', $operationName, $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// re-throw the exception |
176
|
|
|
throw $e; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.