Completed
Pull Request — master (#48)
by Tim
02:56
created

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