Completed
Push — 16.x ( 8889c9...ec9e58 )
by Tim
01:32
created

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