1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Plugins\AbstractPlugin |
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\LoggerKeys; |
24
|
|
|
use TechDivision\Import\ApplicationInterface; |
25
|
|
|
use TechDivision\Import\Configuration\PluginConfigurationInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Abstract plugin implementation. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/techdivision/import |
34
|
|
|
* @link http://www.techdivision.com |
35
|
|
|
*/ |
36
|
|
|
abstract class AbstractPlugin implements PluginInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The appliation instance. |
41
|
|
|
* |
42
|
|
|
* @var \TechDivision\Import\ApplicationInterface |
43
|
|
|
*/ |
44
|
|
|
protected $application; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The plugin configuration instance. |
48
|
|
|
* |
49
|
|
|
* @var \TechDivision\Import\Configuration\PluginConfigurationInterface |
50
|
|
|
*/ |
51
|
|
|
protected $pluginConfiguration; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initializes the plugin with the application instance. |
55
|
|
|
* |
56
|
|
|
* @param \TechDivision\Import\ApplicationInterface $application The application instance |
57
|
|
|
*/ |
58
|
3 |
|
public function __construct(ApplicationInterface $application) |
59
|
|
|
{ |
60
|
3 |
|
$this->application = $application; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set's the plugin configuration instance. |
65
|
|
|
* |
66
|
|
|
* @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
3 |
|
public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration) |
71
|
|
|
{ |
72
|
3 |
|
$this->pluginConfiguration = $pluginConfiguration; |
73
|
3 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return's the plugin configuration instance. |
77
|
|
|
* |
78
|
|
|
* @return \TechDivision\Import\Configuration\PluginConfigurationInterface The plugin configuration instance |
79
|
|
|
*/ |
80
|
3 |
|
public function getPluginConfiguration() |
81
|
|
|
{ |
82
|
3 |
|
return $this->pluginConfiguration; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return's the application instance. |
87
|
|
|
* |
88
|
|
|
* @return \TechDivision\Import\ApplicationInterface The application instance |
89
|
|
|
*/ |
90
|
3 |
|
protected function getApplication() |
91
|
|
|
{ |
92
|
3 |
|
return $this->application; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return's the RegistryProcessor instance to handle the running threads. |
97
|
|
|
* |
98
|
|
|
* @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
99
|
|
|
*/ |
100
|
3 |
|
protected function getRegistryProcessor() |
101
|
|
|
{ |
102
|
3 |
|
return $this->getApplication()->getRegistryProcessor(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return's the import processor instance. |
107
|
|
|
* |
108
|
|
|
* @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
109
|
|
|
*/ |
110
|
|
|
protected function getImportProcessor() |
111
|
|
|
{ |
112
|
|
|
return $this->getApplication()->getImportProcessor(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return's the unique serial for this import process. |
117
|
|
|
* |
118
|
|
|
* @return string The unique serial |
119
|
|
|
*/ |
120
|
2 |
|
public function getSerial() |
121
|
|
|
{ |
122
|
2 |
|
return $this->getApplication()->getSerial(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return's the logger with the passed name, by default the system logger. |
127
|
|
|
* |
128
|
|
|
* @param string $name The name of the requested system logger |
129
|
|
|
* |
130
|
|
|
* @return \Psr\Log\LoggerInterface The logger instance |
131
|
|
|
* @throws \Exception Is thrown, if the requested logger is NOT available |
132
|
|
|
*/ |
133
|
1 |
|
protected function getSystemLogger($name = LoggerKeys::SYSTEM) |
134
|
|
|
{ |
135
|
1 |
|
return $this->getApplication()->getSystemLogger($name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Query whether or not the system logger with the passed name is available. |
140
|
|
|
* |
141
|
|
|
* @param string $name The name of the requested system logger |
142
|
|
|
* |
143
|
|
|
* @return boolean TRUE if the logger with the passed name exists, else FALSE |
144
|
|
|
*/ |
145
|
|
|
protected function hasSystemLogger($name = LoggerKeys::SYSTEM) |
146
|
|
|
{ |
147
|
|
|
return $this->getApplication()->hasSystemLogger($name); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Return's the array with the system logger instances. |
152
|
|
|
* |
153
|
|
|
* @return array The logger instance |
154
|
|
|
*/ |
155
|
|
|
protected function getSystemLoggers() |
156
|
|
|
{ |
157
|
|
|
return $this->getApplication()->getSystemLoggers(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Persist the UUID of the actual import process to the PID file. |
162
|
|
|
* |
163
|
|
|
* @return void |
164
|
|
|
* @throws \Exception Is thrown, if the PID can not be added |
165
|
|
|
*/ |
166
|
|
|
protected function lock() |
167
|
|
|
{ |
168
|
|
|
$this->getApplication()->lock(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Remove's the UUID of the actual import process from the PID file. |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
* @throws \Exception Is thrown, if the PID can not be removed |
176
|
|
|
*/ |
177
|
|
|
protected function unlock() |
178
|
|
|
{ |
179
|
|
|
$this->getApplication()->unlock(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Remove's the passed line from the file with the passed name. |
184
|
|
|
* |
185
|
|
|
* @param string $line The line to be removed |
186
|
|
|
* @param string $filename The name of the file the line has to be removed |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
* @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
190
|
|
|
*/ |
191
|
|
|
protected function removeLineFromFile($line, $filename) |
192
|
|
|
{ |
193
|
|
|
$this->getApplication()->removeLineFromFile($line, $filename); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return's the system configuration. |
198
|
|
|
* |
199
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The system configuration |
200
|
|
|
*/ |
201
|
1 |
|
protected function getConfiguration() |
202
|
|
|
{ |
203
|
1 |
|
return $this->getApplication()->getConfiguration(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Return's the PID filename to use. |
208
|
|
|
* |
209
|
|
|
* @return string The PID filename |
210
|
|
|
*/ |
211
|
|
|
protected function getPidFilename() |
212
|
|
|
{ |
213
|
|
|
return $this->getConfiguration()->getPidFilename(); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Return's the source directory that has to be watched for new files. |
218
|
|
|
* |
219
|
|
|
* @return string The source directory |
220
|
|
|
*/ |
221
|
|
|
protected function getSourceDir() |
222
|
|
|
{ |
223
|
|
|
return $this->getConfiguration()->getSourceDir(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Removes the passed directory recursively. |
228
|
|
|
* |
229
|
|
|
* @param string $src Name of the directory to remove |
230
|
|
|
* |
231
|
|
|
* @return void |
232
|
|
|
* @throws \Exception Is thrown, if the directory can not be removed |
233
|
|
|
*/ |
234
|
|
|
protected function removeDir($src) |
235
|
|
|
{ |
236
|
|
|
|
237
|
|
|
// open the directory |
238
|
|
|
$dir = opendir($src); |
239
|
|
|
|
240
|
|
|
// remove files/folders recursively |
241
|
|
|
while (false !== ($file = readdir($dir))) { |
242
|
|
|
if (($file != '.') && ($file != '..')) { |
243
|
|
|
$full = $src . '/' . $file; |
244
|
|
|
if (is_dir($full)) { |
245
|
|
|
$this->removeDir($full); |
246
|
|
|
} else { |
247
|
|
|
if (!unlink($full)) { |
248
|
|
|
throw new \Exception(sprintf('Can\'t remove file %s', $full)); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
// close handle and remove directory itself |
255
|
|
|
closedir($dir); |
256
|
|
|
if (!rmdir($src)) { |
257
|
|
|
throw new \Exception(sprintf('Can\'t remove directory %s', $src)); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return's the configured swift mailer instance. |
263
|
|
|
* |
264
|
|
|
* @return \Swift_Mailer|null The mailer instance |
265
|
|
|
*/ |
266
|
|
|
protected function getSwiftMailer() |
267
|
|
|
{ |
268
|
|
|
|
269
|
|
|
// the swift mailer configuration |
270
|
|
|
if ($swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer()) { |
|
|
|
|
271
|
|
|
// load the factory that creates the swift mailer instance |
272
|
|
|
$factory = $swiftMailerConfiguration->getFactory(); |
273
|
|
|
// create the swift mailer instance |
274
|
|
|
return $factory::factory($swiftMailerConfiguration); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
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.