Completed
Push — 15.x ( a48227...e72789 )
by Tim
01:41
created

Configuration::getFinderMappings()   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 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<TechDivision\Import\Configuration\Jms\Configuration\Logger>")
241
     */
242
    protected $loggers;
243
244
    /**
245
     * ArrayCollection with the information of the configured operations.
246
     *
247
     * @var \Doctrine\Common\Collections\ArrayCollection
248
     * @Type("array<string, array<string, ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Operation>>>")
249
     */
250
    protected $operations;
251
252
    /**
253
     * The subject's multiple field delimiter character for fields with multiple values, defaults to (,).
254
     *
255
     * @var string
256
     * @Type("string")
257
     * @SerializedName("multiple-field-delimiter")
258
     */
259
    protected $multipleFieldDelimiter = ',';
260
261
    /**
262
     * The subject's multiple value delimiter character for fields with multiple values, defaults to (|).
263
     *
264
     * @var string
265
     * @Type("string")
266
     * @SerializedName("multiple-value-delimiter")
267
     */
268
    protected $multipleValueDelimiter = '|';
269
270
    /**
271
     * The flag to signal that the subject has to use the strict mode or not.
272
     *
273
     * @var boolean
274
     * @Type("boolean")
275
     * @SerializedName("strict-mode")
276
     */
277
    protected $strictMode;
278
279
    /**
280
     * The flag whether or not the import artefacts have to be archived.
281
     *
282
     * @var boolean
283
     * @Type("boolean")
284
     * @SerializedName("archive-artefacts")
285
     * @Accessor(setter="setArchiveArtefacts", getter="haveArchiveArtefacts")
286
     */
287
    protected $archiveArtefacts;
288
289
    /**
290
     * The directory where the archives will be stored.
291
     *
292
     * @var string
293
     * @Type("string")
294
     * @SerializedName("archive-dir")
295
     * @Accessor(setter="setArchiveDir", getter="getArchiveDir")
296
     */
297
    protected $archiveDir;
298
299
    /**
300
     * The flag to signal that the subject has to use the debug mode or not.
301
     *
302
     * @var boolean
303
     * @Type("boolean")
304
     * @SerializedName("debug-mode")
305
     * @Accessor(setter="setDebugMode", getter="isDebugMode")
306
     */
307
    protected $debugMode = false;
308
309
    /**
310
     * The log level to use (see Monolog documentation).
311
     *
312
     * @var string
313
     * @Type("string")
314
     * @SerializedName("log-level")
315
     * @Accessor(setter="setLogLevel", getter="getLogLevel")
316
     */
317
    protected $logLevel = LogLevel::INFO;
318
319
    /**
320
     * The explicit DB ID to use.
321
     *
322
     * @var string
323
     * @Type("string")
324
     * @SerializedName("use-db-id")
325
     */
326
    protected $useDbId;
327
328
    /**
329
     * The explicit PID filename to use.
330
     *
331
     * @var string
332
     * @Type("string")
333
     * @SerializedName("pid-filename")
334
     * @Accessor(setter="setPidFilename", getter="getPidFilename")
335
     */
336
    protected $pidFilename;
337
338
    /**
339
     * The collection with the paths to additional vendor directories.
340
     *
341
     * @var \Doctrine\Common\Collections\ArrayCollection
342
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\VendorDir>")
343
     * @SerializedName("additional-vendor-dirs")
344
     */
345
    protected $additionalVendorDirs;
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 operations has been specified
462
        if ($this->operations === null) {
463
            $this->operations = new ArrayCollection();
464
        }
465
466
        // create an empty collection if no additional venor directories has been specified
467
        if ($this->additionalVendorDirs === null) {
468
            $this->additionalVendorDirs = new ArrayCollection();
469
        }
470
471
        // create an empty collection if no caches has been specified
472
        if ($this->caches === null) {
473
            $this->caches = new ArrayCollection();
474
        }
475
476
        // create an empty collection if no aliases has been specified
477
        if ($this->aliases === null) {
478
            $this->aliases = new ArrayCollection();
479
        }
480
    }
