Completed
Push — 15.x ( 64043b...a8a0e5 )
by Tim
02:48
created

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