Completed
Push — master ( 70cce3...6b7465 )
by Tim
02:52
created

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