Completed
Push — master ( f3f979...855d01 )
by Tim
10s
created

Configuration::setIgnorePid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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