1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Plugins\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\Plugins; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\BunchKeys; |
24
|
|
|
use TechDivision\Import\Callbacks\CallbackVisitorInterface; |
25
|
|
|
use TechDivision\Import\Observers\ObserverVisitorInterface; |
26
|
|
|
use TechDivision\Import\Subjects\SubjectFactoryInterface; |
27
|
|
|
use TechDivision\Import\Subjects\ExportableSubjectInterface; |
28
|
|
|
use TechDivision\Import\Configuration\SubjectConfigurationInterface; |
29
|
|
|
use League\Event\EmitterInterface; |
30
|
|
|
use TechDivision\Import\Utils\EventNames; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The subject executor instance. |
34
|
|
|
* |
35
|
|
|
* @author Tim Wagner <[email protected]> |
36
|
|
|
* @copyright 2017 TechDivision GmbH <[email protected]> |
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
38
|
|
|
* @link https://github.com/techdivision/import |
39
|
|
|
* @link http://www.techdivision.com |
40
|
|
|
*/ |
41
|
|
|
class SubjectExecutor implements SubjectExecutorInterface |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The subject factory instance. |
46
|
|
|
* |
47
|
|
|
* @var \TechDivision\Import\Observers\ObserverVisitorInterface |
48
|
|
|
*/ |
49
|
|
|
protected $observerVisitor; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The subject factory instance. |
53
|
|
|
* |
54
|
|
|
* @var \TechDivision\Import\Callbacks\CallbackVisitorInterface |
55
|
|
|
*/ |
56
|
|
|
protected $callbackVisitor; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The subject factory instance. |
60
|
|
|
* |
61
|
|
|
* @var \TechDivision\Import\Subjects\SubjectFactoryInterface |
62
|
|
|
*/ |
63
|
|
|
protected $subjectFactory; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The event emitter instance. |
67
|
|
|
* |
68
|
|
|
* @var \League\Event\EmitterInterface |
69
|
|
|
*/ |
70
|
|
|
protected $emitter; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initializes the plugin with the application instance. |
74
|
|
|
* |
75
|
|
|
* @param \TechDivision\Import\Callbacks\CallbackVisitorInterface $callbackVisitor The callback visitor instance |
76
|
|
|
* @param \TechDivision\Import\Observers\ObserverVisitorInterface $observerVisitor The observer visitor instance |
77
|
|
|
* @param \TechDivision\Import\Subjects\SubjectFactoryInterface $subjectFactory The subject factory instance |
78
|
|
|
* @param \League\Event\EmitterInterface $emitter The event emitter instance |
79
|
|
|
*/ |
80
|
|
|
public function __construct( |
81
|
|
|
CallbackVisitorInterface $callbackVisitor, |
82
|
|
|
ObserverVisitorInterface $observerVisitor, |
83
|
|
|
SubjectFactoryInterface $subjectFactory, |
84
|
|
|
EmitterInterface $emitter |
85
|
|
|
) { |
86
|
|
|
|
87
|
|
|
// initialize the callback/observer visitors |
88
|
|
|
$this->callbackVisitor = $callbackVisitor; |
89
|
|
|
$this->observerVisitor = $observerVisitor; |
90
|
|
|
$this->subjectFactory = $subjectFactory; |
91
|
|
|
$this->emitter = $emitter; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Executes the passed subject. |
96
|
|
|
* |
97
|
|
|
* @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The message with the subject information |
98
|
|
|
* @param array $matches The bunch matches |
99
|
|
|
* @param string $serial The UUID of the actual import |
100
|
|
|
* @param string $pathname The path to the file to import |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public function execute(SubjectConfigurationInterface $subject, array $matches, $serial, $pathname) |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
// initialize the subject and import the bunch |
108
|
|
|
$subjectInstance = $this->subjectFactory->createSubject($subject); |
109
|
|
|
|
110
|
|
|
try { |
111
|
|
|
// load the subject + plug-in ID to prepare the event names |
112
|
|
|
$subjectName = $subject->getName(); |
113
|
|
|
$pluginName = $subject->getPluginConfiguration()->getName(); |
|
|
|
|
114
|
|
|
|
115
|
|
|
// invoke the event that has to be fired before the subject's import method will be invoked |
116
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_START, $subjectInstance); |
|
|
|
|
117
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_START), $subjectInstance); |
|
|
|
|
118
|
|
|
|
119
|
|
|
// setup the subject instance |
120
|
|
|
$subjectInstance->setUp($serial); |
121
|
|
|
|
122
|
|
|
// initialize the callbacks/observers |
123
|
|
|
$this->callbackVisitor->visit($subjectInstance); |
124
|
|
|
$this->observerVisitor->visit($subjectInstance); |
125
|
|
|
|
126
|
|
|
// finally import the CSV file |
127
|
|
|
$subjectInstance->import($serial, $pathname); |
128
|
|
|
|
129
|
|
|
// query whether or not, we've to export artefacts |
130
|
|
|
if ($subjectInstance instanceof ExportableSubjectInterface) { |
131
|
|
|
try { |
132
|
|
|
// invoke the event that has to be fired before the subject's export method will be invoked |
133
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_START, $subjectInstance); |
|
|
|
|
134
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_START), $subjectInstance); |
|
|
|
|
135
|
|
|
|
136
|
|
|
// export the artefacts if available |
137
|
|
|
$subjectInstance->export( |
138
|
|
|
$matches[BunchKeys::FILENAME], |
139
|
|
|
$matches[BunchKeys::COUNTER] |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
// invoke the event that has to be fired after the subject's export method has been invoked |
143
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_SUCCESS, $subjectInstance); |
|
|
|
|
144
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_SUCCESS), $subjectInstance); |
|
|
|
|
145
|
|
|
} catch (\Exception $e) { |
146
|
|
|
// invoke the event that has to be fired when the subject's export method throws an exception |
147
|
|
|
$this->emitter->emit(EventNames::SUBJECT_EXPORT_FAILURE, $subjectInstance); |
|
|
|
|
148
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_EXPORT_FAILURE), $subjectInstance); |
|
|
|
|
149
|
|
|
|
150
|
|
|
// re-throw the exception |
151
|
|
|
throw $e; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// tear down the subject instance |
156
|
|
|
$subjectInstance->tearDown($serial); |
157
|
|
|
|
158
|
|
|
// invoke the event that has to be fired after the subject's import method has been invoked |
159
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_SUCCESS, $subjectInstance); |
|
|
|
|
160
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_SUCCESS), $subjectInstance); |
|
|
|
|
161
|
|
|
} catch (\Exception $e) { |
162
|
|
|
// query whether or not, we've to export artefacts |
163
|
|
|
if ($subjectInstance instanceof ExportableSubjectInterface) { |
164
|
|
|
// tear down the subject instance |
165
|
|
|
$subjectInstance->tearDown($serial); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// invoke the event that has to be fired when the subject's import method throws an exception |
169
|
|
|
$this->emitter->emit(EventNames::SUBJECT_IMPORT_FAILURE, $subjectInstance); |
|
|
|
|
170
|
|
|
$this->emitter->emit(sprintf('%s.%s.%s', $pluginName, $subjectName, EventNames::SUBJECT_IMPORT_FAILURE), $subjectInstance); |
|
|
|
|
171
|
|
|
|
172
|
|
|
// re-throw the exception |
173
|
|
|
throw $e; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.