Completed
Push — 15.x ( 66d904...a48227 )
by Tim
01:30
created

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