Completed
Push — master ( bcbb74...caf009 )
by Tim
16s queued 11s
created

Configuration::setEmptyAttributeValueConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Configuration
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;
22
23
use Doctrine\Common\Collections\ArrayCollection;
24
use JMS\Serializer\Annotation\Type;
25
use JMS\Serializer\Annotation\Exclude;
26
use JMS\Serializer\Annotation\Accessor;
27
use JMS\Serializer\Annotation\SerializedName;
28
use JMS\Serializer\Annotation\PostDeserialize;
29
use JMS\Serializer\Annotation\ExclusionPolicy;
30
use TechDivision\Import\Configuration\ConfigurationInterface;
31
use TechDivision\Import\Configuration\DatabaseConfigurationInterface;
32
use TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait;
33
use TechDivision\Import\Configuration\Jms\Configuration\CsvTrait;
34
use TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait;
35
use TechDivision\Import\Configuration\ListenerAwareConfigurationInterface;
36
use TechDivision\Import\Configuration\OperationConfigurationInterface;
37
38
/**
39
 * A simple JMS based 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
 * @ExclusionPolicy("none")
48
 */
49
class Configuration implements ConfigurationInterface, ListenerAwareConfigurationInterface
50
{
51
52
    /**
53
     * The default PID filename to use.
54
     *
55
     * @var string
56
     */
57
    const PID_FILENAME = 'importer.pid';
58
59
    /**
60
     * Trait that provides CSV configuration functionality.
61
     *
62
     * @var \TechDivision\Import\Configuration\Jms\Configuration\CsvTrait
63
     */
64
    use CsvTrait;
65
66
    /**
67
     * Trait that provides CSV configuration functionality.
68
     *
69
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
70
     */
71
    use ParamsTrait;
72
73
    /**
74
     * Trait that provides CSV configuration functionality.
75
     *
76
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait
77
     */
78
    use ListenersTrait;
79
80
    /**
81
     * The array with the available database types.
82
     *
83
     * @var array
84
     * @Exclude
85
     */
86
    protected $availableDatabaseTypes = array(
87
        DatabaseConfigurationInterface::TYPE_MYSQL,
88
        DatabaseConfigurationInterface::TYPE_REDIS
89
    );
90
91
    /**
92
     * The operation names to be executed.
93
     *
94
     * @var array
95
     * @Exclude
96
     */
97
    protected $operationNames = array();
98
99
    /**
100
     * Mapping for boolean values passed on the console.
101
     *
102
     * @var array
103
     * @Exclude
104
     */
105
    protected $booleanMapping = array(
106
        'true'  => true,
107
        'false' => false,
108
        '1'     => true,
109
        '0'     => false,
110
        'on'    => true,
111
        'off'   => false
112
    );
113
114
    /**
115
     * The serial that will be passed as commandline option (can not be specified in configuration file).
116
     *
117
     * @var string
118
     * @Exclude
119
     * @Accessor(setter="setSerial", getter="getSerial")
120
     */
121
    protected $serial;
122
123
    /**
124
     * The shortcut that maps the operation names that has to be executed.
125
     *
126
     * @var string
127
     * @Exclude
128
     */
129
    protected $shortcut;
130
131
    /**
132
     * The prefix for the move files subject.
133
     *
134
     * @var string
135
     * @Exclude
136
     * @SerializedName("move-files-prefix")
137
     * @Accessor(setter="setMoveFilesPrefix", getter="getMoveFilesPrefix")
138
     */
139
    protected $moveFilesPrefix;
140
141
    /**
142
     * The name of the command that has been invoked.
143
     *
144
     * @var string
145
     * @Exclude
146
     */
147
    protected $commandName;
148
149
    /**
150
     * The application's unique DI identifier.
151
     *
152
     * @var string
153
     * @Type("string")
154
     * @SerializedName("id")
155
     */
156
    protected $id;
157
158
    /**
159
     * The system name to use.
160
     *
161
     * @var string
162
     * @Type("string")
163
     * @SerializedName("system-name")
164
     * @Accessor(setter="setSystemName", getter="getSystemName")
165
     */
166
    protected $systemName;
167
168
    /**
169
     * The entity type code to use.
170
     *
171
     * @var string
172
     * @Type("string")
173
     * @SerializedName("entity-type-code")
174
     */
175
    protected $entityTypeCode;
176
177
    /**
178
     * The Magento installation directory.
179
     *
180
     * @var string
181
     * @Type("string")
182
     * @SerializedName("installation-dir")
183
     * @Accessor(setter="setInstallationDir", getter="getInstallationDir")
184
     */
185
    protected $installationDir;
186
187
    /**
188
     * The source directory that has to be watched for new files.
189
     *
190
     * @var string
191
     * @Type("string")
192
     * @SerializedName("source-dir")
193
     * @Accessor(setter="setSourceDir", getter="getSourceDir")
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
     * @Accessor(setter="setTargetDir", getter="getTargetDir")
204
     */
205
    protected $targetDir;
206
207
    /**
208
     * The Magento edition, EE or CE.
209
     *
210
     * @var string
211
     * @Type("string")
212
     * @SerializedName("magento-edition")
213
     * @Accessor(setter="setMagentoEdition", getter="getMagentoEdition")
214
     */
215
    protected $magentoEdition = 'CE';
216
217
    /**
218
     * The Magento version, e. g. 2.2.0.
219
     *
220
     * @var string
221
     * @Type("string")
222
     * @SerializedName("magento-version")
223
     * @Accessor(setter="setMagentoVersion", getter="getMagentoVersion")
224
     */
225
    protected $magentoVersion = '2.2.0';
226
227
    /**
228
     * ArrayCollection with the information of the configured databases.
229
     *
230
     * @var \Doctrine\Common\Collections\ArrayCollection
231
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Database>")
232
     */
233
    protected $databases;
234
235
    /**
236
     * ArrayCollection with the information of the configured loggers.
237
     *
238
     * @var \Doctrine\Common\Collections\ArrayCollection
239
     * @Type("ArrayCollection<string, TechDivision\Import\Configuration\Jms\Configuration\Logger>")
240
     */
241
    protected $loggers;
242
243
    /**
244
     * The subject's multiple field delimiter character for fields with multiple values, defaults to (,).
245
     *
246
     * @var string
247
     * @Type("string")
248
     * @SerializedName("multiple-field-delimiter")
249
     */
250
    protected $multipleFieldDelimiter = ',';
251
252
    /**
253
     * The subject's multiple value delimiter character for fields with multiple values, defaults to (|).
254
     *
255
     * @var string
256
     * @Type("string")
257
     * @SerializedName("multiple-value-delimiter")
258
     */
259
    protected $multipleValueDelimiter = '|';
260
261
    /**
262
     * The flag to signal that the subject has to use the strict mode or not.
263
     *
264
     * @var boolean
265
     * @Type("boolean")
266
     * @SerializedName("strict-mode")
267
     */
268
    protected $strictMode;
269
270
    /**
271
     * The flag whether or not the import artefacts have to be archived.
272
     *
273
     * @var boolean
274
     * @Type("boolean")
275
     * @SerializedName("archive-artefacts")
276
     * @Accessor(setter="setArchiveArtefacts", getter="haveArchiveArtefacts")
277
     */
278
    protected $archiveArtefacts = true;
279
280
    /**
281
     * The flag whether or not the import artefacts have to be cleared.
282
     *
283
     * @var boolean
284
     * @Type("boolean")
285
     * @SerializedName("clear-artefacts")
286
     * @Accessor(setter="setClearArtefacts", getter="haveClearArtefacts")
287
     */
288
    protected $clearArtefacts = true;
289
290
    /**
291
     * The directory where the archives will be stored.
292
     *
293
     * @var string
294
     * @Type("string")
295
     * @SerializedName("archive-dir")
296
     * @Accessor(setter="setArchiveDir", getter="getArchiveDir")
297
     */
298
    protected $archiveDir;
299
300
    /**
301
     * The flag to signal that the subject has to use the debug mode or not.
302
     *
303
     * @var boolean
304
     * @Type("boolean")
305
     * @SerializedName("debug-mode")
306
     * @Accessor(setter="setDebugMode", getter="isDebugMode")
307
     */
308
    protected $debugMode = false;
309
310
    /**
311
     * The log level to use (see Monolog documentation).
312
     *
313
     * @var string
314
     * @Type("string")
315
     * @SerializedName("log-level")
316
     * @Accessor(setter="setLogLevel", getter="getLogLevel")
317
     */
318
    protected $logLevel;
319
320
    /**
321
     * The explicit DB ID to use.
322
     *
323
     * @var string
324
     * @Type("string")
325
     * @SerializedName("use-db-id")
326
     */
327
    protected $useDbId;
328
329
    /**
330
     * The explicit PID filename to use.
331
     *
332
     * @var string
333
     * @Type("string")
334
     * @SerializedName("pid-filename")
335
     * @Accessor(setter="setPidFilename", getter="getPidFilename")
336
     */
337
    protected $pidFilename;
338
339
    /**
340
     * The collection with the paths to additional vendor directories.
341
     *
342
     * @var \Doctrine\Common\Collections\ArrayCollection
343
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\VendorDir>")
344
     * @SerializedName("additional-vendor-dirs")
345
     */
346
    protected $additionalVendorDirs;
347
348
    /**
349
     * ArrayCollection with the information of the configured operations.
350
     *
351
     * @var array
352
     * @Type("array<string, array<string, ArrayCollection<string, TechDivision\Import\Configuration\Jms\Configuration\Operation>>>")
353
     */
354
    protected $operations = array();
355
356
    /**
357
     * The array with the Magento Edition specific extension libraries.
358
     *
359
     * @var array
360
     * @Type("array")
361
     * @SerializedName("extension-libraries")
362
     */
363
    protected $extensionLibraries = array();
364
365
    /**
366
     * The array with the custom header mappings.
367
     *
368
     * @var array
369
     * @SerializedName("header-mappings")
370
     * @Type("array<string, array<string, string>>")
371
     */
372
    protected $headerMappings = array();
373
374
    /**
375
     * The array with the custom image types.
376
     *
377
     * @var array
378
     * @Type("array")
379
     * @SerializedName("image-types")
380
     */
381
    protected $imageTypes = array();
382
383
    /**
384
     * The flag to signal that the import should be wrapped within a single transation or not.
385
     *
386
     * @var boolean
387
     * @Type("boolean")
388
     * @SerializedName("single-transaction")
389
     * @Accessor(setter="setSingleTransaction", getter="isSingleTransaction")
390
     */
391
    protected $singleTransaction = false;
392
393
    /**
394
     * The flag to signal that the cache should be enabled or not.
395
     *
396
     * @var boolean
397
     * @Type("boolean")
398
     * @SerializedName("cache-enabled")
399
     * @Accessor(setter="setCacheEnabled", getter="isCacheEnabled")
400
     */
401
    protected $cacheEnabled = false;
402
403
    /**
404
     * ArrayCollection with the information of the configured aliases.
405
     *
406
     * @var \Doctrine\Common\Collections\ArrayCollection
407
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Alias>")
408
     */
409
    protected $aliases;
410
411
    /**
412
     * ArrayCollection with the information of the configured caches.
413
     *
414
     * @var \Doctrine\Common\Collections\ArrayCollection
415
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Cache>")
416
     */
417
    protected $caches;
418
419
    /**
420
     * The array with the shortcuts.
421
     *
422
     * @var array
423
     * @Type("array<string, array<string, array>>")
424
     * @SerializedName("shortcuts")
425
     */
426
    protected $shortcuts = array();
427
428
    /**
429
     * The username to save the import history with.
430
     *
431
     * @var string
432
     * @Type("string")
433
     */
434
    protected $username;
435
436
    /**
437
     * The array with the finder mappings.
438
     *
439
     * @var array
440
     * @SerializedName("finder-mappings")
441
     * @Type("array<string, array<string, string>>")
442
     * @Accessor(setter="setFinderMappings", getter="getFinderMappings")
443
     */
444
    protected $finderMappings = array();
445
446
    /**
447
     * The array with the default values.
448
     *
449
     * @var array
450
     * @SerializedName("default-values")
451
     * @Type("array<string, array<string, string>>")
452
     * @Accessor(setter="setDefaultValues", getter="getDefaultValues")
453
     */
454
    protected $defaultValues = array();
455
456
    /**
457
     * The value to define empty values for attributes
458
     *
459
     * @var string
460
     * @Type("string")
461
     * @SerializedName("empty-attribute-value-constant")
462
     * @Accessor(setter="setEmptyAttributeValueConstant", getter="getEmptyAttributeValueConstant")
463
     */
464
    protected $emptyAttributeValueConstant = '';
465
466
    /**
467
     * Lifecycle callback that will be invoked after deserialization.
468
     *
469
     * @return void
470
     * @PostDeserialize
471
     */
472
    public function postDeserialize()
473
    {
474
475
        // create an empty collection if no loggers has been specified
476
        if ($this->loggers === null) {
477
            $this->loggers = new ArrayCollection();
478
        }
479
480
        // create an empty collection if no caches has been specified
481
        if ($this->caches === null) {
482
            $this->caches = new ArrayCollection();
483
        }
484
485
        // create an empty collection if no aliases has been specified
486
        if ($this->aliases === null) {
487
            $this->aliases = new ArrayCollection();
488
        }
489
490
        // create an empty collection if no databases has been specified
491
        if ($this->databases === null) {
492
            $this->databases = new ArrayCollection();
493
        }
494
495
        // create an empty collection if no additional venor directories has been specified
496
        if ($this->additionalVendorDirs === null) {
497
            $this->additionalVendorDirs = new ArrayCollection();
498
        }
499
    }
500
501
    /**
502
     * Map's the passed value to a boolean.
503
     *
504
     * @param string $value The value to map
505
     *
506
     * @return boolean The mapped value
507
     * @throws \Exception Is thrown, if the value can't be mapped
508
     */
509
    public function mapBoolean($value)
510
    {
511
512
        // do nothing, because passed value is already a boolean
513
        if (is_bool($value)) {
514
            return $value;
515
        }
516
517
        // try to map the passed value to a boolean
518
        if (isset($this->booleanMapping[$val = strtolower($value)])) {
519
            return $this->booleanMapping[$val];
520
        }
521
522
        // throw an exception if we can't convert the passed value
523
        throw new \Exception(sprintf('Can\'t convert %s to boolean', $value));
524
    }
525
526
    /**
527
     * Return's the application's unique DI identifier.
528
     *
529
     * @return string The application's unique DI identifier
530
     */
531
    public function getId()
532
    {
533
        return $this->id;
534
    }
535
536
    /**
537
     * Add's the operation with the passed name ot the operations that has to be executed.
538
     *
539
     * If the operation name has already been added, it'll not be added again.
540
     *
541
     * @param string  $operationName The operation to be executed
542
     * @param boolean $prepend       TRUE if the operation name should be prepended, else FALSE
543
     *
544
     * @return void
545
     */
546
    public function addOperationName($operationName, $prepend = false)
547
    {
548
549
        // do nothing if the operation has already been added
550
        if (in_array($operationName, $this->operationNames)) {
551
            return;
552
        }
553
554
        // add the operation otherwise
555
        $prepend ? array_unshift($this->operationNames, $operationName) : array_push($this->operationNames, $operationName);
556
    }
557
558
    /**
559
     * Return's the operation names that has to be executed.
560
     *
561
     * @param array $operationNames The operation names that has to be executed
562
     *
563
     * @return void
564
     */
565
    public function setOperationNames(array $operationNames)
566
    {
567
        return $this->operationNames = $operationNames;
568
    }
569
570
    /**
571
     * Return's the operation names that has to be executed.
572
     *
573
     * @return array The operation names that has to be executed
574
     */
575
    public function getOperationNames()
576
    {
577
        return $this->operationNames;
578
    }
579
580
    /**
581
     * Queries whether or not the passed operation has to be exceuted or not.
582
     *
583
     * @param \TechDivision\Import\Configuration\OperationConfigurationInterface $operation The operation to query for
584
     *
585
     * @return boolean TRUE if the operation has to be executed, else FALSE
586
     */
587
    public function inOperationNames(OperationConfigurationInterface $operation)
588
    {
589
        return in_array($operation->getName(), $this->getOperationNames());
590
    }
591
592
    /**
593
     * Set's the Magento installation directory.
594
     *
595
     * @param string $installationDir The Magento installation directory
596
     *
597
     * @return void
598
     */
599
    public function setInstallationDir($installationDir)
600
    {
601
        $this->installationDir = $installationDir;
602
    }
603
604
    /**
605
     * Return's the Magento installation directory.
606
     *
607
     * @return string The Magento installation directory
608
     */
609
    public function getInstallationDir()
610
    {
611
        return $this->installationDir;
612
    }
613
614
    /**
615
     * Set's the source directory that has to be watched for new files.
616
     *
617
     * @param string $sourceDir The source directory
618
     *
619
     * @return void
620
     */
621
    public function setSourceDir($sourceDir)
622
    {
623
        $this->sourceDir = $sourceDir;
624
    }
625
626
    /**
627
     * Return's the source directory that has to be watched for new files.
628
     *
629
     * @return string The source directory
630
     */
631
    public function getSourceDir()
632
    {
633
        return $this->sourceDir;
634
    }
635
636
    /**
637
     * Set's the target directory with the files that has been imported.
638
     *
639
     * @param string $targetDir The target directory
640
     *
641
     * @return void
642
     */
643
    public function setTargetDir($targetDir)
644
    {
645
        $this->targetDir = $targetDir;
646
    }
647
648
    /**
649
     * Return's the target directory with the files that has been imported.
650
     *
651
     * @return string The target directory
652
     */
653
    public function getTargetDir()
654
    {
655
        return $this->targetDir;
656
    }
657
658
    /**
659
     * Set's the Magento edition, EE or CE.
660
     *
661
     * @param string $magentoEdition The Magento edition
662
     *
663
     * @return void
664
     */
665
    public function setMagentoEdition($magentoEdition)
666
    {
667
        $this->magentoEdition = $magentoEdition;
668
    }
669
670
    /**
671
     * Return's the Magento edition, EE or CE.
672
     *
673
     * @return string The Magento edition
674
     */
675
    public function getMagentoEdition()
676
    {
677
        return $this->magentoEdition;
678
    }
679
680
    /**
681
     * Return's the Magento version, e. g. 2.1.0.
682
     *
683
     * @param string $magentoVersion The Magento version
684
     *
685
     * @return void
686
     */
687
    public function setMagentoVersion($magentoVersion)
688
    {
689
        $this->magentoVersion = $magentoVersion;
690
    }
691
692
    /**
693
     * Return's the Magento version, e. g. 2.1.0.
694
     *
695
     * @return string The Magento version
696
     */
697
    public function getMagentoVersion()
698
    {
699
        return $this->magentoVersion;
700
    }
701
702
    /**
703
     * Return's the entity type code to be used.
704
     *
705
     * @return string The entity type code to be used
706
     */
707
    public function getEntityTypeCode()
708
    {
709
        return $this->entityTypeCode;
710
    }
711
712
    /**
713
     * Set's the entity type code to be used.
714
     *
715
     * @param string $entityTypeCode The entity type code
716
     *
717
     * @return void
718
     */
719
    public function setEntityTypeCode($entityTypeCode)
720
    {
721
        $this->entityTypeCode = $entityTypeCode;
722
    }
723
724
    /**
725
     * Return's the multiple field delimiter character to use, default value is comma (,).
726
     *
727
     * @return string The multiple field delimiter character
728
     */
729
    public function getMultipleFieldDelimiter()
730
    {
731
        return $this->multipleFieldDelimiter;
732
    }
733
734
    /**
735
     * Return's the multiple value delimiter character to use, default value is comma (|).
736
     *
737
     * @return string The multiple value delimiter character
738
     */
739
    public function getMultipleValueDelimiter()
740
    {
741
        return $this->multipleValueDelimiter;
742
    }
743
744
    /**
745
     * Queries whether or not strict mode is enabled or not, default is TRUE.
746
     *
747
     * @return boolean TRUE if strict mode is enabled, else FALSE
748
     */
749
    public function isStrictMode()
750
    {
751
        return $this->strictMode;
752
    }
753
754
    /**
755
     * Remove's all configured database configuration.
756
     *
757
     * @return void
758
     */
759
    public function clearDatabases()
760
    {
761
        $this->databases->clear();
762
    }
763
764
    /**
765
     * Add's the passed database configuration.
766
     *
767
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
768
     *
769
     * @return void
770
     */
771
    public function addDatabase(DatabaseConfigurationInterface $database)
772
    {
773
        $this->databases->add($database);
774
    }
775
776
    /**
777
     * Return's the number database configurations.
778
     *
779
     * @return integer The number of database configurations
780
     */
781
    public function countDatabases()
782
    {
783
        return $this->databases->count();
784
    }
785
786
    /**
787
     * Return's the database configuration with the passed ID.
788
     *
789
     * @param string $id The ID of the database connection to return
790
     *
791
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
792
     * @throws \Exception Is thrown, if no database configuration is available
793
     */
794
    public function getDatabaseById($id)
795
    {
796
797
        // iterate over the configured databases and return the one with the passed ID
798
        /** @var \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database */
799
        foreach ($this->databases as $database) {
800
            if ($database->getId() === $id && $this->isValidDatabaseType($database)) {
801
                return $database;
802
            }
803
        }
804
805
        // throw an exception, if the database with the passed ID is NOT configured
806
        throw new \Exception(sprintf('Database with ID %s can not be found or has an invalid type', $id));
807
    }
808
809
    /**
810
     * Return's the databases for the given type.
811
     *
812
     * @param string $type The database type to return the configurations for
813
     *
814
     * @return \Doctrine\Common\Collections\Collection The collection with the database configurations
815
     */
816
    public function getDatabasesByType($type)
817
    {
818
819
        // initialize the collection for the database configurations
820
        $databases = new ArrayCollection();
821
822
        // iterate over the configured databases and return the one with the passed ID
823
        /** @var \TechDivision\Import\Configuration\DatabaseConfigurationInterface  $database */
824
        foreach ($this->databases as $database) {
825
            if ($database->getType() === $type && $this->isValidDatabaseType($database)) {
826
                $databases->add($database);
827
            }
828
        }
829
830
        // return the database configurations
831
        return $databases;
832
    }
833
834
    /**
835
     * Query's whether or not the passed database configuration has a valid type.
836
     *
837
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
838
     *
839
     * @return boolean TRUE if the passed database configuration has a valid type, else FALSE
840
     */
841
    protected function isValidDatabaseType(DatabaseConfigurationInterface $database)
842
    {
843
        return in_array(strtolower($database->getType()), $this->availableDatabaseTypes);
844
    }
845
846
    /**
847
     * Return's the database configuration.
848
     *
849
     * If an explicit DB ID is specified, the method tries to return the database with this ID. If
850
     * the database configuration is NOT available, an execption is thrown.
851
     *
852
     * If no explicit DB ID is specified, the method tries to return the default database configuration,
853
     * if not available the first one.
854
     *
855
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
856
     * @throws \Exception Is thrown, if no database configuration is available
857
     */
858
    public function getDatabase()
859
    {
860
861
        // if a DB ID has been set, try to load the database
862
        if ($useDbId = $this->getUseDbId()) {
863
            return $this->getDatabaseById($useDbId);
864
        }
865
866
        // iterate over the configured databases and try return the default database
867
        /** @var \TechDivision\Import\Configuration\DatabaseConfigurationInterface  $database */
868
        foreach ($this->databases as $database) {
869
            if ($database->isDefault() && $this->isValidDatabaseType($database)) {
870
                return $database;
871
            }
872
        }
873
874
        // try to return the first database configurtion
875
        if ($this->databases->count() > 0) {
876
            return $this->databases->first();
877
        }
878
879
        // throw an exception, if no database configuration is available
880
        throw new \Exception('There is no database configuration available');
881
    }
882
883
    /**
884
     * Return's the array with the configured operations.
885
     *
886
     * @return array The array with the operations
887
     */
888
    public function getOperations()
889
    {
890
        return $this->operations;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->operations; (array) is incompatible with the return type declared by the interface TechDivision\Import\Conf...nterface::getOperations of type Doctrine\Common\Collections\ArrayCollection.

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...
891
    }
892
893
    /**
894
     * Return's the ArrayCollection with the configured shortcuts.
895
     *
896
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the shortcuts
897
     */
898
    public function getShortcuts()
899
    {
900
        return $this->shortcuts;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->shortcuts; (array) is incompatible with the return type declared by the interface TechDivision\Import\Conf...Interface::getShortcuts of type Doctrine\Common\Collections\ArrayCollection.

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...
901
    }
902
903
    /**
904
     * Return's the ArrayCollection with the configured loggers.
905
     *
906
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the loggers
907
     */
908
    public function getLoggers()
909
    {
910
        return $this->loggers;
911
    }
912
913
    /**
914
     * Set's the flag that import artefacts have to be archived or not.
915
     *
916
     * @param mixed $archiveArtefacts TRUE if artefacts have to be archived, else FALSE
917
     *
918
     * @return void
919
     */
920
    public function setArchiveArtefacts($archiveArtefacts)
921
    {
922
        $this->archiveArtefacts = $this->mapBoolean($archiveArtefacts);
923
    }
924
925
    /**
926
     * Return's the TRUE if the import artefacts have to be archived.
927
     *
928
     * @return boolean TRUE if the import artefacts have to be archived
929
     */
930
    public function haveArchiveArtefacts()
931
    {
932
        return $this->archiveArtefacts;
933
    }
934
935
    /**
936
     * Set's the flag that import artefacts have to be cleared or not.
937
     *
938
     * @param mixed $clearArtefacts TRUE if artefacts have to be cleared, else FALSE
939
     *
940
     * @return void
941
     */
942
    public function setClearArtefacts($clearArtefacts)
943
    {
944
        $this->clearArtefacts = $this->mapBoolean($clearArtefacts);
945
    }
946
947
    /**
948
     * Return's the TRUE if the import artefacts have to be cleared after the import process.
949
     *
950
     * @return boolean TRUE if the import artefacts have to be cleared
951
     */
952
    public function haveClearArtefacts()
953
    {
954
        return $this->clearArtefacts;
955
    }
956
957
    /**
958
     * The directory where the archives will be stored.
959
     *
960
     * @param string $archiveDir The archive directory
961
     *
962
     * @return void
963
     */
964
    public function setArchiveDir($archiveDir)
965
    {
966
        $this->archiveDir = $archiveDir;
967
    }
968
969
    /**
970
     * The directory where the archives will be stored.
971
     *
972
     * @return string The archive directory
973
     */
974
    public function getArchiveDir()
975
    {
976
        return $this->archiveDir;
977
    }
978
979
    /**
980
     * Set's the debug mode.
981
     *
982
     * @param mixed $debugMode TRUE if debug mode is enabled, else FALSE
983
     *
984
     * @return void
985
     */
986
    public function setDebugMode($debugMode)
987
    {
988
        $this->debugMode = $this->mapBoolean($debugMode);
989
    }
990
991
    /**
992
     * Queries whether or not debug mode is enabled or not, default is TRUE.
993
     *
994
     * @return boolean TRUE if debug mode is enabled, else FALSE
995
     */
996
    public function isDebugMode()
997
    {
998
        return $this->debugMode;
999
    }
1000
1001
    /**
1002
     * Set's the log level to use.
1003
     *
1004
     * @param string $logLevel The log level to use
1005
     *
1006
     * @return void
1007
     */
1008
    public function setLogLevel($logLevel)
1009
    {
1010
        $this->logLevel = $logLevel;
1011
    }
1012
1013
    /**
1014
     * Return's the log level to use.
1015
     *
1016
     * @return string The log level to use
1017
     */
1018
    public function getLogLevel()
1019
    {
1020
        return $this->logLevel;
1021
    }
1022
1023
    /**
1024
     * Set's the explicit DB ID to use.
1025
     *
1026
     * @param string $useDbId The explicit DB ID to use
1027
     *
1028
     * @return void
1029
     */
1030
    public function setUseDbId($useDbId)
1031
    {
1032
        $this->useDbId = $useDbId;
1033
    }
1034
1035
    /**
1036
     * Return's the explicit DB ID to use.
1037
     *
1038
     * @return string The explicit DB ID to use
1039
     */
1040
    public function getUseDbId()
1041
    {
1042
        return $this->useDbId;
1043
    }
1044
1045
    /**
1046
     * Set's the PID filename to use.
1047
     *
1048
     * @param string $pidFilename The PID filename to use
1049
     *
1050
     * @return void
1051
     */
1052
    public function setPidFilename($pidFilename)
1053
    {
1054
        $this->pidFilename = $pidFilename;
1055
    }
1056
1057
    /**
1058
     * Return's the PID filename to use.
1059
     *
1060
     * @return string The PID filename to use
1061
     */
1062
    public function getPidFilename()
1063
    {
1064
        return $this->pidFilename;
1065
    }
1066
1067
    /**
1068
     * Set's the systemm name to be used.
1069
     *
1070
     * @param string $systemName The system name to be used
1071
     *
1072
     * @return void
1073
     */
1074
    public function setSystemName($systemName)
1075
    {
1076
        $this->systemName = $systemName;
1077
    }
1078
1079
    /**
1080
     * Return's the systemm name to be used.
1081
     *
1082
     * @return string The system name to be used
1083
     */
1084
    public function getSystemName()
1085
    {
1086
        return $this->systemName;
1087
    }
1088
1089
    /**
1090
     * Set's the collection with the path of the Magento Edition specific extension libraries.
1091
     *
1092
     * @param array $extensionLibraries The paths of the Magento Edition specific extension libraries
1093
     *
1094
     * @return void
1095
     */
1096
    public function setExtensionLibraries(array $extensionLibraries)
1097
    {
1098
        $this->extensionLibraries = $extensionLibraries;
1099
    }
1100
1101
    /**
1102
     * Return's an array with the path of the Magento Edition specific extension libraries.
1103
     *
1104
     * @return array The paths of the Magento Edition specific extension libraries
1105
     */
1106
    public function getExtensionLibraries()
1107
    {
1108
        return $this->extensionLibraries;
1109
    }
1110
1111
    /**
1112
     * Return's a collection with the path to additional vendor directories.
1113
     *
1114
     * @return \Doctrine\Common\Collections\ArrayCollection The paths to additional vendor directories
1115
     */
1116
    public function getAdditionalVendorDirs()
1117
    {
1118
        return $this->additionalVendorDirs;
1119
    }
1120
1121
    /**
1122
     * The array with the subject's custom header mappings.
1123
     *
1124
     * @return array The custom header mappings
1125
     */
1126
    public function getHeaderMappings()
1127
    {
1128
        return $this->headerMappings;
1129
    }
1130
1131
    /**
1132
     * The array with the subject's custom image types.
1133
     *
1134
     * @return array The custom image types
1135
     */
1136
    public function getImageTypes()
1137
    {
1138
        return $this->imageTypes;
1139
    }
1140
1141
    /**
1142
     * Set's the flag that decides whether or not the import should be wrapped within a single transaction.
1143
     *
1144
     * @param mixed $singleTransaction TRUE if the import should be wrapped in a single transation, else FALSE
1145
     *
1146
     * @return void
1147
     */
1148
    public function setSingleTransaction($singleTransaction)
1149
    {
1150
        $this->singleTransaction = $this->mapBoolean($singleTransaction);
1151
    }
1152
1153
    /**
1154
     * Whether or not the import should be wrapped within a single transation.
1155
     *
1156
     * @return boolean TRUE if the import should be wrapped in a single transation, else FALSE
1157
     */
1158
    public function isSingleTransaction()
1159
    {
1160
        return $this->singleTransaction;
1161
    }
1162
1163
    /**
1164
     * Set's the flag that decides whether or not the the cache has been enabled.
1165
     *
1166
     * @param mixed $cacheEnabled TRUE if the cache has been enabled, else FALSE
1167
     *
1168
     * @return void
1169
     */
1170
    public function setCacheEnabled($cacheEnabled)
1171
    {
1172
        $this->cacheEnabled = $this->mapBoolean($cacheEnabled);
1173
    }
1174
1175
    /**
1176
     * Whether or not the cache functionality should be enabled.
1177
     *
1178
     * @return boolean TRUE if the cache has to be enabled, else FALSE
1179
     */
1180
    public function isCacheEnabled()
1181
    {
1182
        return $this->cacheEnabled;
1183
    }
1184
1185
    /**
1186
     * Set's the passed serial from the commandline to the configuration.
1187
     *
1188
     * @param string $serial The serial from the commandline
1189
     *
1190
     * @return void
1191
     */
1192
    public function setSerial($serial)
1193
    {
1194
        $this->serial = $serial;
1195
    }
1196
1197
    /**
1198
     * Return's the serial from the commandline.
1199
     *
1200
     * @return string The serial
1201
     */
1202
    public function getSerial()
1203
    {
1204
        return $this->serial;
1205
    }
1206
1207
    /**
1208
     * Return's the configuration for the caches.
1209
     *
1210
     * @return \Doctrine\Common\Collections\ArrayCollection The cache configurations
1211
     */
1212
    public function getCaches()
1213
    {
1214
1215
        // iterate over the caches and set the parent configuration instance
1216
        foreach ($this->caches as $cache) {
1217
            $cache->setConfiguration($this);
1218
        }
1219
1220
        // return the array with the caches
1221
        return $this->caches;
1222
    }
1223
1224
    /**
1225
     * Return's the cache configuration for the passed type.
1226
     *
1227
     * @param string $type The cache type to return the configuation for
1228
     *
1229
     * @return \TechDivision\Import\Configuration\CacheConfigurationInterface The cache configuration
1230
     */
1231
    public function getCacheByType($type)
1232
    {
1233
1234
        // load the available cache configurations
1235
        $caches = $this->getCaches();
1236
1237
        // try to load the cache for the passed type
1238
        /** @var \TechDivision\Import\Configuration\CacheConfigurationInterface $cache */
1239
        foreach ($caches as $cache) {
1240
            if ($cache->getType() === $type) {
1241
                return $cache;
1242
            }
1243
        }
1244
    }
1245
1246
    /**
1247
     * Return's the alias configuration.
1248
     *
1249
     * @return \Doctrine\Common\Collections\ArrayCollection The alias configuration
1250
     */
1251
    public function getAliases()
1252
    {
1253
        return $this->aliases;
1254
    }
1255
1256
    /**
1257
     * Set's the prefix for the move files subject.
1258
     *
1259
     * @param string $moveFilesPrefix The prefix for the move files subject
1260
     *
1261
     * @return void
1262
     */
1263
    public function setMoveFilesPrefix($moveFilesPrefix)
1264
    {
1265
        $this->moveFilesPrefix = $moveFilesPrefix;
1266
    }
1267
1268
    /**
1269
     * Return's the prefix for the move files subject.
1270
     *
1271
     * @return string The prefix for the move files subject
1272
     */
1273
    public function getMoveFilesPrefix()
1274
    {
1275
        return $this->moveFilesPrefix;
1276
    }
1277
1278
    /**
1279
     * Set's the shortcut that maps the operation names that has to be executed.
1280
     *
1281
     * @param string $shortcut The shortcut
1282
     *
1283
     * @return void
1284
     */
1285
    public function setShortcut($shortcut)
1286
    {
1287
        $this->shortcut = $shortcut;
1288
    }
1289
1290
    /**
1291
     * Return's the shortcut that maps the operation names that has to be executed.
1292
     *
1293
     * @return string The shortcut
1294
     */
1295
    public function getShortcut()
1296
    {
1297
        return $this->shortcut;
1298
    }
1299
1300
    /**
1301
     * Set's the name of the command that has been invoked.
1302
     *
1303
     * @param string $commandName The command name
1304
     *
1305
     * @return void
1306
     */
1307
    public function setCommandName($commandName)
1308
    {
1309
        $this->commandName = $commandName;
1310
    }
1311
1312
    /**
1313
     * Return's the name of the command that has been invoked.
1314
     *
1315
     * @return string The command name
1316
     */
1317
    public function getCommandName()
1318
    {
1319
        return $this->commandName;
1320
    }
1321
1322
    /**
1323
     * Set's the username to save the import history with.
1324
     *
1325
     * @param string $username The username
1326
     *
1327
     * @return void
1328
     */
1329
    public function setUsername($username)
1330
    {
1331
        $this->username = $username;
1332
    }
1333
1334
    /**
1335
     * Return's the username to save the import history with.
1336
     *
1337
     * @return string The username
1338
     */
1339
    public function getUsername()
1340
    {
1341
        return $this->username;
1342
    }
1343
1344
    /**
1345
     * Set's the array with the finder mappings.
1346
     *
1347
     * @param array $finderMappings The finder mappings
1348
     *
1349
     * @return void
1350
     */
1351
    public function setFinderMappings(array $finderMappings)
1352
    {
1353
1354
        // convert the finder mappings keys, which are constants, to their values
1355
        foreach ($finderMappings as $entityTypeCode => $mappings) {
1356
            foreach ($mappings as $key => $value) {
1357
                $this->finderMappings[$entityTypeCode][defined($key) ? constant($key) : $key] = $value;
1358
            }
1359
        }
1360
    }
1361
1362
    /**
1363
     * Return's the array with the finder mappings.
1364
     *
1365
     * @return array The finder mappings
1366
     */
1367
    public function getFinderMappings()
1368
    {
1369
        return $this->finderMappings;
1370
    }
1371
1372
    /**
1373
     * Return's the mapped finder for the passed key.
1374
     *
1375
     * @param string $key The key of the finder to map
1376
     *
1377
     * @return string The mapped finder name
1378
     * @throws \InvalidArgumentException Is thrown if the mapping with passed key can not be resolved
1379
     */
1380
    public function getFinderMappingByKey($key)
1381
    {
1382
1383
        // flatten the array, because we don't handle the entity type code yet
1384
        $finderMappings = array_reduce($this->finderMappings, function ($carry, $item) {
1385
            return array_replace($carry, $item);
1386
        }, array());
1387
1388
        // try to resolve the mapping for the finder with the passed key
1389
        if (isset($finderMappings[$key])) {
1390
            return $finderMappings[$key];
1391
        }
1392
1393
        // throw an exception otherwise
1394
        throw new \InvalidArgumentException(
1395
            sprintf('Can\'t load mapping for finder with key "%s"', $key)
1396
        );
1397
    }
1398
1399
    /**
1400
     * Sets the default values from the configuration.
1401
     *
1402
     * @param array $defaultValues The array with the default values
1403
     *
1404
     * @return void
1405
     */
1406
    public function setDefaultValues(array $defaultValues)
1407
    {
1408
        $this->defaultValues = $defaultValues;
1409
    }
1410
1411
    /**
1412
     * Load the default values from the configuration.
1413
     *
1414
     * @return array The array with the default values
1415
     */
1416
    public function getDefaultValues()
1417
    {
1418
        return $this->defaultValues;
1419
    }
1420
1421
    /**
1422
     * Return's an unique array with the prefixes of all configured subjects.
1423
     *
1424
     * @param array $ignore An array with prefixes that has to be ignored
1425
     *
1426
     * @return array An array with the available prefixes
1427
     */
1428
    public function getPrefixes($ignore = array('.*'))
1429
    {
1430
1431
        // initialize the array for the prefixes
1432
        $prefixes = array();
1433
1434
        foreach ($this->getOperations() as $operation) {
1435
            foreach ($operation as $entityTypes) {
1436
                foreach ($entityTypes as $operationConfiguration) {
1437
                    foreach ($operationConfiguration->getPlugins() as $plugin) {
1438
                        foreach ($plugin->getSubjects() as $subject) {
1439
                            // ignore the prefix, if it has already been added or it has to be ignored
1440
                            if (in_array($prefix = $subject->getPrefix(), $prefixes, true) ||
1441
                                in_array($prefix, $ignore, true)
1442
                            ) {
1443
                                continue;
1444
                            }
1445
1446
                            // add the prefix to the list
1447
                            $prefixes[] = $prefix;
1448
                        }
1449
                    }
1450
                }
1451
            }
1452
        }
1453
1454
        // return the array with the unique prefixes
1455
        return $prefixes;
1456
    }
1457
1458
    /**
1459
     * Return's an array with the subjects which prefix is NOT in the passed
1460
     * array of blacklisted prefixes and that matches the filters.
1461
     *
1462
     * @param callable[] $filters An array with filters to filter the subjects that has to be returned
1463
     *
1464
     * @return array An array with the matching subjects
1465
     */
1466
    public function getSubjects(array $filters = array())
1467
    {
1468
1469
        // initialize the array for the prefixes
1470
        $subjects = array();
1471
1472
        // iterate over all configured subjects
1473
        foreach ($this->getOperations() as $operation) {
1474
            foreach ($operation as $entityTypes) {
1475
                foreach ($entityTypes as $operationConfiguration) {
1476
                    foreach ($operationConfiguration->getPlugins() as $plugin) {
1477
                        /** @var \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
1478
                        foreach ($plugin->getSubjects() as $subject) {
1479
                            $subjects[] = $subject;
1480
                        }
1481
                    }
1482
                }
1483
            }
1484
        }
1485
1486
        // filter the subjects
1487
        foreach ($filters as $filter) {
1488
            $subjects = array_filter($subjects, $filter);
1489
        }
1490
1491
        // return the array with the filtered subjects
1492
        return $subjects;
1493
    }
1494
1495
    /**
1496
     * Get the definition from an empty value
1497
     *
1498
     * @return string A string with constant for empty attribute value
1499
     */
1500
    public function getEmptyAttributeValueConstant()
1501
    {
1502
        return empty($this->emptyAttributeValueConstant) ? "__EMPTY__VALUE__" : $this->emptyAttributeValueConstant;
1503
    }
1504
1505
    /**
1506
     * Set the definition from an empty value
1507
     *
1508
     * @param string $emptyAttributeValueConstant give the definition for an emppty value
1509
     *
1510
     * @return void
1511
     */
1512
    public function setEmptyAttributeValueConstant($emptyAttributeValueConstant)
1513
    {
1514
        $this->emptyAttributeValueConstant = $emptyAttributeValueConstant;
1515
    }
1516
}
1517