Completed
Push — 15.x ( 2d1ea5...d23069 )
by Tim
01:41
created

Configuration::postDeserialize()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 16
nop 0
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 Psr\Log\LogLevel;
24
use Doctrine\Common\Collections\ArrayCollection;
25
use JMS\Serializer\Annotation\Type;
26
use JMS\Serializer\Annotation\Exclude;
27
use JMS\Serializer\Annotation\Accessor;
28
use JMS\Serializer\Annotation\SerializedName;
29
use JMS\Serializer\Annotation\PostDeserialize;
30
use JMS\Serializer\Annotation\ExclusionPolicy;
31
use TechDivision\Import\ConfigurationInterface;
32
use TechDivision\Import\Configuration\DatabaseConfigurationInterface;
33
use TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait;
34
use TechDivision\Import\Configuration\Jms\Configuration\CsvTrait;
35
use TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait;
36
use TechDivision\Import\Configuration\ListenerAwareConfigurationInterface;
37
use TechDivision\Import\Configuration\OperationConfigurationInterface;
38
39
/**
40
 * A simple JMS based configuration implementation.
41
 *
42
 * @author    Tim Wagner <[email protected]>
43
 * @copyright 2016 TechDivision GmbH <[email protected]>
44
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
45
 * @link      https://github.com/techdivision/import-configuration-jms
46
 * @link      http://www.techdivision.com
47
 *
48
 * @ExclusionPolicy("none")
49
 */
