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
|
|
|
* A SLSB that handles the product import process. |
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 delimiter character for CSV files. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
* @Type("string") |
45
|
|
|
*/ |
46
|
|
|
protected $delimiter; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The subject's enclosure character for CSV files. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
* @Type("string") |
53
|
|
|
*/ |
54
|
|
|
protected $enclosure; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The subject's escape character for CSV files. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
* @Type("string") |
61
|
|
|
*/ |
62
|
|
|
protected $escape; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The subject's source charset for the CSV file. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
* @Type("string") |
69
|
|
|
* @SerializedName("from-charset") |
70
|
|
|
*/ |
71
|
|
|
protected $fromCharset; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The subject's target charset for a CSV file. |
75
|
|
|
* |
76
|
|
|
* @var string |
77
|
|
|
* @Type("string") |
78
|
|
|
* @SerializedName("to-charset") |
79
|
|
|
*/ |
80
|
|
|
protected $toCharset; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The subject's file mode for a CSV target file. |
84
|
|
|
* |
85
|
|
|
* @var string |
86
|
|
|
* @Type("string") |
87
|
|
|
* @SerializedName("file-mode") |
88
|
|
|
*/ |
89
|
|
|
protected $fileMode; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The flag to signal that the subject has to use the strict mode or not. |
93
|
|
|
* |
94
|
|
|
* @var boolean |
95
|
|
|
* @Type("boolean") |
96
|
|
|
* @SerializedName("strict-mode") |
97
|
|
|
*/ |
98
|
|
|
protected $strictMode; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The subject's class name. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
* @Type("string") |
105
|
|
|
* @SerializedName("class-name") |
106
|
|
|
*/ |
107
|
|
|
protected $className; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The subject's processor type to use. |
111
|
|
|
* |
112
|
|
|
* @var string |
113
|
|
|
* @Type("string") |
114
|
|
|
* @SerializedName("processor-factory") |
115
|
|
|
*/ |
116
|
|
|
protected $processorFactory; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The subject's utility class with the SQL statements to use. |
120
|
|
|
* |
121
|
|
|
* @var string |
122
|
|
|
* @Type("string") |
123
|
|
|
* @SerializedName("utility-class-name") |
124
|
|
|
*/ |
125
|
|
|
protected $utilityClassName; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The file prefix for import files. |
129
|
|
|
* |
130
|
|
|
* @var string |
131
|
|
|
* @Type("string") |
132
|
|
|
*/ |
133
|
|
|
protected $prefix = 'magento-import'; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The source date format to use in the subject. |
137
|
|
|
* |
138
|
|
|
* @var string |
139
|
|
|
* @Type("string") |
140
|
|
|
* @SerializedName("source-date-format") |
141
|
|
|
*/ |
142
|
|
|
protected $sourceDateFormat = 'n/d/y, g:i A'; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* The source directory that has to be watched for new files. |
146
|
|
|
* |
147
|
|
|
* @var string |
148
|
|
|
* @Type("string") |
149
|
|
|
* @SerializedName("source-dir") |
150
|
|
|
*/ |
151
|
|
|
protected $sourceDir; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The target directory with the files to be imported. |
155
|
|
|
* |
156
|
|
|
* @var string |
157
|
|
|
* @Type("string") |
158
|
|
|
* @SerializedName("target-dir") |
159
|
|
|
*/ |
160
|
|
|
protected $targetDir; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* The array with the subject's observers. |
164
|
|
|
* |
165
|
|
|
* @var array |
166
|
|
|
* @Type("array") |
167
|
|
|
*/ |
168
|
|
|
protected $observers = array(); |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* The array with the subject's callbacks. |
172
|
|
|
* |
173
|
|
|
* @var array |
174
|
|
|
* @Type("array<string, array>") |
175
|
|
|
*/ |
176
|
|
|
protected $callbacks = array(); |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* The array with the subject's params. |
180
|
|
|
* |
181
|
|
|
* @var array |
182
|
|
|
* @Type("array") |
183
|
|
|
*/ |
184
|
|
|
protected $params = array(); |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* A reference to the parent configuration instance. |
188
|
|
|
* |
189
|
|
|
* @var \TechDivision\Import\ConfigurationInterface |
190
|
|
|
*/ |
191
|
|
|
protected $configuration; |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Return's the delimiter character to use, default value is comma (,). |
195
|
|
|
* |
196
|
|
|
* @return string The delimiter character |
197
|
|
|
*/ |
198
|
|
|
public function getDelimiter() |
199
|
|
|
{ |
200
|
|
|
return $this->delimiter; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* The enclosure character to use, default value is double quotation ("). |
205
|
|
|
* |
206
|
|
|
* @return string The enclosure character |
207
|
|
|
*/ |
208
|
|
|
public function getEnclosure() |
209
|
|
|
{ |
210
|
|
|
return $this->enclosure; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* The escape character to use, default value is backslash (\). |
215
|
|
|
* |
216
|
|
|
* @return string The escape character |
217
|
|
|
*/ |
218
|
|
|
public function getEscape() |
219
|
|
|
{ |
220
|
|
|
return $this->escape; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* The file encoding of the CSV source file, default value is UTF-8. |
225
|
|
|
* |
226
|
|
|
* @return string The charset used by the CSV source file |
227
|
|
|
*/ |
228
|
|
|
public function getFromCharset() |
229
|
|
|
{ |
230
|
|
|
return $this->fromCharset; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* The file encoding of the CSV targetfile, default value is UTF-8. |
235
|
|
|
* |
236
|
|
|
* @return string The charset used by the CSV target file |
237
|
|
|
*/ |
238
|
|
|
public function getToCharset() |
239
|
|
|
{ |
240
|
|
|
return $this->toCharset; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* The file mode of the CSV target file, either one of write or append, default is write. |
245
|
|
|
* |
246
|
|
|
* @return string The file mode of the CSV target file |
247
|
|
|
*/ |
248
|
|
|
public function getFileMode() |
249
|
|
|
{ |
250
|
|
|
return $this->fileMode; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Queries whether or not strict mode is enabled or not, default is TRUE. |
255
|
|
|
* |
256
|
|
|
* @return boolean TRUE if strict mode is enabled, else FALSE |
257
|
|
|
*/ |
258
|
|
|
public function isStrictMode() |
259
|
|
|
{ |
260
|
|
|
return $this->strictMode; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Return's the subject's class name. |
265
|
|
|
* |
266
|
|
|
* @return string The subject's class name |
267
|
|
|
*/ |
268
|
|
|
public function getClassName() |
269
|
|
|
{ |
270
|
|
|
return $this->className; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Return's the subject's processor factory type to use. |
275
|
|
|
* |
276
|
|
|
* @return string The subject's processor factory type |
277
|
|
|
*/ |
278
|
|
|
public function getProcessorFactory() |
279
|
|
|
{ |
280
|
|
|
return $this->processorFactory; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return's the utility class with the SQL statements to use. |
285
|
|
|
* |
286
|
|
|
* @return string The utility class name |
287
|
|
|
*/ |
288
|
|
|
public function getUtilityClassName() |
289
|
|
|
{ |
290
|
|
|
return $this->utilityClassName; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Return's the array with the params. |
295
|
|
|
* |
296
|
|
|
* @return array The params |
297
|
|
|
*/ |
298
|
|
|
public function getParams() |
299
|
|
|
{ |
300
|
|
|
if (!$params = reset($this->params)) { |
301
|
|
|
$params = array(); |
302
|
|
|
} |
303
|
|
|
return $params; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Query whether or not the param with the passed name exists. |
308
|
|
|
* |
309
|
|
|
* @param string $name The name of the param to be queried |
310
|
|
|
* |
311
|
|
|
* @return boolean TRUE if the requested param exists, else FALSE |
312
|
|
|
*/ |
313
|
|
|
public function hasParam($name) |
314
|
|
|
{ |
315
|
|
|
return array_key_exists($name, $this->getParams()); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Return's the param with the passed name. |
320
|
|
|
* |
321
|
|
|
* @param string $name The name of the param to return |
322
|
|
|
* @param mixed $defaultValue The default value if the param doesn't exists |
323
|
|
|
* |
324
|
|
|
* @return string The requested param |
325
|
|
|
* @throws \Exception Is thrown, if the requested param is not available |
326
|
|
|
*/ |
327
|
|
|
public function getParam($name, $defaultValue = null) |
328
|
|
|
{ |
329
|
|
|
|
330
|
|
|
// query whether or not, the param is set |
331
|
|
|
if (array_key_exists($name, $params = $this->getParams())) { |
332
|
|
|
return $params[$name]; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
// if not, query we query if a default value has been passed |
336
|
|
|
if ($defaultValue != null) { |
337
|
|
|
return $defaultValue; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// throw an exception if neither the param exists or a default value has been passed |
341
|
|
|
throw new \Exception(sprintf('Requested param %s not available', $name)); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Set's the reference to the configuration instance. |
346
|
|
|
* |
347
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
348
|
|
|
* |
349
|
|
|
* @return void |
350
|
|
|
*/ |
351
|
|
|
public function setConfiguration(ConfigurationInterface $configuration) |
352
|
|
|
{ |
353
|
|
|
$this->configuration = $configuration; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Return's the reference to the configuration instance. |
358
|
|
|
* |
359
|
|
|
* @return \TechDivision\Import\ConfigurationInterface The configuration instance |
360
|
|
|
*/ |
361
|
|
|
public function getConfiguration() |
362
|
|
|
{ |
363
|
|
|
return $this->configuration; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Set's the prefix for the import files. |
368
|
|
|
* |
369
|
|
|
* @param string $prefix The prefix |
370
|
|
|
* |
371
|
|
|
* @return void |
372
|
|
|
*/ |
373
|
|
|
public function setPrefix($prefix) |
374
|
|
|
{ |
375
|
|
|
$this->prefix = $prefix; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Return's the prefix for the import files. |
380
|
|
|
* |
381
|
|
|
* @return string The prefix |
382
|
|
|
*/ |
383
|
|
|
public function getPrefix() |
384
|
|
|
{ |
385
|
|
|
return $this->prefix; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Return's the array with the subject's observers. |
390
|
|
|
* |
391
|
|
|
* @return array The subject's observers |
392
|
|
|
*/ |
393
|
|
|
public function getObservers() |
394
|
|
|
{ |
395
|
|
|
return $this->observers; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Return's the array with the subject's callbacks. |
400
|
|
|
* |
401
|
|
|
* @return array The subject's callbacks |
402
|
|
|
*/ |
403
|
|
|
public function getCallbacks() |
404
|
|
|
{ |
405
|
|
|
return $this->callbacks; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Return's the subject's source date format to use. |
410
|
|
|
* |
411
|
|
|
* @return string The source date format |
412
|
|
|
*/ |
413
|
|
|
public function getSourceDateFormat() |
414
|
|
|
{ |
415
|
|
|
return $this->sourceDateFormat; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Set's the subject's source date format to use. |
420
|
|
|
* |
421
|
|
|
* @param string $sourceDateFormat The source date format |
422
|
|
|
* |
423
|
|
|
* @return void |
424
|
|
|
*/ |
425
|
|
|
public function setSourceDateFormat($sourceDateFormat) |
426
|
|
|
{ |
427
|
|
|
$this->sourceDateFormat = $sourceDateFormat; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Set's the source directory that has to be watched for new files. |
432
|
|
|
* |
433
|
|
|
* @param string $sourceDir The source directory |
434
|
|
|
* |
435
|
|
|
* @return void |
436
|
|
|
*/ |
437
|
|
|
public function setSourceDir($sourceDir) |
438
|
|
|
{ |
439
|
|
|
$this->sourceDir = $sourceDir; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Return's the source directory that has to be watched for new files. |
444
|
|
|
* |
445
|
|
|
* @return string The source directory |
446
|
|
|
*/ |
447
|
|
|
public function getSourceDir() |
448
|
|
|
{ |
449
|
|
|
return $this->sourceDir; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Return's the target directory with the files to be imported. |
454
|
|
|
* |
455
|
|
|
* @return string The target directory |
456
|
|
|
*/ |
457
|
|
|
public function getTargetDir() |
458
|
|
|
{ |
459
|
|
|
return $this->targetDir; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Set's the target directory with the files to be imported. |
464
|
|
|
* |
465
|
|
|
* @param string $targetDir The target directory |
466
|
|
|
* |
467
|
|
|
* @return void |
468
|
|
|
*/ |
469
|
|
|
public function setTargetDir($targetDir) |
470
|
|
|
{ |
471
|
|
|
$this->targetDir = $targetDir; |
472
|
|
|
} |
473
|
|
|
} |
474
|
|
|
|