Completed
Push — master ( 31d69b...c656e0 )
by Marcus
09:48
created

Subject::setPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\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-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Configuration;
22
23
use JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\Annotation\SerializedName;
25
use JMS\Serializer\Annotation\PostDeserialize;
26
use TechDivision\Import\Configuration\ConfigurationInterface;
27
use TechDivision\Import\Configuration\PluginConfigurationInterface;
28
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
29
use TechDivision\Import\Configuration\ListenerAwareConfigurationInterface;
30
use TechDivision\Import\Configuration\Jms\Configuration\Subject\FileResolver;
31
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter;
32
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter;
33
use TechDivision\Import\Configuration\Jms\Configuration\Subject\DateConverter;
34
use TechDivision\Import\Configuration\Jms\Configuration\Subject\NumberConverter;
35
use TechDivision\Import\Configuration\Jms\Configuration\Subject\FilesystemAdapter;
36
use TechDivision\Import\Configuration\Jms\Configuration\Subject\FileWriter;
37
38
/**
39
 * The subject configuration implementation.
40
 *
41
 * @author    Tim Wagner <[email protected]>
42
 * @copyright 2016 TechDivision GmbH <[email protected]>
43
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
44
 * @link      https://github.com/techdivision/import-configuration-jms
45
 * @link      http://www.techdivision.com
46
 */
