Completed
Pull Request — master (#2)
by Tim
02:14
created

Configuration::getLoggers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
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\SerializedName;
26
use Doctrine\Common\Collections\ArrayCollection;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Configuration\Jms\Configuration\Operation;
29
use TechDivision\Import\Configuration\DatabaseConfigurationInterface;
30
31
/**
32
 * A simple JMS based configuration implementation.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import-configuration-jms
38
 * @link      http://www.techdivision.com
39
 */
40
class Configuration implements ConfigurationInterface
41
{
42
43
    /**
44
     * The default PID filename to use.
45
     *
46
     * @var string
47
     */
48
    const PID_FILENAME = 'importer.pid';
49
50
    /**
51
     * Mapping for boolean values passed on the console.
52
     *
53
     * @var array
54
     */
55
    protected $booleanMapping = array(
56
        'true'  => true,
57
        'false' => false,
58
        '1'     => true,
59
        '0'     => false,
60
        'on'    => true,
61
        'off'   => false
62
    );
63
64
    /**
65
     * The operation name to use.
66
     *
67
     * @var string
68
     * @Type("string")
69
     * @SerializedName("operation-name")
70
     */
71
    protected $operationName;
72
73
    /**
74
     * The entity type code to use.
75
     *
76
     * @var string
77
     * @Type("string")
78
     * @SerializedName("entity-type-code")
79
     */
80
    protected $entityTypeCode;
81
82
    /**
83
     * The Magento installation directory.
84
     *
85
     * @var string
86
     * @Type("string")
87
     * @SerializedName("installation-dir")
88
     */
89
    protected $installationDir;
90
91
    /**
92
     * The source directory that has to be watched for new files.
93
     *
94
     * @var string
95
     * @Type("string")
96
     * @SerializedName("source-dir")
97
     */
98
    protected $sourceDir;
99
100
    /**
101
     * The target directory with the files that has been imported.
102
     *
103
     * @var string
104
     * @Type("string")
105
     * @SerializedName("target-dir")
106
     */
107
    protected $targetDir;
108
109
    /**
110
     * The Magento edition, EE or CE.
111
     *
112
     * @var string
113
     * @Type("string")
114
     * @SerializedName("magento-edition")
115
     */
116
    protected $magentoEdition = 'CE';
117
118
    /**
119
     * The Magento version, e. g. 2.1.0.
120
     *
121
     * @var string
122
     * @Type("string")
123
     * @SerializedName("magento-version")
124
     */
125
    protected $magentoVersion = '2.1.2';
126
127
    /**
128
     * ArrayCollection with the information of the configured databases.
129
     *
130
     * @var \Doctrine\Common\Collections\ArrayCollection
131
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Database>")
132
     */
133
    protected $databases;
134
135
    /**
136
     * ArrayCollection with the information of the configured loggers.
137
     *
138
     * @var \Doctrine\Common\Collections\ArrayCollection
139
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Logger>")
140
     */
141
    protected $loggers = array();
142
143
    /**
144
     * ArrayCollection with the information of the configured operations.
145
     *
146
     * @var \Doctrine\Common\Collections\ArrayCollection
147
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Operation>")
148
     */
149
    protected $operations = array();
150
151
    /**
152
     * The source date format to use in the subject.
153
     *
154
     * @var string
155
     * @Type("string")
156
     * @SerializedName("source-date-format")
157
     */
158
    protected $sourceDateFormat = 'n/d/y, g:i A';
159
160
    /**
161
     * The subject's multiple field delimiter character for fields with multiple values, defaults to (,).
162
     *
163
     * @var string
164
     * @Type("string")
165
     * @SerializedName("multiple-field-delimiter")
166
     */
167
    protected $multipleFieldDelimiter = ',';
168
169
    /**
170
     * The subject's multiple value delimiter character for fields with multiple values, defaults to (|).
171
     *
172
     * @var string
173
     * @Type("string")
174
     * @SerializedName("multiple-value-delimiter")
175
     */
176
    protected $multipleValueDelimiter = '|';
177
178
    /**
179
     * The subject's delimiter character for CSV files.
180
     *
181
     * @var string
182
     * @Type("string")
183
     */
184
    protected $delimiter;
185
186
    /**
187
     * The subject's enclosure character for CSV files.
188
     *
189
     * @var string
190
     * @Type("string")
191
     */
192
    protected $enclosure;
193
194
    /**
195
     * The subject's escape character for CSV files.
196
     *
197
     * @var string
198
     * @Type("string")
199
     */
200
    protected $escape;
201
202
    /**
203
     * The subject's source charset for the CSV file.
204
     *
205
     * @var string
206
     * @Type("string")
207
     * @SerializedName("from-charset")
208
     */
209
    protected $fromCharset;
210
211
    /**
212
     * The subject's target charset for a CSV file.
213
     *
214
     * @var string
215
     * @Type("string")
216
     * @SerializedName("to-charset")
217
     */
218
    protected $toCharset;
219
220
    /**
221
     * The subject's file mode for a CSV target file.
222
     *
223
     * @var string
224
     * @Type("string")
225
     * @SerializedName("file-mode")
226
     */
227
    protected $fileMode;
228
229
    /**
230
     * The flag to signal that the subject has to use the strict mode or not.
231
     *
232
     * @var boolean
233
     * @Type("boolean")
234
     * @SerializedName("strict-mode")
235
     */
236
    protected $strictMode;
237
238
    /**
239
     * The flag whether or not the import artefacts have to be archived.
240
     *
241
     * @var boolean
242
     * @Type("boolean")
243
     * @SerializedName("archive-artefacts")
244
     */
245
    protected $archiveArtefacts;
246
247
    /**
248
     * The directory where the archives will be stored.
249
     *
250
     * @var string
251
     * @Type("string")
252
     * @SerializedName("archive-dir")
253
     */
254
    protected $archiveDir;
255
256
    /**
257
     * The flag to signal that the subject has to use the debug mode or not.
258
     *
259
     * @var boolean
260
     * @Type("boolean")
261
     * @SerializedName("debug-mode")
262
     */
263
    protected $debugMode = false;
264
265
    /**
266
     * The log level to use (see Monolog documentation).
267
     *
268
     * @var string
269
     * @Type("string")
270
     * @SerializedName("log-level")
271
     */
272
    protected $logLevel = LogLevel::INFO;
273
274
    /**
275
     * The explicit DB ID to use.
276
     *
277
     * @var string
278
     * @Type("string")
279
     * @SerializedName("use-db-id")
280
     */
281
    protected $useDbId;
282
283
    /**
284
     * The explicit PID filename to use.
285
     *
286
     * @var string
287
     * @Type("string")
288
     * @SerializedName("pid-filename")
289
     */
290
    protected $pidFilename;
291
292
    /**
293
     * The collection with the paths to additional vendor directories.
294
     *
295
     * @var \Doctrine\Common\Collections\ArrayCollection
296
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\VendorDir>")
297
     * @SerializedName("additional-vendor-dirs")
298
     */
299
    protected $additionalVendorDirs = array();
300
301
    /**
302
     * The array with the Magento Edition specific extension libraries.
303
     *
304
     * @var array
305
     * @Type("array")
306
     * @SerializedName("extension-libraries")
307
     */
308
    protected $extensionLibraries = array();
309
310
    /**
311
     * Return's the array with the plugins of the operation to use.
312
     *
313
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the plugins
314
     * @throws \Exception Is thrown, if no plugins are available for the actual operation
315
     */
316
    public function getPlugins()
317
    {
318
319
        // iterate over the operations and return the subjects of the actual one
320
        /** @var TechDivision\Import\Configuration\OperationInterface $operation */
321
        foreach ($this->getOperations() as $operation) {
322
            if ($this->getOperation()->equals($operation)) {
323
                return $operation->getPlugins();
324
            }
325
        }
326
327
        // throw an exception if no plugins are available
328
        throw new \Exception(sprintf('Can\'t find any plugins for operation %s', $this->getOperation()));
329
    }
330
331
    /**
332
     * Map's the passed value to a boolean.
333
     *
334
     * @param string $value The value to map
335
     *
336
     * @return boolean The mapped value
337
     * @throws \Exception Is thrown, if the value can't be mapped
338
     */
339
    public function mapBoolean($value)
340
    {
341
342
        // try to map the passed value to a boolean
343
        if (isset($this->booleanMapping[$value])) {
344
            return $this->booleanMapping[$value];
345
        }
346
347
        // throw an exception if we can't convert the passed value
348
        throw new \Exception(sprintf('Can\'t convert %s to boolean', $value));
349
    }
350
351
    /**
352
     * Return's the operation, initialize from the actual operation name.
353
     *
354
     * @return \TechDivision\Import\Configuration\OperationInterface The operation instance
355
     */
356
    protected function getOperation()
357
    {
358
        return new Operation($this->getOperationName());
359
    }
360
361
    /**
362
     * Return's the operation name that has to be used.
363
     *
364
     * @param string $operationName The operation name that has to be used
365
     *
366
     * @return void
367
     */
368
    public function setOperationName($operationName)
369
    {
370
        return $this->operationName = $operationName;
371
    }
372
373
    /**
374
     * Return's the operation name that has to be used.
375
     *
376
     * @return string The operation name that has to be used
377
     */
378
    public function getOperationName()
379
    {
380
        return $this->operationName;
381
    }
382
383
    /**
384
     * Set's the Magento installation directory.
385
     *
386
     * @param string $installationDir The Magento installation directory
387
     *
388
     * @return void
389
     */
390
    public function setInstallationDir($installationDir)
391
    {
392
        $this->installationDir = $installationDir;
393
    }
394
395
    /**
396
     * Return's the Magento installation directory.
397
     *
398
     * @return string The Magento installation directory
399
     */
400
    public function getInstallationDir()
401
    {
402
        return $this->installationDir;
403
    }
404
405
    /**
406
     * Set's the source directory that has to be watched for new files.
407
     *
408
     * @param string $sourceDir The source directory
409
     *
410
     * @return void
411
     */
412
    public function setSourceDir($sourceDir)
413
    {
414
        $this->sourceDir = $sourceDir;
415
    }
416
417
    /**
418
     * Return's the source directory that has to be watched for new files.
419
     *
420
     * @return string The source directory
421
     */
422
    public function getSourceDir()
423
    {
424
        return $this->sourceDir;
425
    }
426
427
    /**
428
     * Set's the target directory with the files that has been imported.
429
     *
430
     * @param string $targetDir The target directory
431
     *
432
     * @return void
433
     */
434
    public function setTargetDir($targetDir)
435
    {
436
        $this->targetDir = $targetDir;
437
    }
438
439
    /**
440
     * Return's the target directory with the files that has been imported.
441
     *
442
     * @return string The target directory
443
     */
444
    public function getTargetDir()
445
    {
446
        return $this->targetDir;
447
    }
448
449
    /**
450
     * Set's the Magento edition, EE or CE.
451
     *
452
     * @param string $magentoEdition The Magento edition
453
     *
454
     * @return void
455
     */
456
    public function setMagentoEdition($magentoEdition)
457
    {
458
        $this->magentoEdition = $magentoEdition;
459
    }
460
461
    /**
462
     * Return's the Magento edition, EE or CE.
463
     *
464
     * @return string The Magento edition
465
     */
466
    public function getMagentoEdition()
467
    {
468
        return $this->magentoEdition;
469
    }
470
471
    /**
472
     * Return's the Magento version, e. g. 2.1.0.
473
     *
474
     * @param string $magentoVersion The Magento version
475
     *
476
     * @return void
477
     */
478
    public function setMagentoVersion($magentoVersion)
479
    {
480
        $this->magentoVersion = $magentoVersion;
481
    }
482
483
    /**
484
     * Return's the Magento version, e. g. 2.1.0.
485
     *
486
     * @return string The Magento version
487
     */
488
    public function getMagentoVersion()
489
    {
490
        return $this->magentoVersion;
491
    }
492
493
    /**
494
     * Return's the subject's source date format to use.
495
     *
496
     * @return string The source date format
497
     */
498
    public function getSourceDateFormat()
499
    {
500
        return $this->sourceDateFormat;
501
    }
502
503
    /**
504
     * Set's the subject's source date format to use.
505
     *
506
     * @param string $sourceDateFormat The source date format
507
     *
508
     * @return void
509
     */
510
    public function setSourceDateFormat($sourceDateFormat)
511
    {
512
        $this->sourceDateFormat = $sourceDateFormat;
513
    }
514
515
    /**
516
     * Return's the entity type code to be used.
517
     *
518
     * @return string The entity type code to be used
519
     */
520
    public function getEntityTypeCode()
521
    {
522
        return $this->entityTypeCode;
523
    }
524
525
    /**
526
     * Set's the entity type code to be used.
527
     *
528
     * @param string $entityTypeCode The entity type code
529
     *
530
     * @return void
531
     */
532
    public function setEntityTypeCode($entityTypeCode)
533
    {
534
        $this->entityTypeCode = $entityTypeCode;
535
    }
536
537
    /**
538
     * Return's the multiple field delimiter character to use, default value is comma (,).
539
     *
540
     * @return string The multiple field delimiter character
541
     */
542
    public function getMultipleFieldDelimiter()
543
    {
544
        return $this->multipleFieldDelimiter;
545
    }
546
547
    /**
548
     * Return's the multiple value delimiter character to use, default value is comma (|).
549
     *
550
     * @return string The multiple value delimiter character
551
     */
552
    public function getMultipleValueDelimiter()
553
    {
554
        return $this->multipleValueDelimiter;
555
    }
556
557
    /**
558
     * Return's the delimiter character to use, default value is comma (,).
559
     *
560
     * @return string The delimiter character
561
     */
562
    public function getDelimiter()
563
    {
564
        return $this->delimiter;
565
    }
566
567
    /**
568
     * The enclosure character to use, default value is double quotation (").
569
     *
570
     * @return string The enclosure character
571
     */
572
    public function getEnclosure()
573
    {
574
        return $this->enclosure;
575
    }
576
577
    /**
578
     * The escape character to use, default value is backslash (\).
579
     *
580
     * @return string The escape character
581
     */
582
    public function getEscape()
583
    {
584
        return $this->escape;
585
    }
586
587
    /**
588
     * The file encoding of the CSV source file, default value is UTF-8.
589
     *
590
     * @return string The charset used by the CSV source file
591
     */
592
    public function getFromCharset()
593
    {
594
        return $this->fromCharset;
595
    }
596
597
    /**
598
     * The file encoding of the CSV targetfile, default value is UTF-8.
599
     *
600
     * @return string The charset used by the CSV target file
601
     */
602
    public function getToCharset()
603
    {
604
        return $this->toCharset;
605
    }
606
607
    /**
608
     * The file mode of the CSV target file, either one of write or append, default is write.
609
     *
610
     * @return string The file mode of the CSV target file
611
     */
612
    public function getFileMode()
613
    {
614
        return $this->fileMode;
615
    }
616
617
    /**
618
     * Queries whether or not strict mode is enabled or not, default is TRUE.
619
     *
620
     * @return boolean TRUE if strict mode is enabled, else FALSE
621
     */
622
    public function isStrictMode()
623
    {
624
        return $this->strictMode;
625
    }
626
627
    /**
628
     * Remove's all configured database configuration.
629
     *
630
     * @return void
631
     */
632
    public function clearDatabases()
633
    {
634
        $this->databases->clear();
635
    }
636
637
    /**
638
     * Add's the passed database configuration.
639
     *
640
     * @param \TechDivision\Import\Configuration\DatabaseConfigurationInterface $database The database configuration
641
     *
642
     * @return void
643
     */
644
    public function addDatabase(DatabaseConfigurationInterface $database)
645
    {
646
        $this->databases->add($database);
647
    }
648
649
    /**
650
     * Return's the database configuration.
651
     *
652
     * If an explicit DB ID is specified, the method tries to return the database with this ID. If
653
     * the database configuration is NOT available, an execption is thrown.
654
     *
655
     * If no explicit DB ID is specified, the method tries to return the default database configuration,
656
     * if not available the first one.
657
     *
658
     * @return \TechDivision\Import\Configuration\Jms\Configuration\Database The database configuration
659
     * @throws \Exception Is thrown, if no database configuration is available
660
     */
661
    public function getDatabase()
662
    {
663
664
        // if a DB ID has been set, try to load the database
665
        if ($useDbId = $this->getUseDbId()) {
666
            // iterate over the configured databases and return the one with the passed ID
667
            /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
668
            foreach ($this->databases as $database) {
669
                if ($database->getId() === $useDbId) {
670
                    return $database;
671
                }
672
            }
673
674
            // throw an exception, if the database with the passed ID is NOT configured
675
            throw new \Exception(sprintf('Database with ID %s can not be found', $useDbId));
676
        }
677
678
        // iterate over the configured databases and try return the default database
679
        /** @var TechDivision\Import\Configuration\DatabaseInterface  $database */
680
        foreach ($this->databases as $database) {
681
            if ($database->isDefault()) {
682
                return $database;
683
            }
684
        }
685
686
        // try to return the first database configurtion
687
        if ($this->databases->count() > 0) {
688
            return $this->databases->first();
689
        }
690
691
        // throw an exception, if no database configuration is available
692
        throw new \Exception('There is no database configuration available');
693
    }
694
695
    /**
696
     * Return's the ArrayCollection with the configured operations.
697
     *
698
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations
699
     */
700
    public function getOperations()
701
    {
702
        return $this->operations;
703
    }
704
705
    /**
706
     * Return's the ArrayCollection with the configured loggers.
707
     *
708
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the loggers
709
     */
710
    public function getLoggers()
711
    {
712
        return $this->loggers;
713
    }
714
715
    /**
716
     * Return's the TRUE if the import artefacts have to be archived.
717
     *
718
     * @return boolean TRUE if the import artefacts have to be archived
719
     */
720
    public function haveArchiveArtefacts()
721
    {
722
        return $this->archiveArtefacts;
723
    }
724
725
    /**
726
     * The directory where the archives will be stored.
727
     *
728
     * @return string The archive directory
729
     */
730
    public function getArchiveDir()
731
    {
732
        return $this->archiveDir;
733
    }
734
735
    /**
736
     * Set's the debug mode.
737
     *
738
     * @param boolean $debugMode TRUE if debug mode is enabled, else FALSE
739
     *
740
     * @return void
741
     */
742
    public function setDebugMode($debugMode)
743
    {
744
        $this->debugMode = $debugMode;
745
    }
746
747
    /**
748
     * Queries whether or not debug mode is enabled or not, default is TRUE.
749
     *
750
     * @return boolean TRUE if debug mode is enabled, else FALSE
751
     */
752
    public function isDebugMode()
753
    {
754
        return $this->debugMode;
755
    }
756
757
    /**
758
     * Set's the log level to use.
759
     *
760
     * @param string $logLevel The log level to use
761
     *
762
     * @return void
763
     */
764
    public function setLogLevel($logLevel)
765
    {
766
        $this->logLevel = $logLevel;
767
    }
768
769
    /**
770
     * Return's the log level to use.
771
     *
772
     * @return string The log level to use
773
     */
774
    public function getLogLevel()
775
    {
776
        return $this->logLevel;
777
    }
778
779
    /**
780
     * Set's the explicit DB ID to use.
781
     *
782
     * @param string $useDbId The explicit DB ID to use
783
     *
784
     * @return void
785
     */
786
    public function setUseDbId($useDbId)
787
    {
788
        $this->useDbId = $useDbId;
789
    }
790
791
    /**
792
     * Return's the explicit DB ID to use.
793
     *
794
     * @return string The explicit DB ID to use
795
     */
796
    public function getUseDbId()
797
    {
798
        return $this->useDbId;
799
    }
800
801
    /**
802
     * Set's the PID filename to use.
803
     *
804
     * @param string $pidFilename The PID filename to use
805
     *
806
     * @return void
807
     */
808
    public function setPidFilename($pidFilename)
809
    {
810
        $this->pidFilename = $pidFilename;
811
    }
812
813
    /**
814
     * Return's the PID filename to use.
815
     *
816
     * @return string The PID filename to use
817
     */
818
    public function getPidFilename()
819
    {
820
        return $this->pidFilename;
821
    }
822
823
    /**
824
     * Return's a collection with the path to additional vendor directories.
825
     *
826
     * @return \Doctrine\Common\Collections\ArrayCollection The paths to additional vendor directories
827
     */
828
    public function getAdditionalVendorDirs()
829
    {
830
        return $this->additionalVendorDirs;
831
    }
832
833
    /**
834
     * Return's an array with the path of the Magento Edition specific extension libraries.
835
     *
836
     * @return array The paths of the Magento Edition specific extension libraries
837
     */
838
    public function getExtensionLibraries()
839
    {
840
        return $this->extensionLibraries;
841
    }
842
}
843