481
482
    /**
483
     * Map's the passed value to a boolean.
484
     *
485
     * @param string $value The value to map
486
     *
487
     * @return boolean The mapped value
488
     * @throws \Exception Is thrown, if the value can't be mapped
489
     */
490
    public function mapBoolean($value)
491
    {
492
493
        // do nothing, because passed value is already a boolean
494
        if (is_bool($value)) {
495
            return $value;
496
        }
497
498
        // try to map the passed value to a boolean
499
        if (isset($this->booleanMapping[$val = strtolower($value)])) {
500
            return $this->booleanMapping[$val];
501
        }
502
503
        // throw an exception if we can't convert the passed value
504
        throw new \Exception(sprintf('Can\'t convert %s to boolean', $value));
505
    }
506
507
    /**
508
     * Return's the application's unique DI identifier.
509
     *
510
     * @return string The application's unique DI identifier
511
     */
512
    public function getId()
513
    {
514
        return $this->id;
515
    }
516
517
    /**
518
     * Add's the operation with the passed name ot the operations that has to be executed.
519
     *
520
     * If the operation name has already been added, it'll not be added again.
521
     *
522
     * @param string  $operationName The operation to be executed
523
     * @param boolean $prepend       TRUE if the operation name should be prepended, else FALSE
524
     *
525
     * @return void
526
     */
527
    public function addOperationName($operationName, $prepend = false)
528
    {
529
530
        // do nothing if the operation has already been added
531
        if (in_array($operationName, $this->operationNames)) {
532
            return;
533
        }
534
535
        // add the operation otherwise
536
        $prepend ? array_unshift($this->operationNames, $operationName) : array_push($this->operationNames, $operationName);
537
    }
538
539
    /**
540
     * Return's the operation names that has to be executed.
541
     *
542
     * @param array $operationNames The operation names that has to be executed
543
     *
544
     * @return void
545
     */
546
    public function setOperationNames(array $operationNames)
547
    {
548
        return $this->operationNames = $operationNames;
549
    }
550
551
    /**
552
     * Return's the operation names that has to be executed.
553
     *
554
     * @return array The operation names that has to be executed
555
     */
556
    public function getOperationNames()
557
    {
558
        return $this->operationNames;
559
    }
560
561
    /**
562
     * Queries whether or not the passed operation has to be exceuted or not.
563
     *
564
     * @param \TechDivision\Import\Configuration\OperationConfigurationInterface $operation The operation to query for
565
     *
566
     * @return boolean TRUE if the operation has to be executed, else FALSE
567
     */
568
    public function inOperationNames(OperationConfigurationInterface $operation)
569
    {
570
        return in_array($operation->getName(), $this->getOperationNames());
571
    }
572
573
    /**
574
     * Set's the Magento installation directory.
575
     *
576
     * @param string $installationDir The Magento installation directory
577
     *
578
     * @return void
579
     */
580
    public function setInstallationDir($installationDir)
581
    {
582
        $this->installationDir = $installationDir;
583
    }
584
585
    /**
586
     * Return's the Magento installation directory.
587
     *
588
     * @return string The Magento installation directory
589
     */
590
    public function getInstallationDir()
591
    {
592
        return $this->installationDir;
593
    }
594
595
    /**
596
     * Set's the source directory that has to be watched for new files.
597
     *
598
     * @param string $sourceDir The source directory
599
     *
600
     * @return void
601
     */
602
    public function setSourceDir($sourceDir)
603
    {
604
        $this->sourceDir = $sourceDir;
605
    }
606
607
    /**
608
     * Return's the source directory that has to be watched for new files.
609
     *
610
     * @return string The source directory
611
     */
612
    public function getSourceDir()
613
    {
614
        return $this->sourceDir;
615
    }
616
617
    /**
618
     * Set's the target directory with the files that has been imported.
619
     *
620
     * @param string $targetDir The target directory
621
     *
622
     * @return void
623
     */
624
    public function setTargetDir($targetDir)
625
    {
626
        $this->targetDir = $targetDir;
627
    }
628
629
    /**
630
     * Return's the target directory with the files that has been imported.
631
     *
632
     * @return string The target directory
633
     */
