1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Configuration\Subject |
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-cli-simple |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Configuration; |
22
|
|
|
|
23
|
|
|
use JMS\Serializer\Annotation\Type; |
24
|
|
|
use JMS\Serializer\Annotation\SerializedName; |
25
|
|
|
use TechDivision\Import\ConfigurationInterface; |
26
|
|
|
use TechDivision\Import\Configuration\SubjectInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The subject configuration implementation. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class Subject implements SubjectInterface |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The subject's class name. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
* @Type("string") |
45
|
|
|
* @SerializedName("class-name") |
46
|
|
|
*/ |
47
|
|
|
protected $className; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The subject's processor type to use. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
* @Type("string") |
54
|
|
|
* @SerializedName("processor-factory") |
55
|
|
|
*/ |
56
|
|
|
protected $processorFactory; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The subject's utility class with the SQL statements to use. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
* @Type("string") |
63
|
|
|
* @SerializedName("utility-class-name") |
64
|
|
|
*/ |
65
|
|
|
protected $utilityClassName; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* The file prefix for import files. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
* @Type("string") |
72
|
|
|
*/ |
73
|
|
|
protected $prefix = 'magento-import'; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The array with the subject's observers. |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
* @Type("array") |
80
|
|
|
*/ |
81
|
|
|
protected $observers = array(); |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The array with the subject's callbacks. |
85
|
|
|
* |
86
|
|
|
* @var array |
87
|
|
|
* @Type("array<string, array>") |
88
|
|
|
*/ |
89
|
|
|
protected $callbacks = array(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The array with the subject's params. |
93
|
|
|
* |
94
|
|
|
* @var array |
95
|
|
|
* @Type("array") |
96
|
|
|
*/ |
97
|
|
|
protected $params = array(); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* A reference to the parent configuration instance. |
101
|
|
|
* |
102
|
|
|
* @var \TechDivision\Import\ConfigurationInterface |
103
|
|
|
*/ |
104
|
|
|
protected $configuration; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The flag to signal that the subjects needs a OK file to be processed or not. |
108
|
|
|
* |
109
|
|
|
* @var boolean |
110
|
|
|
* @Type("boolean") |
111
|
|
|
* @SerializedName("ok-file-needed") |
112
|
|
|
*/ |
113
|
|
|
protected $okFileNeeded = false; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return's the multiple field delimiter character to use, default value is comma (,). |
117
|
|
|
* |
118
|
|
|
* @return string The multiple field delimiter character |
119
|
|
|
*/ |
120
|
|
|
public function getMultipleFieldDelimiter() |
121
|
|
|
{ |
122
|
|
|
return $this->getConfiguration()->getMultipleFieldDelimiter(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return's the delimiter character to use, default value is comma (,). |
127
|
|
|
* |
128
|
|
|
* @return string The delimiter character |
129
|
|
|
*/ |
130
|
|
|
public function getDelimiter() |
131
|
|
|
{ |
132
|
|
|
return $this->getConfiguration()->getDelimiter(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The enclosure character to use, default value is double quotation ("). |
137
|
|
|
* |
138
|
|
|
* @return string The enclosure character |
139
|
|
|
*/ |
140
|
|
|
public function getEnclosure() |
141
|
|
|
{ |
142
|
|
|
return $this->getConfiguration()->getEnclosure(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* The escape character to use, default value is backslash (\). |
147
|
|
|
* |
148
|
|
|
* @return string The escape character |
149
|
|
|
*/ |
150
|
|
|
public function getEscape() |
151
|
|
|
{ |
152
|
|
|
return $this->getConfiguration()->getEscape(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* The file encoding of the CSV source file, default value is UTF-8. |
157
|
|
|
* |
158
|
|
|
* @return string The charset used by the CSV source file |
159
|
|
|
*/ |
160
|
|
|
public function getFromCharset() |
161
|
|
|
{ |
162
|
|
|
return $this->getConfiguration()->getFromCharset(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* The file encoding of the CSV targetfile, default value is UTF-8. |
167
|
|
|
* |
168
|
|
|
* @return string The charset used by the CSV target file |
169
|
|
|
*/ |
170
|
|
|
public function getToCharset() |
171
|
|
|
{ |
172
|
|
|
return $this->getConfiguration()->getToCharset(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* The file mode of the CSV target file, either one of write or append, default is write. |
177
|
|
|
* |
178
|
|
|
* @return string The file mode of the CSV target file |
179
|
|
|
*/ |
180
|
|
|
public function getFileMode() |
181
|
|
|
{ |
182
|
|
|
return $this->getConfiguration()->getFileMode(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Queries whether or not strict mode is enabled or not, default is TRUE. |
187
|
|
|
* |
188
|
|
|
* @return boolean TRUE if strict mode is enabled, else FALSE |
189
|
|
|
*/ |
190
|
|
|
public function isStrictMode() |
191
|
|
|
{ |
192
|
|
|
return $this->getConfiguration()->isStrictMode(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Return's the subject's source date format to use. |
197
|
|
|
* |
198
|
|
|
* @return string The source date format |
199
|
|
|
*/ |
200
|
|
|
public function getSourceDateFormat() |
201
|
|
|
{ |
202
|
|
|
return $this->getConfiguration()->getSourceDateFormat(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return's the source directory that has to be watched for new files. |
207
|
|
|
* |
208
|
|
|
* @return string The source directory |
209
|
|
|
*/ |
210
|
|
|
public function getSourceDir() |
211
|
|
|
{ |
212
|
|
|
return $this->getConfiguration()->getSourceDir(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Return's the target directory with the files that has been imported. |
217
|
|
|
* |
218
|
|
|
* @return string The target directory |
219
|
|
|
*/ |
220
|
|
|
public function getTargetDir() |
221
|
|
|
{ |
222
|
|
|
return $this->getConfiguration()->getTargetDir(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Queries whether or not debug mode is enabled or not, default is TRUE. |
227
|
|
|
* |
228
|
|
|
* @return boolean TRUE if debug mode is enabled, else FALSE |
229
|
|
|
*/ |
230
|
|
|
public function isDebugMode() |
231
|
|
|
{ |
232
|
|
|
return $this->getConfiguration()->isDebugMode(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Return's the subject's class name. |
237
|
|
|
* |
238
|
|
|
* @return string The subject's class name |
239
|
|
|
*/ |
240
|
|
|
public function getClassName() |
241
|
|
|
{ |
242
|
|
|
return $this->className; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Return's the subject's processor factory type to use. |
247
|
|
|
* |
248
|
|
|
* @return string The subject's processor factory type |
249
|
|
|
*/ |
250
|
|
|
public function getProcessorFactory() |
251
|
|
|
{ |
252
|
|
|
return $this->processorFactory; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Return's the utility class with the SQL statements to use. |
257
|
|
|
* |
258
|
|
|
* @return string The utility class name |
259
|
|
|
*/ |
260
|
|
|
public function getUtilityClassName() |
261
|
|
|
{ |
262
|
|
|
return $this->utilityClassName; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Return's the array with the params. |
267
|
|
|
* |
268
|
|
|
* @return array The params |
269
|
|
|
*/ |
270
|
|
|
public function getParams() |
271
|
|
|
{ |
272
|
|
|
if (!$params = reset($this->params)) { |
273
|
|
|
$params = array(); |
274
|
|
|
} |
275
|
|
|
return $params; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Query whether or not the param with the passed name exists. |
280
|
|
|
* |
281
|
|
|
* @param string $name The name of the param to be queried |
282
|
|
|
* |
283
|
|
|
* @return boolean TRUE if the requested param exists, else FALSE |
284
|
|
|
*/ |
285
|
|
|
public function hasParam($name) |
286
|
|
|
{ |
287
|
|
|
return array_key_exists($name, $this->getParams()); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Return's the param with the passed name. |
292
|
|
|
* |
293
|
|
|
* @param string $name The name of the param to return |
294
|
|
|
* @param mixed $defaultValue The default value if the param doesn't exists |
295
|
|
|
* |
296
|
|
|
* @return string The requested param |
297
|
|
|
* @throws \Exception Is thrown, if the requested param is not available |
298
|
|
|
*/ |
299
|
|
|
public function getParam($name, $defaultValue = null) |
300
|
|
|
{ |
301
|
|
|
|
302
|
|
|
// query whether or not, the param is set |
303
|
|
|
if (array_key_exists($name, $params = $this->getParams())) { |
304
|
|
|
return $params[$name]; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// if not, query we query if a default value has been passed |
308
|
|
|
if ($defaultValue != null) { |
309
|
|
|
return $defaultValue; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// throw an exception if neither the param exists or a default value has been passed |
313
|
|
|
throw new \Exception(sprintf('Requested param %s not available', $name)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Set's the reference to the configuration instance. |
318
|
|
|
* |
319
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
320
|
|
|
* |
321
|
|
|
* @return void |
322
|
|
|
*/ |
323
|
|
|
public function setConfiguration(ConfigurationInterface $configuration) |
324
|
|
|
{ |
325
|
|
|
$this->configuration = $configuration; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Return's the reference to the configuration instance. |
330
|
|
|
* |
331
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The configuration instance |
332
|
|
|
*/ |
333
|
|
|
public function getConfiguration() |
334
|
|
|
{ |
335
|
|
|
return $this->configuration; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Set's the prefix for the import files. |
340
|
|
|
* |
341
|
|
|
* @param string $prefix The prefix |
342
|
|
|
* |
343
|
|
|
* @return void |
344
|
|
|
*/ |
345
|
|
|
public function setPrefix($prefix) |
346
|
|
|
{ |
347
|
|
|
$this->prefix = $prefix; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Return's the prefix for the import files. |
352
|
|
|
* |
353
|
|
|
* @return string The prefix |
354
|
|
|
*/ |
355
|
|
|
public function getPrefix() |
356
|
|
|
{ |
357
|
|
|
return $this->prefix; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Return's the array with the subject's observers. |
362
|
|
|
* |
363
|
|
|
* @return array The subject's observers |
364
|
|
|
*/ |
365
|
|
|
public function getObservers() |
366
|
|
|
{ |
367
|
|
|
return $this->observers; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Return's the array with the subject's callbacks. |
372
|
|
|
* |
373
|
|
|
* @return array The subject's callbacks |
374
|
|
|
*/ |
375
|
|
|
public function getCallbacks() |
376
|
|
|
{ |
377
|
|
|
return $this->callbacks; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Set's the flag to signal that the an OK file is needed for the subject |
382
|
|
|
* to be processed. |
383
|
|
|
* |
384
|
|
|
* @param boolean $okFileNeeded TRUE if the subject needs an OK file, else FALSE |
385
|
|
|
* |
386
|
|
|
* @return void |
387
|
|
|
*/ |
388
|
|
|
public function setOkFileNeeded($okFileNeeded) |
389
|
|
|
{ |
390
|
|
|
$this->okFileNeeded = $okFileNeeded; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Queries whether or not that the subject needs an OK file to be processed. |
395
|
|
|
* |
396
|
|
|
* @return boolean TRUE if the subject needs an OK file, else FALSE |
397
|
|
|
*/ |
398
|
|
|
public function isOkFileNeeded() |
399
|
|
|
{ |
400
|
|
|
return $this->okFileNeeded; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|