47
class Subject implements SubjectConfigurationInterface, ListenerAwareConfigurationInterface
48
{
49
50
    /**
51
     * The trait that provides parameter handling functionality.
52
     *
53
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
54
     */
55
    use ParamsTrait;
56
57
    /**
58
     * Trait that provides CSV configuration functionality.
59
     *
60
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait
61
     */
62
    use ListenersTrait;
63
64
    /**
65
     * The subject's unique DI identifier.
66
     *
67
     * @var string
68
     * @Type("string")
69
     * @SerializedName("id")
70
     */
71
    protected $id;
72
73
    /**
74
     * The subject's name.
75
     *
76
     * @var string
77
     * @Type("string")
78
     * @SerializedName("name")
79
     */
80
    protected $name;
81
82
    /**
83
     * The array with the subject's observers.
84
     *
85
     * @var array
86
     * @Type("array")
87
     */
88
    protected $observers = array();
89
90
    /**
91
     * The array with the subject's callbacks.
92
     *
93
     * @var array
94
     * @Type("array<string, array>")
95
     */
96
    protected $callbacks = array();
97
98
    /**
99
     * The array with the subject's frontend input callbacks.
100
     *
101
     * @var array
102
     * @Type("array<string, array>")
103
     * @SerializedName("frontend-input-callbacks")
104
     */
105
    protected $frontendInputCallbacks = array();
106
107
    /**
108
     * The flag to signal that the subjects needs a OK file to be processed or not.
109
     *
110
     * @var boolean
111
     * @Type("boolean")
112
     * @SerializedName("ok-file-needed")
113
     */
114
    protected $okFileNeeded = false;
115
116
    /**
117
     *The flag to signal that the subject has to create a .imported flagfile or not.
118
     *
119
     * @var boolean
120
     * @Type("boolean")
121
     * @SerializedName("create-imported-file")
122
     */
123
    protected $createImportedFile = true;
124
125
    /**
126
     * The import adapter configuration instance.
127
     *
128
     * @var \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface
129
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter")
130
     * @SerializedName("import-adapter")
131
     */
132
    protected $importAdapter;
133
134
    /**
135
     * The export adapter configuration instance.
136
     *
137
     * @var \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface
138
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter")
139
     * @SerializedName("export-adapter")
140
     */
141
    protected $exportAdapter;
142
143
    /**
144
     * The filesystem adapter configuration instance.
145
     *
146
     * @var \TechDivision\Import\Configuration\Subject\FilesystemAdapterConfigurationInterface
147
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\FilesystemAdapter")
148
     * @SerializedName("filesystem-adapter")
149
     */
150
    protected $filesystemAdapter;
151
152
    /**
153
     * The file resolver configuration instance.
154
     *
155
     * @var \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface
156
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\FileResolver")
157
     * @SerializedName("file-resolver")
158
     */
159
    protected $fileResolver;
160
161
    /**
162
     * The file writer configuration instance.
163
     *
164
     * @var \TechDivision\Import\Configuration\Subject\FileWriterConfigurationInterface
165
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\FileWriter")
166
     * @SerializedName("file-writer")
167
     */
168
    protected $fileWriter;
169
170
    /**
171
     * The number converter configuration instance.
172
     *
173
     * @var \TechDivision\Import\Configuration\Subject\NumberConverterConfigurationInterface
174
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\NumberConverter")
175
     * @SerializedName("number-converter")
176
     */
177
    protected $numberConverter;
178
179
    /**
180
     * The date converter configuration instance.
181
     *
182
     * @var \TechDivision\Import\Configuration\Subject\DateConverterConfigurationInterface
183
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\DateConverter")
184
     * @SerializedName("date-converter")
185
     */
186
    protected $dateConverter;
187
188
    /**
189
     * The source directory that has to be watched for new files.
190
     *
191
     * @var string
192
     * @Type("string")
193
     * @SerializedName("source-dir")
194
     */
195
    protected $sourceDir;
196
197
    /**
198
     * The target directory with the files that has been imported.
199
     *
200
     * @var string
201
     * @Type("string")
202
     * @SerializedName("target-dir")
203
     */
204
    protected $targetDir;
205
206
    /**
207
     * A reference to the parent configuration instance.
208
     *
209
     * @var \TechDivision\Import\Configuration\ConfigurationInterface
210
     */
211
    protected $configuration;
212
213
    /**
214
     * The configuration of the parent plugin.
215
     *
216
     * @var \TechDivision\Import\Configuration\PluginConfigurationInterface
217
     */
218
    protected $pluginConfiguration;
219
220
    /**
221
     * Lifecycle callback that will be invoked after deserialization.
222
     *
223
     * @return void
224
     * @PostDeserialize
225
     */
226
    public function postDeserialize()
227
    {
228
229
        // set a default import adatper if none has been configured
230
        if ($this->importAdapter === null) {
231
            $this->importAdapter = new ImportAdapter();
232
        }
233
234
        // set a default export adatper if none has been configured
235
        if ($this->exportAdapter === null) {
236
            $this->exportAdapter = new ExportAdapter();
237
        }
238
239
        // set a default filesystem adatper if none has been configured
240
        if ($this->filesystemAdapter === null) {
241
            $this->filesystemAdapter = new FilesystemAdapter();
242
        }
243
244
        // set a default file resolver if none has been configured
245
        if ($this->fileResolver === null) {
246
            $this->fileResolver = new FileResolver();
247
        }
248
249
        // set a default file writer if none has been configured
250
        if ($this->fileWriter === null) {
251
            $this->fileWriter = new FileWriter();
252
        }
253
254
        // set a default number converter if none has been configured
255
        if ($this->numberConverter === null) {
256
            $this->numberConverter = new NumberConverter();
257
        }
258
259
        // set a default date converter if none has been configured
260
        if ($this->dateConverter === null) {
261
            $this->dateConverter = new DateConverter();
262
        }
263
    }
264
265
    /**
266
     * Return's the multiple field delimiter character to use, default value is comma (,).
267
     *
268
     * @return string The multiple field delimiter character
269
     */
270
    public function getMultipleFieldDelimiter()
271
    {
272
        return $this->getConfiguration()->getMultipleFieldDelimiter();
273
    }
274
275
    /**
276
     * Return's the multiple value delimiter character to use, default value is comma (|).
277
     *
278
     * @return string The multiple value delimiter character
279
     */
280
    public function getMultipleValueDelimiter()
281
    {
282
        return $this->getConfiguration()->getMultipleValueDelimiter();
283
    }
284
285
    /**
286
     * Return's the delimiter character to use, default value is comma (,).
287
     *
288
     * @return string The delimiter character
289
     */
290
    public function getDelimiter()
291
    {
292
        return $this->getConfiguration()->getDelimiter();
293
    }
294
295
    /**
296
     * The enclosure character to use, default value is double quotation (").
297
     *
298
     * @return string The enclosure character
299
     */
300
    public function getEnclosure()
301
    {
302
        return $this->getConfiguration()->getEnclosure();
303
    }
304
305
    /**
306
     * The escape character to use, default value is backslash (\).
307
     *
308
     * @return string The escape character
309
     */
310
    public function getEscape()
311
    {
312
        return $this->getConfiguration()->getEscape();
313
    }
314
315
    /**
316
     * The file encoding of the CSV source file, default value is UTF-8.
317
     *
318
     * @return string The charset used by the CSV source file
319
     */
320
    public function getFromCharset()
321
    {
322
        return $this->getConfiguration()->getFromCharset();
323
    }
324
325
    /**
326
     * The file encoding of the CSV targetfile, default value is UTF-8.
327
     *
328
     * @return string The charset used by the CSV target file
329
     */
330
    public function getToCharset()
331
    {
332
        return $this->getConfiguration()->getToCharset();
333
    }
334
335
    /**
336
     * The file mode of the CSV target file, either one of write or append, default is write.
337
     *
338
     * @return string The file mode of the CSV target file
339
     */
340
    public function getFileMode()
341
    {
342
        return $this->getConfiguration()->getFileMode();
343
    }
344
345
    /**
346
     * Queries whether or not strict mode is enabled or not, default is TRUE.
347
     *
348
     * @return boolean TRUE if strict mode is enabled, else FALSE
349
     */
350
    public function isStrictMode()
351
    {
352
        return $this->getConfiguration()->isStrictMode();
353
    }
354
355
    /**
356
     * Return's the subject's source date format to use.
357
     *
358
     * @return string The source date format
359
     */
360
    public function getSourceDateFormat()
361
    {
362
        return $this->getDateConverter()->getSourceDateFormat();
363
    }
364
365
    /**
366
     * Return's the source directory that has to be watched for new files.
367
     *
368
     * @return string The source directory
369
     */
370
    public function getSourceDir()
371
    {
372
        return $this->sourceDir ? $this->sourceDir : $this->getConfiguration()->getSourceDir();
373
    }
374
375
    /**
376
     * Return's the target directory with the files that has been imported.
377
     *
378
     * @return string The target directory
379
     */
380
    public function getTargetDir()
381
    {
382
        return $this->targetDir ? $this->targetDir : $this->getConfiguration()->getTargetDir();
383
    }
384
385
    /**
386
     * Queries whether or not debug mode is enabled or not, default is TRUE.
387
     *
388
     * @return boolean TRUE if debug mode is enabled, else FALSE
389
     */
390
    public function isDebugMode()
391
    {
392
        return $this->getConfiguration()->isDebugMode();
393
    }
394
395
    /**
396
     * Return's the subject's unique DI identifier.
397
     *
398
     * @return string The subject's unique DI identifier
399
     */
400
    public function getId()
401
    {
402
        return $this->id;
403
    }
404
405
    /**
406
     * Return's the subject's name or the ID, if the name is NOT set.
407
     *
408
     * @return string The subject's name
409
     * @see \TechDivision\Import\Configuration\SubjectConfigurationInterface::getId()
410
     */
411
    public function getName()
412
    {
413
        return $this->name ? $this->name : $this->getId();
414
    }
415
416
    /**
417
     * Set's the reference to the configuration instance.
418
     *
419
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
420
     *
421
     * @return void
422
     */
423
    public function setConfiguration(ConfigurationInterface $configuration)
424
    {
425
        $this->configuration = $configuration;
426
    }
427
428
    /**
429
     * Return's the reference to the configuration instance.
430
     *
431
     * @return \TechDivision\Import\Configuration\ConfigurationInterface The configuration instance
432
     */
433
    public function getConfiguration()
434
    {
435
        return $this->configuration;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->configuration; (TechDivision\Import\Conf...\ConfigurationInterface) is incompatible with the return type declared by the interface TechDivision\Import\Conf...rface::getConfiguration of type TechDivision\Import\ConfigurationInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
436
    }
437
438
    /**
439
     * Set's the reference to the parent plugin configuration instance.
440
     *
441
     * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The parent plugin configuration instance
442
     *
443
     * @return void
444
     */
445
    public function setPluginConfiguration(PluginConfigurationInterface $pluginConfiguration)
446
    {
447
        $this->pluginConfiguration = $pluginConfiguration;
448
    }
449
450
    /**
451
     * Return's the reference to the parent plugin configuration instance.
452
     *
453
     * @return \TechDivision\Import\Configuration\PluginConfigurationInterface The parent plugin configuration instance
454
     */
455
    public function getPluginConfiguration()
456
    {
457
        return $this->pluginConfiguration;
458
    }
459
460
    /**
461
     * Return's the prefix for the import files.
462
     *
463
     * @return string The prefix
464
     */
465
    public function getPrefix()
466
    {
467
        return $this->getFileResolver()->getPrefix();
468
    }
469
470
    /**
471
     * Return's the array with the subject's observers.
472
     *
473
     * @return array The subject's observers
474
     */
475
    public function getObservers()
476
    {
477
        return $this->observers;
478
    }
479
480
    /**
481
     * Return's the array with the subject's callbacks.
482
     *
483
     * @return array The subject's callbacks
484
     */
485
    public function getCallbacks()
486
    {
487
        return $this->callbacks;
488
    }
489
490
    /**
491
     * Return's the array with the subject's frontend input callbacks.
492
     *
493
     * @return array The subject's frontend input callbacks
494
     */
495
    public function getFrontendInputCallbacks()
496
    {
497
        return $this->frontendInputCallbacks;
498
    }
499
500
    /**
501
     * Set's the flag to signal that the an OK file is needed for the subject
502
     * to be processed.
503
     *
504
     * @param boolean $okFileNeeded TRUE if the subject needs an OK file, else FALSE
505
     *
506
     * @return void
507
     */
508
    public function setOkFileNeeded($okFileNeeded)
509
    {
510
        $this->okFileNeeded = $okFileNeeded;
511
    }
512
513
    /**
514
     * Queries whether or not that the subject needs an OK file to be processed.
515
     *
516
     * @return boolean TRUE if the subject needs an OK file, else FALSE
517
     */
518
    public function isOkFileNeeded()
519
    {
520
        return $this->okFileNeeded;
521
    }
522
523
    /**
524
     * Queries whether or not the subject should create a .imported flagfile
525
     *
526
     * @return boolean TRUE if subject has to create the .imported flagfile, else FALSE
527
     */
528
    public function isCreatingImportedFile()
529
    {
530
        return $this->createImportedFile;
531
    }
532
533
    /**
534
     * Return's the import adapter configuration instance.
535
     *
536
     * @return \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface The import adapter configuration instance
537
     */
538
    public function getImportAdapter()
539
    {
540
        return $this->importAdapter;
541
    }
542
543
    /**
544
     * Return's the export adapter configuration instance.
545
     *
546
     * @return \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface The export adapter configuration instance
547
     */
548
    public function getExportAdapter()
549
    {
550
        return $this->exportAdapter;
551
    }
552
553
    /**
554
     * Return's the filesystem adapter configuration instance.
555
     *
556
     * @return \TechDivision\Import\Configuration\Subject\FilesystemAdapterConfigurationInterface The filesystem adapter configuration instance
557
     */
558
    public function getFilesystemAdapter()
559
    {
560
        return $this->filesystemAdapter;
561
    }
562
563
    /**
564
     * Return's the file resolver configuration instance.
565
     *
566
     * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The file resolver configuration instance
567
     */
568
    public function getFileResolver()
569
    {
570
        return $this->fileResolver;
571
    }
572
573
    /**
574
     * Return's the file writer configuration instance.
575
     *
576
     * @return \TechDivision\Import\Configuration\Subject\FileWriterConfigurationInterface The file writer configuration instance
577
     */
578
    public function getFileWriter()
579
    {
580
        return $this->fileWriter;
581
    }
582
583
    /**
584
     * Return's the number converter configuration instance.
585
     *
586
     * @return \TechDivision\Import\Configuration\Subject\NumberConverterConfigurationInterface The number converter configuration instance
587
     */
588
    public function getNumberConverter()
589
    {
590
        return $this->numberConverter;
591
    }
592
593
    /**
594
     * Return's the date converter configuration instance.
595
     *
596
     * @return \TechDivision\Import\Configuration\Subject\DateConverterConfigurationInterface The date converter configuration instance
597
     */
598
    public function getDateConverter()
599
    {
600
        return $this->dateConverter;
601
    }
602
603
    /**
604
     * The array with the subject's custom header mappings.
605
     *
606
     * @return array The custom header mappings
607
     */
608
    public function getHeaderMappings()
609
    {
610
        return $this->getConfiguration()->getHeaderMappings();
611
    }
612
613
    /**
614
     * The array with the subject's custom image types.
615
     *
616
     * @return array The custom image types
617
     */
618
    public function getImageTypes()
619
    {
620
        return $this->getConfiguration()->getImageTypes();
621
    }
622
623
    /**
624
     * Return's the execution context configuration for the actualy plugin configuration.
625
     *
626
     * @return \TechDivision\Import\ExecutionContextInterface The execution context to use
627
     */
628
    public function getExecutionContext()
629
    {
630
        return $this->getPluginConfiguration()->getExecutionContext();
631
    }
632
633
    /**
634
     * Load the default values from the configuration.
635
     *
636
     * @return array The array with the default values
637
     */
638
    public function getDefaultValues()
639
    {
640
        return $this->getConfiguration()->getDefaultValues();
641
    }
642
643
    /**
644
     * Return's the full opration name, which consists of the Magento edition, the entity type code and the operation name.
645
     *
646
     * @param string $separator The separator used to seperate the elements
647
     *
648
     * @return string The full operation name
649
     */
650
    public function getFullOperationName($separator = '/')
651
    {
652
        return $this->getPluginConfiguration()->getFullOperationName();
653
    }
654
}
655