634
    public function getTargetDir()
635
    {
636
        return $this->targetDir;
637
    }
638
639
    /**
640
     * Set's the Magento edition, EE or CE.
641
     *
642
     * @param string $magentoEdition The Magento edition
643
     *
644
     * @return void
645
     */
646
    public function setMagentoEdition($magentoEdition)
647
    {
648
        $this->magentoEdition = $magentoEdition;
649
    }
650
651
    /**
652
     * Return's the Magento edition, EE or CE.
653
     *
654
     * @return string The Magento edition
655
     */
656
    public function getMagentoEdition()
657
    {
658
        return $this->magentoEdition;
659
    }
660
661
    /**
662
     * Return's the Magento version, e. g. 2.1.0.
663
     *
664
     * @param string $magentoVersion The Magento version
665
     *
666
     * @return void
667
     */
668
    public function setMagentoVersion($magentoVersion)
669
    {
670
        $this->magentoVersion = $magentoVersion;
671
    }
672
673
    /**
674
     * Return's the Magento version, e. g. 2.1.0.
675
     *
676
     * @return string The Magento version
677
     */
678
    public function getMagentoVersion()
679
    {
680
        return $this->magentoVersion;
681
    }
682
683
    /**
684
     * Return's the entity type code to be used.
685
     *
686
     * @return string The entity type code to be used
687
     */
688
    public function getEntityTypeCode()
689
    {
690
        return $this->entityTypeCode;
691
    }
692
693
    /**
694
     * Set's the entity type code to be used.
695
     *
696
     * @param string $entityTypeCode The entity type code
697
     *
698
     * @return void
699
     */
700
    public function setEntityTypeCode($entityTypeCode)
701
    {
702
        $this->entityTypeCode = $entityTypeCode;
703
    }
704
705
    /**
706
     * Return's the multiple field delimiter character to use, default value is comma (,).
707
     *
708
     * @return string The multiple field delimiter character
709
     */
710
    public function getMultipleFieldDelimiter()
711
    {
712
        return $this->multipleFieldDelimiter;
713
    }
714
715
    /**
716
     * Return's the multiple value delimiter character to use, default value is comma (|).
717
     *
718
     * @return string The multiple value delimiter character
719
     */
720
    public function getMultipleValueDelimiter()
721
    {
722
        return $this->multipleValueDelimiter;
723
    }
724
725
    /**
726
     * Queries whether or not strict mode is enabled or not, default is TRUE.
727
     *
728
     * @return boolean TRUE if strict mode is enabled, else FALSE
729
     */
730
    public function isStrictMode()
731
    {
732
        return $this->strictMode;
733
    }
734
735
    /**
736
     * Remove's all configured database configuration.
737
     *
738
     * @return void
739
     */
740
    public function clearDatabases()
741
    {
742
        $this->databases->clear();
743
    }
744
745
    /**
746
     * Add's the passed database configuration.
747
     *
748
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
749
     *
750
     * @return void
751
     */
752
    public function addDatabase(DatabaseConfigurationInterface $database)
753
    {
754
        $this->databases->add($database);
755
    }
756
757
    /**
758
     * Return's the number database configurations.
759
     *
760
     * @return integer The number of database configurations
761
     */
762
    public function countDatabases()
763
    {
764
        return $this->databases->count();
765
    }
766
767
    /**
768
     * Return's the database configuration with the passed ID.
769
     *
770
     * @param string $id The ID of the database connection to return
771
     *
772
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
773
     * @throws \Exception Is thrown, if no database configuration is available
774
     */
775
    public function getDatabaseById($id)
776
    {
777
778
        // iterate over the configured databases and return the one with the passed ID
779
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
780
        foreach ($this->databases as $database) {
781
            if ($database->getId() === $id && $this->isValidDatabaseType($database)) {
782
                return $database;
783
            }
784
        }
785
786
        // throw an exception, if the database with the passed ID is NOT configured
787
        throw new \Exception(sprintf('Database with ID %s can not be found or has an invalid type', $id));
788
    }