50
class Configuration implements ConfigurationInterface, ListenerAwareConfigurationInterface
51
{
52
53
    /**
54
     * The default PID filename to use.
55
     *
56
     * @var string
57
     */
58
    const PID_FILENAME = 'importer.pid';
59
60
    /**
61
     * Trait that provides CSV configuration functionality.
62
     *
63
     * @var \TechDivision\Import\Configuration\Jms\Configuration\CsvTrait
64
     */
65
    use CsvTrait;
66
67
    /**
68
     * Trait that provides CSV configuration functionality.
69
     *
70
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
71
     */
72
    use ParamsTrait;
73
74
    /**
75
     * Trait that provides CSV configuration functionality.
76
     *
77
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait
78
     */
79
    use ListenersTrait;
80
81
    /**
82
     * The array with the available database types.
83
     *
84
     * @var array
85
     * @Exclude
86
     */
87
    protected $availableDatabaseTypes = array(
88
        DatabaseConfigurationInterface::TYPE_MYSQL,
89
        DatabaseConfigurationInterface::TYPE_REDIS
90
    );
91
92
    /**
93
     * The operation names to be executed.
94
     *
95
     * @var array
96
     * @Exclude
97
     */
98
    protected $operationNames = array();
99
100
    /**
101
     * Mapping for boolean values passed on the console.
102
     *
103
     * @var array
104
     * @Exclude
105
     */
106
    protected $booleanMapping = array(
107
        'true'  => true,
108
        'false' => false,
109
        '1'     => true,
110
        '0'     => false,
111
        'on'    => true,
112
        'off'   => false
113
    );
114
115
    /**
116
     * The serial that will be passed as commandline option (can not be specified in configuration file).
117
     *
118
     * @var string
119
     * @Exclude
120
     * @Accessor(setter="setSerial", getter="getSerial")
121
     */
122
    protected $serial;
123
124
    /**
125
     * The shortcut that maps the operation names that has to be executed.
126
     *
127
     * @var string
128
     * @Exclude
129
     */
130
    protected $shortcut;
131
132
    /**
133
     * The prefix for the move files subject.
134
     *
135
     * @var string
136
     * @Exclude
137
     * @SerializedName("move-files-prefix")
138
     * @Accessor(setter="setMoveFilesPrefix", getter="getMoveFilesPrefix")
139
     */
140
    protected $moveFilesPrefix;
141
142
    /**
143
     * The name of the command that has been invoked.
144
     *
145
     * @var string
146
     * @Exclude
147
     */
148
    protected $commandName;
149
150
    /**
151
     * The application's unique DI identifier.
152
     *
153
     * @var string
154
     * @Type("string")
155
     * @SerializedName("id")
156
     */
157
    protected $id;
158
159
    /**
160
     * The system name to use.
161
     *
162
     * @var string
163
     * @Type("string")
164
     * @SerializedName("system-name")
165
     * @Accessor(setter="setSystemName", getter="getSystemName")
166
     */
167
    protected $systemName;
168
169
    /**
170
     * The entity type code to use.
171
     *
172
     * @var string
173
     * @Type("string")
174
     * @SerializedName("entity-type-code")
175
     */
176
    protected $entityTypeCode;
177
178
    /**
179
     * The Magento installation directory.
180
     *
181
     * @var string
182
     * @Type("string")
183
     * @SerializedName("installation-dir")
184
     * @Accessor(setter="setInstallationDir", getter="getInstallationDir")
185
     */
186
    protected $installationDir;
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
     * @Accessor(setter="setSourceDir", getter="getSourceDir")
195
     */
196
    protected $sourceDir;
197
198
    /**
199
     * The target directory with the files that has been imported.
200
     *
201
     * @var string
202
     * @Type("string")
203
     * @SerializedName("target-dir")
204
     * @Accessor(setter="setTargetDir", getter="getTargetDir")
205
     */
206
    protected $targetDir;
207
208
    /**
209
     * The Magento edition, EE or CE.
210
     *
211
     * @var string
212
     * @Type("string")
213
     * @SerializedName("magento-edition")
214
     * @Accessor(setter="setMagentoEdition", getter="getMagentoEdition")
215
     */
216
    protected $magentoEdition = 'CE';
217
218
    /**
219
     * The Magento version, e. g. 2.2.0.
220
     *
221
     * @var string
222
     * @Type("string")
223
     * @SerializedName("magento-version")
224
     * @Accessor(setter="setMagentoVersion", getter="getMagentoVersion")
225
     */
226
    protected $magentoVersion = '2.2.0';
227
228
    /**
229
     * ArrayCollection with the information of the configured databases.
230
     *
231
     * @var \Doctrine\Common\Collections\ArrayCollection
232
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Database>")
233
     */
234
    protected $databases;
235
236
    /**
237
     * ArrayCollection with the information of the configured loggers.
238
     *
239
     * @var \Doctrine\Common\Collections\ArrayCollection
240
     * @Type("ArrayCollection<string, TechDivision\Import\Configuration\Jms\Configuration\Logger>")
241
     */
242
    protected $loggers;
243
244
    /**
245
     * The subject's multiple field delimiter character for fields with multiple values, defaults to (,).
246
     *
247
     * @var string
248
     * @Type("string")
249
     * @SerializedName("multiple-field-delimiter")
250
     */
251
    protected $multipleFieldDelimiter = ',';
252
253
    /**
254
     * The subject's multiple value delimiter character for fields with multiple values, defaults to (|).
255
     *
256
     * @var string
257
     * @Type("string")
258
     * @SerializedName("multiple-value-delimiter")
259
     */
260
    protected $multipleValueDelimiter = '|';
261
262
    /**
263
     * The flag to signal that the subject has to use the strict mode or not.
264
     *
265
     * @var boolean
266
     * @Type("boolean")
267
     * @SerializedName("strict-mode")
268
     */
269
    protected $strictMode;
270
271
    /**
272
     * The flag whether or not the import artefacts have to be archived.
273
     *
274
     * @var boolean
275
     * @Type("boolean")
276
     * @SerializedName("archive-artefacts")
277
     * @Accessor(setter="setArchiveArtefacts", getter="haveArchiveArtefacts")
278
     */
279
    protected $archiveArtefacts;
280
281
    /**
282
     * The directory where the archives will be stored.
283
     *
284
     * @var string
285
     * @Type("string")
286
     * @SerializedName("archive-dir")
287
     * @Accessor(setter="setArchiveDir", getter="getArchiveDir")
288
     */
289
    protected $archiveDir;
290
291
    /**
292
     * The flag to signal that the subject has to use the debug mode or not.
293
     *
294
     * @var boolean
295
     * @Type("boolean")
296
     * @SerializedName("debug-mode")
297
     * @Accessor(setter="setDebugMode", getter="isDebugMode")
298
     */
299
    protected $debugMode = false;
300
301
    /**
302
     * The log level to use (see Monolog documentation).
303
     *
304
     * @var string
305
     * @Type("string")
306
     * @SerializedName("log-level")
307
     * @Accessor(setter="setLogLevel", getter="getLogLevel")
308
     */
309
    protected $logLevel = LogLevel::INFO;
310
311
    /**
312
     * The explicit DB ID to use.
313
     *
314
     * @var string
315
     * @Type("string")
316
     * @SerializedName("use-db-id")
317
     */
318
    protected $useDbId;
319
320
    /**
321
     * The explicit PID filename to use.
322
     *
323
     * @var string
324
     * @Type("string")
325
     * @SerializedName("pid-filename")
326
     * @Accessor(setter="setPidFilename", getter="getPidFilename")
327
     */
328
    protected $pidFilename;
329
330
    /**
331
     * The collection with the paths to additional vendor directories.
332
     *
333
     * @var \Doctrine\Common\Collections\ArrayCollection
334
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\VendorDir>")
335
     * @SerializedName("additional-vendor-dirs")
336
     */
337
    protected $additionalVendorDirs;
338
339
    /**
340
     * ArrayCollection with the information of the configured operations.
341
     *
342
     * @var array
343
     * @Type("array<string, array<string, ArrayCollection<string, TechDivision\Import\Configuration\Jms\Configuration\Operation>>>")
344
     */
345
    protected $operations = array();
346
347
    /**
348
     * The array with the Magento Edition specific extension libraries.
349
     *
350
     * @var array
351
     * @Type("array")
352
     * @SerializedName("extension-libraries")
353
     */
354
    protected $extensionLibraries = array();
355
356
    /**
357
     * The array with the custom header mappings.
358
     *
359
     * @var array
360
     * @SerializedName("header-mappings")
361
     * @Type("array<string, array<string, string>>")
362
     */
363
    protected $headerMappings = array();
364
365
    /**
366
     * The array with the custom image types.
367
     *
368
     * @var array
369
     * @Type("array")
370
     * @SerializedName("image-types")
371
     */
372
    protected $imageTypes = array();
373
374
    /**
375
     * The flag to signal that the import should be wrapped within a single transation or not.
376
     *
377
     * @var boolean
378
     * @Type("boolean")
379
     * @SerializedName("single-transaction")
380
     * @Accessor(setter="setSingleTransaction", getter="isSingleTransaction")
381
     */
382
    protected $singleTransaction = false;
383
384
    /**
385
     * The flag to signal that the cache should be enabled or not.
386
     *
387
     * @var boolean
388
     * @Type("boolean")
389
     * @SerializedName("cache-enabled")
390
     * @Accessor(setter="setCacheEnabled", getter="isCacheEnabled")
391
     */
392
    protected $cacheEnabled = true;
393
394
    /**
395
     * ArrayCollection with the information of the configured aliases.
396
     *
397
     * @var \Doctrine\Common\Collections\ArrayCollection
398
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Alias>")
399
     */
400
    protected $aliases;
401
402
    /**
403
     * ArrayCollection with the information of the configured caches.
404
     *
405
     * @var \Doctrine\Common\Collections\ArrayCollection
406
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Cache>")
407
     */
408
    protected $caches;
409
410
    /**
411
     * The array with the shortcuts.
412
     *
413
     * @var array
414
     * @Type("array<string, array<string, array>>")
415
     * @SerializedName("shortcuts")
416
     */
417
    protected $shortcuts = array();
418
419
    /**
420
     * The username to save the import history with.
421
     *
422
     * @var string
423
     * @Type("string")
424
     */
425
    protected $username;
426
427
    /**
428
     * The array with the finder mappings.
429
     *
430
     * @var array
431
     * @SerializedName("finder-mappings")
432
     * @Type("array<string, string>")
433
     * @Accessor(setter="setFinderMappings", getter="getFinderMappings")
434
     */
435
    protected $finderMappings = array();
436
437
    /**
438
     * The array with the default values.
439
     *
440
     * @var array
441
     * @SerializedName("default-values")
442
     * @Type("array<string, array<string, string>>")
443
     * @Accessor(setter="setDefaultValues", getter="getDefaultValues")
444
     */
445
    protected $defaultValues = array();
446
447
    /**
448
     * Lifecycle callback that will be invoked after deserialization.
449
     *
450
     * @return void
451
     * @PostDeserialize
452
     */
453
    public function postDeserialize()
454
    {
455
456
        // create an empty collection if no loggers has been specified
457
        if ($this->loggers === null) {
458
            $this->loggers = new ArrayCollection();
459
        }
460
461
        // create an empty collection if no additional venor directories has been specified
462
        if ($this->additionalVendorDirs === null) {
463
            $this->additionalVendorDirs = new ArrayCollection();
464
        }
465
466
        // create an empty collection if no caches has been specified
467
        if ($this->caches === null) {
468
            $this->caches = new ArrayCollection();
469
        }
470
471
        // create an empty collection if no aliases has been specified
472
        if ($this->aliases === null) {
473
            $this->aliases = new ArrayCollection();
474
        }
475
    }
476
477
    /**
478
     * Map's the passed value to a boolean.
479
     *
480
     * @param string $value The value to map
481
     *
482
     * @return boolean The mapped value
483
     * @throws \Exception Is thrown, if the value can't be mapped
484
     */
485
    public function mapBoolean($value)
486
    {
487
488
        // do nothing, because passed value is already a boolean
489
        if (is_bool($value)) {
490
            return $value;
491
        }
492
493
        // try to map the passed value to a boolean
494
        if (isset($this->booleanMapping[$val = strtolower($value)])) {
495
            return $this->booleanMapping[$val];
496
        }
497
498
        // throw an exception if we can't convert the passed value
499
        throw new \Exception(sprintf('Can\'t convert %s to boolean', $value));
500
    }
501
502
    /**
503
     * Return's the application's unique DI identifier.
504
     *
505
     * @return string The application's unique DI identifier
506
     */
507
    public function getId()
508
    {
509
        return $this->id;
510
    }
511
512
    /**
513
     * Add's the operation with the passed name ot the operations that has to be executed.
514
     *
515
     * If the operation name has already been added, it'll not be added again.
516
     *
517
     * @param string  $operationName The operation to be executed
518
     * @param boolean $prepend       TRUE if the operation name should be prepended, else FALSE
519
     *
520
     * @return void
521
     */
522
    public function addOperationName($operationName, $prepend = false)
523
    {
524
525
        // do nothing if the operation has already been added
526
        if (in_array($operationName, $this->operationNames)) {
527
            return;
528
        }
529
530
        // add the operation otherwise
531
        $prepend ? array_unshift($this->operationNames, $operationName) : array_push($this->operationNames, $operationName);
532
    }
533
534
    /**
535
     * Return's the operation names that has to be executed.
536
     *
537
     * @param array $operationNames The operation names that has to be executed
538
     *
539
     * @return void
540
     */
541
    public function setOperationNames(array $operationNames)
542
    {
543
        return $this->operationNames = $operationNames;
544
    }
545
546
    /**
547
     * Return's the operation names that has to be executed.
548
     *
549
     * @return array The operation names that has to be executed
550
     */
551
    public function getOperationNames()
552
    {
553
        return $this->operationNames;
554
    }
555
556
    /**
557
     * Queries whether or not the passed operation has to be exceuted or not.
558
     *
559
     * @param \TechDivision\Import\Configuration\OperationConfigurationInterface $operation The operation to query for
560
     *
561
     * @return boolean TRUE if the operation has to be executed, else FALSE
562
     */
563
    public function inOperationNames(OperationConfigurationInterface $operation)
564
    {
565
        return in_array($operation->getName(), $this->getOperationNames());
566
    }
567
568
    /**
569
     * Set's the Magento installation directory.
570
     *
571
     * @param string $installationDir The Magento installation directory
572
     *
573
     * @return void
574
     */
575
    public function setInstallationDir($installationDir)
576
    {
577
        $this->installationDir = $installationDir;
578
    }
579
580
    /**
581
     * Return's the Magento installation directory.
582
     *
583
     * @return string The Magento installation directory
584
     */
585
    public function getInstallationDir()
586
    {
587
        return $this->installationDir;
588
    }
589
590
    /**
591
     * Set's the source directory that has to be watched for new files.
592
     *
593
     * @param string $sourceDir The source directory
594
     *
595
     * @return void
596
     */
597
    public function setSourceDir($sourceDir)
598
    {
599
        $this->sourceDir = $sourceDir;
600
    }
601
602
    /**
603
     * Return's the source directory that has to be watched for new files.
604
     *
605
     * @return string The source directory
606
     */
607
    public function getSourceDir()
608
    {
609
        return $this->sourceDir;
610
    }
611
612
    /**
613
     * Set's the target directory with the files that has been imported.
614
     *
615
     * @param string $targetDir The target directory
616
     *
617
     * @return void
618
     */
619
    public function setTargetDir($targetDir)
620
    {
621
        $this->targetDir = $targetDir;
622
    }
623
624
    /**
625
     * Return's the target directory with the files that has been imported.
626
     *
627
     * @return string The target directory
628
     */
629
    public function getTargetDir()
630
    {
631
        return $this->targetDir;
632
    }
633
634
    /**
635
     * Set's the Magento edition, EE or CE.
636
     *
637
     * @param string $magentoEdition The Magento edition
638
     *
639
     * @return void
640
     */
641
    public function setMagentoEdition($magentoEdition)
642
    {
643
        $this->magentoEdition = $magentoEdition;
644
    }
645
646
    /**
647
     * Return's the Magento edition, EE or CE.
648
     *
649
     * @return string The Magento edition
650
     */
651
    public function getMagentoEdition()
652
    {
653
        return $this->magentoEdition;
654
    }
655
656
    /**
657
     * Return's the Magento version, e. g. 2.1.0.
658
     *
659
     * @param string $magentoVersion The Magento version
660
     *
661
     * @return void
662
     */
663
    public function setMagentoVersion($magentoVersion)
664
    {
665
        $this->magentoVersion = $magentoVersion;
666
    }
667
668
    /**
669
     * Return's the Magento version, e. g. 2.1.0.
670
     *
671
     * @return string The Magento version
672
     */
673
    public function getMagentoVersion()
674
    {
675
        return $this->magentoVersion;
676
    }
677
678
    /**
679
     * Return's the entity type code to be used.
680
     *
681
     * @return string The entity type code to be used
682
     */
683
    public function getEntityTypeCode()
684
    {
685
        return $this->entityTypeCode;
686
    }
687
688
    /**
689
     * Set's the entity type code to be used.
690
     *
691
     * @param string $entityTypeCode The entity type code
692
     *
693
     * @return void
694
     */
695
    public function setEntityTypeCode($entityTypeCode)
696
    {
697
        $this->entityTypeCode = $entityTypeCode;
698
    }
699
700
    /**
701
     * Return's the multiple field delimiter character to use, default value is comma (,).
702
     *
703
     * @return string The multiple field delimiter character
704
     */
705
    public function getMultipleFieldDelimiter()
706
    {
707
        return $this->multipleFieldDelimiter;
708
    }
709
710
    /**
711
     * Return's the multiple value delimiter character to use, default value is comma (|).
712
     *
713
     * @return string The multiple value delimiter character
714
     */
715
    public function getMultipleValueDelimiter()
716
    {
717
        return $this->multipleValueDelimiter;
718
    }
719
720
    /**
721
     * Queries whether or not strict mode is enabled or not, default is TRUE.
722
     *
723
     * @return boolean TRUE if strict mode is enabled, else FALSE
724
     */
725
    public function isStrictMode()
726
    {
727
        return $this->strictMode;
728
    }
729
730
    /**
731
     * Remove's all configured database configuration.
732
     *
733
     * @return void
734
     */
735
    public function clearDatabases()
736
    {
737
        $this->databases->clear();
738
    }
739
740
    /**
741
     * Add's the passed database configuration.
742
     *
743
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
744
     *
745
     * @return void
746
     */
747
    public function addDatabase(DatabaseConfigurationInterface $database)
748
    {
749
        $this->databases->add($database);
750
    }
751
752
    /**
753
     * Return's the number database configurations.
754
     *
755
     * @return integer The number of database configurations
756
     */
757
    public function countDatabases()
758
    {
759
        return $this->databases->count();
760
    }
761
762
    /**
763
     * Return's the database configuration with the passed ID.
764
     *
765
     * @param string $id The ID of the database connection to return
766
     *
767
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
768
     * @throws \Exception Is thrown, if no database configuration is available
769
     */
770
    public function getDatabaseById($id)
771
    {
772
773
        // iterate over the configured databases and return the one with the passed ID
774
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
775
        foreach ($this->databases as $database) {
776
            if ($database->getId() === $id && $this->isValidDatabaseType($database)) {
777
                return $database;
778
            }
779
        }
780
781
        // throw an exception, if the database with the passed ID is NOT configured
782
        throw new \Exception(sprintf('Database with ID %s can not be found or has an invalid type', $id));
783
    }
784
785
    /**
786
     * Return's the databases for the given type.
787
     *
788
     * @param string $type The database type to return the configurations for
789
     *
790
     * @return \Doctrine\Common\Collections\Collection The collection with the database configurations
791
     */
792
    public function getDatabasesByType($type)
793
    {
794
795
        // initialize the collection for the database configurations
796
        $databases = new ArrayCollection();
797
798
        // iterate over the configured databases and return the one with the passed ID
799
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
800
        foreach ($this->databases as $database) {
801
            if ($database->getType() === $type && $this->isValidDatabaseType($database)) {
802
                $databases->add($database);
803
            }
804
        }
805
806
        // return the database configurations
807
        return $databases;
808
    }
809
810
    /**
811
     * Query's whether or not the passed database configuration has a valid type.
812
     *
813
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
814
     *
815
     * @return boolean TRUE if the passed database configuration has a valid type, else FALSE
816
     */
817
    protected function isValidDatabaseType(DatabaseConfigurationInterface $database)
818
    {
819
        return in_array(strtolower($database->getType()), $this->availableDatabaseTypes);
820
    }
821
822
    /**
823
     * Return's the database configuration.
824
     *
825
     * If an explicit DB ID is specified, the method tries to return the database with this ID. If
826
     * the database configuration is NOT available, an execption is thrown.
827
     *
828
     * If no explicit DB ID is specified, the method tries to return the default database configuration,
829
     * if not available the first one.
830
     *
831
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
832
     * @throws \Exception Is thrown, if no database configuration is available
833
     */
834
    public function getDatabase()
835
    {
836
837
        // if a DB ID has been set, try to load the database
838
        if ($useDbId = $this->getUseDbId()) {
839
            return $this->getDatabaseById($useDbId);
840
        }
841
842
        // iterate over the configured databases and try return the default database
843
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
844
        foreach ($this->databases as $database) {
845
            if ($database->isDefault() && $this->isValidDatabaseType($database)) {
846
                return $database;
847
            }
848
        }
849
850
        // try to return the first database configurtion
851
        if ($this->databases->count() > 0) {
852
            return $this->databases->first();
853
        }
854
855
        // throw an exception, if no database configuration is available
856
        throw new \Exception('There is no database configuration available');
857
    }
858
859
    /**
860
     * Return's the array with the configured operations.
861
     *
862
     * @return array The array with the operations
863
     */
864
    public function getOperations()
865
    {
866
        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...
867
    }
868
869
    /**
870
     * Return's the ArrayCollection with the configured shortcuts.
871
     *
872
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the shortcuts
873
     */
874
    public function getShortcuts()
875
    {
876
        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...
877
    }
878
879
    /**
880
     * Return's the ArrayCollection with the configured loggers.
881
     *
882
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the loggers
883
     */
884
    public function getLoggers()
885
    {
886
        return $this->loggers;
887
    }
888
889
    /**
890
     * Set's the flag that import artefacts have to be archived or not.
891
     *
892
     * @param mixed $archiveArtefacts TRUE if artefacts have to be archived, else FALSE
893
     *
894
     * @return void
895
     */
896
    public function setArchiveArtefacts($archiveArtefacts)
897
    {
898
        $this->archiveArtefacts = $this->mapBoolean($archiveArtefacts);
899
    }
900
901
    /**
902
     * Return's the TRUE if the import artefacts have to be archived.
903
     *
904
     * @return boolean TRUE if the import artefacts have to be archived
905
     */
906
    public function haveArchiveArtefacts()
907
    {
908
        return $this->archiveArtefacts;
909
    }
910
911
    /**
912
     * The directory where the archives will be stored.
913
     *
914
     * @param string $archiveDir The archive directory
915
     *
916
     * @return void
917
     */
918
    public function setArchiveDir($archiveDir)
919
    {
920
        $this->archiveDir = $archiveDir;
921
    }
922
923
    /**
924
     * The directory where the archives will be stored.
925
     *
926
     * @return string The archive directory
927
     */
928
    public function getArchiveDir()
929
    {
930
        return $this->archiveDir;
931
    }
932
933
    /**
934
     * Set's the debug mode.
935
     *
936
     * @param mixed $debugMode TRUE if debug mode is enabled, else FALSE
937
     *
938
     * @return void
939
     */
940
    public function setDebugMode($debugMode)
941
    {
942
        $this->debugMode = $this->mapBoolean($debugMode);
943
    }
944
945
    /**
946
     * Queries whether or not debug mode is enabled or not, default is TRUE.
947
     *
948
     * @return boolean TRUE if debug mode is enabled, else FALSE
949
     */
950
    public function isDebugMode()
951
    {
952
        return $this->debugMode;
953
    }
954
955
    /**
956
     * Set's the log level to use.
957
     *
958
     * @param string $logLevel The log level to use
959
     *
960
     * @return void
961
     */
962
    public function setLogLevel($logLevel)
963
    {
964
        $this->logLevel = $logLevel;
965
    }
966
967
    /**
968
     * Return's the log level to use.
969
     *
970
     * @return string The log level to use
971
     */
972
    public function getLogLevel()
973
    {
974
        return $this->logLevel;
975
    }
976
977
    /**
978
     * Set's the explicit DB ID to use.
979
     *
980
     * @param string $useDbId The explicit DB ID to use
981
     *
982
     * @return void
983
     */
984
    public function setUseDbId($useDbId)
985
    {
986
        $this->useDbId = $useDbId;
987
    }
988
989
    /**
990
     * Return's the explicit DB ID to use.
991
     *
992
     * @return string The explicit DB ID to use
993
     */
994
    public function getUseDbId()
995
    {
996
        return $this->useDbId;
997
    }
998
999
    /**
1000
     * Set's the PID filename to use.
1001
     *
1002
     * @param string $pidFilename The PID filename to use
1003
     *
1004
     * @return void
1005
     */
1006
    public function setPidFilename($pidFilename)
1007
    {
1008
        $this->pidFilename = $pidFilename;
1009
    }
1010
1011
    /**
1012
     * Return's the PID filename to use.
1013
     *
1014
     * @return string The PID filename to use
1015
     */
1016
    public function getPidFilename()
1017
    {
1018
        return $this->pidFilename;
1019
    }
1020
1021
    /**
1022
     * Set's the systemm name to be used.
1023
     *
1024
     * @param string $systemName The system name to be used
1025
     *
1026
     * @return void
1027
     */
1028
    public function setSystemName($systemName)
1029
    {
1030
        $this->systemName = $systemName;
1031
    }
1032
1033
    /**
1034
     * Return's the systemm name to be used.
1035
     *
1036
     * @return string The system name to be used
1037
     */
1038
    public function getSystemName()
1039
    {
1040
        return $this->systemName;
1041
    }
1042
1043
    /**
1044
     * Set's the collection with the path of the Magento Edition specific extension libraries.
1045
     *
1046
     * @param array $extensionLibraries The paths of the Magento Edition specific extension libraries
1047
     *
1048
     * @return void
1049
     */
1050
    public function setExtensionLibraries(array $extensionLibraries)
1051
    {
1052
        $this->extensionLibraries = $extensionLibraries;
1053
    }
1054
1055
    /**
1056
     * Return's an array with the path of the Magento Edition specific extension libraries.
1057
     *
1058
     * @return array The paths of the Magento Edition specific extension libraries
1059
     */
1060
    public function getExtensionLibraries()
1061
    {
1062
        return $this->extensionLibraries;
1063
    }
1064
1065
    /**
1066
     * Return's a collection with the path to additional vendor directories.
1067
     *
1068
     * @return \Doctrine\Common\Collections\ArrayCollection The paths to additional vendor directories
1069
     */
1070
    public function getAdditionalVendorDirs()
1071
    {
1072
        return $this->additionalVendorDirs;
1073
    }
1074
1075
    /**
1076
     * The array with the subject's custom header mappings.
1077
     *
1078
     * @return array The custom header mappings
1079
     */
1080
    public function getHeaderMappings()
1081
    {
1082
        return $this->headerMappings;
1083
    }
1084
1085
    /**
1086
     * The array with the subject's custom image types.
1087
     *
1088
     * @return array The custom image types
1089
     */
1090
    public function getImageTypes()
1091
    {
1092
        return $this->imageTypes;
1093
    }
1094
1095
    /**
1096
     * Set's the flag that decides whether or not the import should be wrapped within a single transaction.
1097
     *
1098
     * @param mixed $singleTransaction TRUE if the import should be wrapped in a single transation, else FALSE
1099
     *
1100
     * @return void
1101
     */
1102
    public function setSingleTransaction($singleTransaction)
1103
    {
1104
        $this->singleTransaction = $this->mapBoolean($singleTransaction);
1105
    }
1106
1107
    /**
1108
     * Whether or not the import should be wrapped within a single transation.
1109
     *
1110
     * @return boolean TRUE if the import should be wrapped in a single transation, else FALSE
1111
     */
1112
    public function isSingleTransaction()
1113
    {
1114
        return $this->singleTransaction;
1115
    }
1116
1117
    /**
1118
     * Set's the flag that decides whether or not the the cache has been enabled.
1119
     *
1120
     * @param mixed $cacheEnabled TRUE if the cache has been enabled, else FALSE
1121
     *
1122
     * @return void
1123
     */
1124
    public function setCacheEnabled($cacheEnabled)
1125
    {
1126
        $this->cacheEnabled = $this->mapBoolean($cacheEnabled);
1127
    }
1128
1129
    /**
1130
     * Whether or not the cache functionality should be enabled.
1131
     *
1132
     * @return boolean TRUE if the cache has to be enabled, else FALSE
1133
     */
1134
    public function isCacheEnabled()
1135
    {
1136
        return $this->cacheEnabled;
1137
    }
1138
1139
    /**
1140
     * Set's the passed serial from the commandline to the configuration.
1141
     *
1142
     * @param string $serial The serial from the commandline
1143
     *
1144
     * @return void
1145
     */
1146
    public function setSerial($serial)
1147
    {
1148
        $this->serial = $serial;
1149
    }
1150
1151
    /**
1152
     * Return's the serial from the commandline.
1153
     *
1154
     * @return string The serial
1155
     */
1156
    public function getSerial()
1157
    {
1158
        return $this->serial;
1159
    }
1160
1161
    /**
1162
     * Return's the configuration for the caches.
1163
     *
1164
     * @return \Doctrine\Common\Collections\ArrayCollection The cache configurations
1165
     */
1166
    public function getCaches()
1167
    {
1168
1169
        // iterate over the caches and set the parent configuration instance
1170
        foreach ($this->caches as $cache) {
1171
            $cache->setConfiguration($this);
1172
        }
1173
1174
        // return the array with the caches
1175
        return $this->caches;
1176
    }
1177
1178
    /**
1179
     * Return's the cache configuration for the passed type.
1180
     *
1181
     * @param string $type The cache type to return the configuation for
1182
     *
1183
     * @return \TechDivision\Import\Configuration\CacheConfigurationInterface The cache configuration
1184
     */
1185
    public function getCacheByType($type)
1186
    {
1187
1188
        // load the available cache configurations
1189
        $caches = $this->getCaches();
1190
1191
        // try to load the cache for the passed type
1192
        /** @var \TechDivision\Import\Configuration\CacheConfigurationInterface $cache */
1193
        foreach ($caches as $cache) {
1194
            if ($cache->getType() === $type) {
1195
                return $cache;
1196
            }
1197
        }
1198
    }
1199
1200
    /**
1201
     * Return's the alias configuration.
1202
     *
1203
     * @return \Doctrine\Common\Collections\ArrayCollection The alias configuration
1204
     */
1205
    public function getAliases()
1206
    {
1207
        return $this->aliases;
1208
    }
1209
1210
    /**
1211
     * Set's the prefix for the move files subject.
1212
     *
1213
     * @param string $moveFilesPrefix The prefix for the move files subject
1214
     *
1215
     * @return void
1216
     */
1217
    public function setMoveFilesPrefix($moveFilesPrefix)
1218
    {
1219
        $this->moveFilesPrefix = $moveFilesPrefix;
1220
    }
1221
1222
    /**
1223
     * Return's the prefix for the move files subject.
1224
     *
1225
     * @return string The prefix for the move files subject
1226
     */
1227
    public function getMoveFilesPrefix()
1228
    {
1229
        return $this->moveFilesPrefix;
1230
    }
1231
1232
    /**
1233
     * Set's the shortcut that maps the operation names that has to be executed.
1234
     *
1235
     * @param string $shortcut The shortcut
1236
     *
1237
     * @return void
1238
     */
1239
    public function setShortcut($shortcut)
1240
    {
1241
        $this->shortcut = $shortcut;
1242
    }
1243
1244
    /**
1245
     * Return's the shortcut that maps the operation names that has to be executed.
1246
     *
1247
     * @return string The shortcut
1248
     */
1249
    public function getShortcut()
1250
    {
1251
        return $this->shortcut;
1252
    }
1253
1254
    /**
1255
     * Set's the name of the command that has been invoked.
1256
     *
1257
     * @param string $commandName The command name
1258
     *
1259
     * @return void
1260
     */
1261
    public function setCommandName($commandName)
1262
    {
1263
        $this->commandName = $commandName;
1264
    }
1265
1266
    /**
1267
     * Return's the name of the command that has been invoked.
1268
     *
1269
     * @return string The command name
1270
     */
1271
    public function getCommandName()
1272
    {
1273
        return $this->commandName;
1274
    }
1275
1276
    /**
1277
     * Set's the username to save the import history with.
1278
     *
1279
     * @param string $username The username
1280
     *
1281
     * @return void
1282
     */
1283
    public function setUsername($username)
1284
    {
1285
        $this->username = $username;
1286
    }
1287
1288
    /**
1289
     * Return's the username to save the import history with.
1290
     *
1291
     * @return string The username
1292
     */
1293
    public function getUsername()
1294
    {
1295
        return $this->username;
1296
    }
1297
1298
    /**
1299
     * Set's the array with the finder mappings.
1300
     *
1301
     * @param array $finderMappings The finder mappings
1302
     *
1303
     * @return void
1304
     */
1305
    public function setFinderMappings(array $finderMappings)
1306
    {
1307
1308
        // convert the finder mappings keys, which are constants, to their values
1309
        foreach ($finderMappings as $key => $value) {
1310
            $this->finderMappings[defined($key) ? constant($key) : $key] = $value;
1311
        }
1312
    }
1313
1314
    /**
1315
     * Return's the array with the finder mappings.
1316
     *
1317
     * @return array The finder mappings
1318
     */
1319
    public function getFinderMappings()
1320
    {
1321
        return $this->finderMappings;
1322
    }
1323
1324
    /**
1325
     * Return's the mapped finder for the passed key.
1326
     *
1327
     * @param string $key The key of the finder to map
1328
     *
1329
     * @return string The mapped finder name
1330
     * @throws \InvalidArgumentException Is thrown if the mapping with passed key can not be resolved
1331
     */
1332
    public function getFinderMappingByKey($key)
1333
    {
1334
1335
        // try to resolve the mapping for the finder with the passed key
1336
        if (isset($this->finderMappings[$key])) {
1337
            return $this->finderMappings[$key];
1338
        }
1339
1340
        // throw an exception otherwise
1341
        throw new \InvalidArgumentException(sprintf('Can\'t load mapping for finder with key "%s"', $key));
1342
    }
1343
1344
    /**
1345
     * Sets the default values from the configuration.
1346
     *
1347
     * @param array $defaultValues The array with the default values
1348
     *
1349
     * @return void
1350
     */
1351
    public function setDefaultValues(array $defaultValues)
1352
    {
1353
        $this->defaultValues = $defaultValues;
1354
    }
1355
1356
    /**
1357
     * Load the default values from the configuration.
1358
     *
1359
     * @return array The array with the default values
1360
     */
1361
    public function getDefaultValues()
1362
    {
1363
        return $this->defaultValues;
1364
    }
1365
}
1366