789
790
    /**
791
     * Return's the databases for the given type.
792
     *
793
     * @param string $type The database type to return the configurations for
794
     *
795
     * @return \Doctrine\Common\Collections\Collection The collection with the database configurations
796
     */
797
    public function getDatabasesByType($type)
798
    {
799
800
        // initialize the collection for the database configurations
801
        $databases = new ArrayCollection();
802
803
        // iterate over the configured databases and return the one with the passed ID
804
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
805
        foreach ($this->databases as $database) {
806
            if ($database->getType() === $type && $this->isValidDatabaseType($database)) {
807
                $databases->add($database);
808
            }
809
        }
810
811
        // return the database configurations
812
        return $databases;
813
    }
814
815
    /**
816
     * Query's whether or not the passed database configuration has a valid type.
817
     *
818
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
819
     *
820
     * @return boolean TRUE if the passed database configuration has a valid type, else FALSE
821
     */
822
    protected function isValidDatabaseType(DatabaseConfigurationInterface $database)
823
    {
824
        return in_array(strtolower($database->getType()), $this->availableDatabaseTypes);
825
    }
826
827
    /**
828
     * Return's the database configuration.
829
     *
830
     * If an explicit DB ID is specified, the method tries to return the database with this ID. If
831
     * the database configuration is NOT available, an execption is thrown.
832
     *
833
     * If no explicit DB ID is specified, the method tries to return the default database configuration,
834
     * if not available the first one.
835
     *
836
     * @return \TechDivision\Import\Configuration\DatabaseConfigurationInterface The database configuration
837
     * @throws \Exception Is thrown, if no database configuration is available
838
     */
839
    public function getDatabase()
840
    {
841
842
        // if a DB ID has been set, try to load the database
843
        if ($useDbId = $this->getUseDbId()) {
844
            return $this->getDatabaseById($useDbId);
845
        }
846
847
        // iterate over the configured databases and try return the default database
848
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
849
        foreach ($this->databases as $database) {
850
            if ($database->isDefault() && $this->isValidDatabaseType($database)) {
851
                return $database;
852
            }
853
        }
854
855
        // try to return the first database configurtion
856
        if ($this->databases->count() > 0) {
857
            return $this->databases->first();
858
        }
859
860
        // throw an exception, if no database configuration is available
861
        throw new \Exception('There is no database configuration available');
862
    }
863
864
    /**
865
     * Return's the ArrayCollection with the configured operations.
866
     *
867
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations
868
     */
869
    public function getOperations()
870
    {
871
        return $this->operations;
872
    }
873
874
    /**
875
     * Return's the ArrayCollection with the configured shortcuts.
876
     *
877
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the shortcuts
878
     */
879
    public function getShortcuts()
880
    {
881
        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...
882
    }
883
884
    /**
885
     * Return's the ArrayCollection with the configured loggers.
886
     *
887
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the loggers
888
     */
889
    public function getLoggers()
890
    {
891
        return $this->loggers;
892
    }
893
894
    /**
895
     * Set's the flag that import artefacts have to be archived or not.
896
     *
897
     * @param mixed $archiveArtefacts TRUE if artefacts have to be archived, else FALSE
898
     *
899
     * @return void
900
     */
901
    public function setArchiveArtefacts($archiveArtefacts)
902
    {
903
        $this->archiveArtefacts = $this->mapBoolean($archiveArtefacts);
904
    }
905
906
    /**
907
     * Return's the TRUE if the import artefacts have to be archived.
908
     *
909
     * @return boolean TRUE if the import artefacts have to be archived
910
     */
911
    public function haveArchiveArtefacts()
912
    {
913
        return $this->archiveArtefacts;
914
    }
915
916
    /**
917
     * The directory where the archives will be stored.
918
     *
919
     * @param string $archiveDir The archive directory
920
     *
921
     * @return void
922
     */
923
    public function setArchiveDir($archiveDir)
924
    {
925
        $this->archiveDir = $archiveDir;
926
    }
927
928
    /**
929
     * The directory where the archives will be stored.
930
     *
931
     * @return string The archive directory
932
     */
933
    public function getArchiveDir()
934
    {
935
        return $this->archiveDir;
936
    }
937
938
    /**
939
     * Set's the debug mode.
940
     *
941
     * @param mixed $debugMode TRUE if debug mode is enabled, else FALSE
942
     *
943
     * @return void
944
     */
945
    public function setDebugMode($debugMode)
946
    {
947
        $this->debugMode = $this->mapBoolean($debugMode);
948
    }
949
950
    /**
951
     * Queries whether or not debug mode is enabled or not, default is TRUE.
952
     *
953
     * @return boolean TRUE if debug mode is enabled, else FALSE
954
     */
955
    public function isDebugMode()
956
    {
957
        return $this->debugMode;
958
    }
959
960
    /**
961
     * Set's the log level to use.
962
     *
963
     * @param string $logLevel The log level to use
964
     *
965
     * @return void
966
     */
967
    public function setLogLevel($logLevel)
968
    {
969
        $this->logLevel = $logLevel;
970
    }
971
972
    /**
973
     * Return's the log level to use.
974
     *
975
     * @return string The log level to use
976
     */
977
    public function getLogLevel()
978
    {
979
        return $this->logLevel;
980
    }
981
982
    /**
983
     * Set's the explicit DB ID to use.
984
     *
985
     * @param string $useDbId The explicit DB ID to use
986
     *
987
     * @return void
988
     */
989
    public function setUseDbId($useDbId)
990
    {
991
        $this->useDbId = $useDbId;
992
    }
993
994
    /**
995
     * Return's the explicit DB ID to use.
996
     *
997
     * @return string The explicit DB ID to use
998
     */
999
    public function getUseDbId()
1000
    {
1001
        return $this->useDbId;
1002
    }
1003
1004
    /**
1005
     * Set's the PID filename to use.
1006
     *
1007
     * @param string $pidFilename The PID filename to use
1008
     *
1009
     * @return void
1010
     */
1011
    public function setPidFilename($pidFilename)
1012
    {
1013
        $this->pidFilename = $pidFilename;
1014
    }
1015
1016
    /**
1017
     * Return's the PID filename to use.
1018
     *
1019
     * @return string The PID filename to use
1020
     */
1021
    public function getPidFilename()
1022
    {
1023
        return $this->pidFilename;
1024
    }
1025
1026
    /**
1027
     * Set's the systemm name to be used.
1028
     *
1029
     * @param string $systemName The system name to be used
1030
     *
1031
     * @return void
1032
     */
1033
    public function setSystemName($systemName)
1034
    {
1035
        $this->systemName = $systemName;
1036
    }
1037
1038
    /**
1039
     * Return's the systemm name to be used.
1040
     *
1041
     * @return string The system name to be used
1042
     */
1043
    public function getSystemName()
1044
    {
1045
        return $this->systemName;
1046
    }
1047
1048
    /**
1049
     * Set's the collection with the path of the Magento Edition specific extension libraries.
1050
     *
1051
     * @param array $extensionLibraries The paths of the Magento Edition specific extension libraries
1052
     *
1053
     * @return void
1054
     */
1055
    public function setExtensionLibraries(array $extensionLibraries)
1056
    {
1057
        $this->extensionLibraries = $extensionLibraries;
1058
    }
1059
1060
    /**
1061
     * Return's an array with the path of the Magento Edition specific extension libraries.
1062
     *
1063
     * @return array The paths of the Magento Edition specific extension libraries
1064
     */
1065
    public function getExtensionLibraries()
1066
    {
1067
        return $this->extensionLibraries;
1068
    }
1069
1070
    /**
1071
     * Return's a collection with the path to additional vendor directories.
1072
     *
1073
     * @return \Doctrine\Common\Collections\ArrayCollection The paths to additional vendor directories
1074
     */
1075
    public function getAdditionalVendorDirs()
1076
    {
1077
        return $this->additionalVendorDirs;
1078
    }
1079
1080
    /**
1081
     * The array with the subject's custom header mappings.
1082
     *
1083
     * @return array The custom header mappings
1084
     */
1085
    public function getHeaderMappings()
1086
    {
1087
        return $this->headerMappings;
1088
    }
1089
1090
    /**
1091
     * The array with the subject's custom image types.
1092
     *
1093
     * @return array The custom image types
1094
     */
1095
    public function getImageTypes()
1096
    {
1097
        return $this->imageTypes;
1098
    }
1099
1100
    /**
1101
     * Set's the flag that decides whether or not the import should be wrapped within a single transaction.
1102
     *
1103
     * @param mixed $singleTransaction TRUE if the import should be wrapped in a single transation, else FALSE
1104
     *
1105
     * @return void
1106
     */
1107
    public function setSingleTransaction($singleTransaction)
1108
    {
1109
        $this->singleTransaction = $this->mapBoolean($singleTransaction);
1110
    }
1111
1112
    /**
1113
     * Whether or not the import should be wrapped within a single transation.
1114
     *
1115
     * @return boolean TRUE if the import should be wrapped in a single transation, else FALSE
1116
     */
1117
    public function isSingleTransaction()
1118
    {
1119
        return $this->singleTransaction;
1120
    }
1121
1122
    /**
1123
     * Set's the flag that decides whether or not the the cache has been enabled.
1124
     *
1125
     * @param mixed $cacheEnabled TRUE if the cache has been enabled, else FALSE
1126
     *
1127
     * @return void
1128
     */
1129
    public function setCacheEnabled($cacheEnabled)
1130
    {
1131
        $this->cacheEnabled = $this->mapBoolean($cacheEnabled);
1132
    }
1133
1134
    /**
1135
     * Whether or not the cache functionality should be enabled.
1136
     *
1137
     * @return boolean TRUE if the cache has to be enabled, else FALSE
1138
     */
1139
    public function isCacheEnabled()
1140
    {
1141
        return $this->cacheEnabled;
1142
    }
1143
1144
    /**
1145
     * Set's the passed serial from the commandline to the configuration.
1146
     *
1147
     * @param string $serial The serial from the commandline
1148
     *
1149
     * @return void
1150
     */
1151
    public function setSerial($serial)
1152
    {
1153
        $this->serial = $serial;
1154
    }
1155
1156
    /**
1157
     * Return's the serial from the commandline.
1158
     *
1159
     * @return string The serial
1160
     */
1161
    public function getSerial()
1162
    {
1163
        return $this->serial;
1164
    }
1165
1166
    /**
1167
     * Return's the configuration for the caches.
1168
     *
1169
     * @return \Doctrine\Common\Collections\ArrayCollection The cache configurations
1170
     */
1171
    public function getCaches()
1172
    {
1173
1174
        // iterate over the caches and set the parent configuration instance
1175
        foreach ($this->caches as $cache) {
1176
            $cache->setConfiguration($this);
1177
        }
1178
1179
        // return the array with the caches
1180
        return $this->caches;
1181
    }
1182
1183
    /**
1184
     * Return's the cache configuration for the passed type.
1185
     *
1186
     * @param string $type The cache type to return the configuation for
1187
     *
1188
     * @return \TechDivision\Import\Configuration\CacheConfigurationInterface The cache configuration
1189
     */
1190
    public function getCacheByType($type)
1191
    {
1192
1193
        // load the available cache configurations
1194
        $caches = $this->getCaches();
1195
1196
        // try to load the cache for the passed type
1197
        /** @var \TechDivision\Import\Configuration\CacheConfigurationInterface $cache */
1198
        foreach ($caches as $cache) {
1199
            if ($cache->getType() === $type) {
1200
                return $cache;
1201
            }
1202
        }
1203
    }
1204
1205
    /**
1206
     * Return's the alias configuration.
1207
     *
1208
     * @return \Doctrine\Common\Collections\ArrayCollection The alias configuration
1209
     */
1210
    public function getAliases()
1211
    {
1212
        return $this->aliases;
1213
    }
1214
1215
    /**
1216
     * Set's the prefix for the move files subject.
1217
     *
1218
     * @param string $moveFilesPrefix The prefix for the move files subject
1219
     *
1220
     * @return void
1221
     */
1222
    public function setMoveFilesPrefix($moveFilesPrefix)
1223
    {
1224
        $this->moveFilesPrefix = $moveFilesPrefix;
1225
    }
1226
1227
    /**
1228
     * Return's the prefix for the move files subject.
1229
     *
1230
     * @return string The prefix for the move files subject
1231
     */
1232
    public function getMoveFilesPrefix()
1233
    {
1234
        return $this->moveFilesPrefix;
1235
    }
1236
1237
    /**
1238
     * Set's the shortcut that maps the operation names that has to be executed.
1239
     *
1240
     * @param string $shortcut The shortcut
1241
     *
1242
     * @return void
1243
     */
1244
    public function setShortcut($shortcut)
1245
    {
1246
        $this->shortcut = $shortcut;
1247
    }
1248
1249
    /**
1250
     * Return's the shortcut that maps the operation names that has to be executed.
1251
     *
1252
     * @return string The shortcut
1253
     */
1254
    public function getShortcut()
1255
    {
1256
        return $this->shortcut;
1257
    }
1258
1259
    /**
1260
     * Set's the name of the command that has been invoked.
1261
     *
1262
     * @param string $commandName The command name
1263
     *
1264
     * @return void
1265
     */
1266
    public function setCommandName($commandName)
1267
    {
1268
        $this->commandName = $commandName;
1269
    }
1270
1271
    /**
1272
     * Return's the name of the command that has been invoked.
1273
     *
1274
     * @return string The command name
1275
     */
1276
    public function getCommandName()
1277
    {
1278
        return $this->commandName;
1279
    }
1280
1281
    /**
1282
     * Set's the username to save the import history with.
1283
     *
1284
     * @param string $username The username
1285
     *
1286
     * @return void
1287
     */
1288
    public function setUsername($username)
1289
    {
1290
        $this->username = $username;
1291
    }
1292
1293
    /**
1294
     * Return's the username to save the import history with.
1295
     *
1296
     * @return string The username
1297
     */
1298
    public function getUsername()
1299
    {
1300
        return $this->username;
1301
    }
1302
1303
    /**
1304
     * Set's the array with the finder mappings.
1305
     *
1306
     * @param array $finderMappings The finder mappings
1307
     *
1308
     * @return void
1309
     */
1310
    public function setFinderMappings(array $finderMappings)
1311
    {
1312
1313
        // convert the finder mappings keys, which are constants, to their values
1314
        foreach ($finderMappings as $key => $value) {
1315
            $this->finderMappings[defined($key) ? constant($key) : $key] = $value;
1316
        }
1317
    }
1318
1319
    /**
1320
     * Return's the array with the finder mappings.
1321
     *
1322
     * @return array The finder mappings
1323
     */
1324
    public function getFinderMappings()
1325
    {
1326
        return $this->finderMappings;
1327
    }
1328
1329
    /**
1330
     * Return's the mapped finder for the passed key.
1331
     *
1332
     * @param string $key The key of the finder to map
1333
     *
1334
     * @return string The mapped finder name
1335
     * @throws \InvalidArgumentException Is thrown if the mapping with passed key can not be resolved
1336
     */
1337
    public function getFinderMappingByKey($key)
1338
    {
1339
1340
        // try to resolve the mapping for the finder with the passed key
1341
        if (isset($this->finderMappings[$key])) {
1342
            return $this->finderMappings[$key];
1343
        }
1344
1345
        // throw an exception otherwise
1346
        throw new \InvalidArgumentException(sprintf('Can\'t load mapping for finder with key "%s"', $key));
1347
    }
1348
1349
    /**
1350
     * Sets the default values from the configuration.
1351
     *
1352
     * @param array $defaultValues The array with the default values
1353
     *
1354
     * @return void
1355
     */
1356
    public function setDefaultValues(array $defaultValues)
1357
    {
1358
        $this->defaultValues = $defaultValues;
1359
    }
1360
1361
    /**
1362
     * Load the default values from the configuration.
1363
     *
1364
     * @return array The array with the default values
1365
     */
1366
    public function getDefaultValues()
1367
    {
1368
        return $this->defaultValues;
1369
    }
1370
}
1371