Completed
Push — master ( 453c86...c30384 )
by Tim
11s
created

Configuration::getArchiveDir()   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 JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\SerializerBuilder;
25
use JMS\Serializer\Annotation\SerializedName;
26
use TechDivision\Import\ConfigurationInterface;
27
use Symfony\Component\Console\Input\InputInterface;
28
use TechDivision\Import\Cli\Command\InputOptionKeys;
29
use TechDivision\Import\Cli\Command\InputArgumentKeys;
30
use TechDivision\Import\Cli\Configuration\Operation;
31
32
/**
33
 * A simple configuration implementation.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2016 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-cli-simple
39
 * @link      http://www.techdivision.com
40
 */
41
class Configuration implements ConfigurationInterface
42
{
43
44
    /**
45
     * The operation name to use.
46
     *
47
     * @var string
48
     * @Type("string")
49
     * @SerializedName("operation-name")
50
     */
51
    protected $operationName;
52
53
    /**
54
     * The Magento edition, EE or CE.
55
     *
56
     * @var string
57
     * @Type("string")
58
     * @SerializedName("magento-edition")
59
     */
60
    protected $magentoEdition = 'CE';
61
62
    /**
63
     * The Magento version, e. g. 2.1.0.
64
     *
65
     * @var string
66
     * @Type("string")
67
     * @SerializedName("magento-version")
68
     */
69
    protected $magentoVersion = '2.1.2';
70
71
    /**
72
     * The Magento installation directory.
73
     *
74
     * @var string
75
     * @Type("string")
76
     * @SerializedName("installation-dir")
77
     */
78
    protected $installationDir;
79
80
    /**
81
     * The source directory that has to be watched for new files.
82
     *
83
     * @var string
84
     * @Type("string")
85
     * @SerializedName("source-dir")
86
     */
87
    protected $sourceDir;
88
89
    /**
90
     * The target directory with the files that has been imported.
91
     *
92
     * @var string
93
     * @Type("string")
94
     * @SerializedName("target-dir")
95
     */
96
    protected $targetDir;
97
98
    /**
99
     * The database configuration.
100
     *
101
     * @var TechDivision\Import\Configuration\Database
102
     * @Type("TechDivision\Import\Cli\Configuration\Database")
103
     */
104
    protected $database;
105
106
    /**
107
     * ArrayCollection with the information of the configured operations.
108
     *
109
     * @var \Doctrine\Common\Collections\ArrayCollection
110
     * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Operation>")
111
     */
112
    protected $operations;
113
114
    /**
115
     * The subject's utility class with the SQL statements to use.
116
     *
117
     * @var string
118
     * @Type("string")
119
     * @SerializedName("utility-class-name")
120
     */
121
    protected $utilityClassName;
122
123
    /**
124
     * The source date format to use in the subject.
125
     *
126
     * @var string
127
     * @Type("string")
128
     * @SerializedName("source-date-format")
129
     */
130
    protected $sourceDateFormat = 'n/d/y, g:i A';
131
132
    /**
133
     * The subject's multiple field delimiter character for fields with multiple values, defaults to (,).
134
     *
135
     * @var string
136
     * @Type("string")
137
     * @SerializedName("multiple-field-delimiter")
138
     */
139
    protected $multipleFieldDelimiter = ',';
140
141
    /**
142
     * The subject's delimiter character for CSV files.
143
     *
144
     * @var string
145
     * @Type("string")
146
     */
147
    protected $delimiter;
148
149
    /**
150
     * The subject's enclosure character for CSV files.
151
     *
152
     * @var string
153
     * @Type("string")
154
     */
155
    protected $enclosure;
156
157
    /**
158
     * The subject's escape character for CSV files.
159
     *
160
     * @var string
161
     * @Type("string")
162
     */
163
    protected $escape;
164
165
    /**
166
     * The subject's source charset for the CSV file.
167
     *
168
     * @var string
169
     * @Type("string")
170
     * @SerializedName("from-charset")
171
     */
172
    protected $fromCharset;
173
174
    /**
175
     * The subject's target charset for a CSV file.
176
     *
177
     * @var string
178
     * @Type("string")
179
     * @SerializedName("to-charset")
180
     */
181
    protected $toCharset;
182
183
    /**
184
     * The subject's file mode for a CSV target file.
185
     *
186
     * @var string
187
     * @Type("string")
188
     * @SerializedName("file-mode")
189
     */
190
    protected $fileMode;
191
192
    /**
193
     * The flag to signal that the subject has to use the strict mode or not.
194
     *
195
     * @var boolean
196
     * @Type("boolean")
197
     * @SerializedName("strict-mode")
198
     */
199
    protected $strictMode;
200
201
    /**
202
     * The flag whether or not the import artefacts have to be archived.
203
     *
204
     * @var boolean
205
     * @Type("boolean")
206
     * @SerializedName("archive-artefacts")
207
     */
208
    protected $archiveArtefacts;
209
210
    /**
211
     * The directory where the archives will be stored.
212
     *
213
     * @var string
214
     * @Type("string")
215
     * @SerializedName("archive-dir")
216
     */
217
    protected $archiveDir;
218
219
    /**
220
     * Factory implementation to create a new initialized configuration instance.
221
     *
222
     * If command line options are specified, they will always override the
223
     * values found in the configuration file.
224
     *
225
     * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance
226
     *
227
     * @return \TechDivision\Import\Cli\Configuration The configuration instance
228
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
229
     */
230
    public static function factory(InputInterface $input)
231
    {
232
233
        // load the configuration filename we want to use
234
        $filename = $input->getOption(InputOptionKeys::CONFIGURATION);
235
236
        // load the JSON data
237
        if (!$jsonData = file_get_contents($filename)) {
238
            throw new \Exception('Can\'t load configuration file $filename');
239
        }
240
241
        // initialize the JMS serializer and load the configuration
242
        $serializer = SerializerBuilder::create()->build();
243
        /** @var \TechDivision\Import\Cli\Configuration $instance */
244
        $instance = $serializer->deserialize($jsonData, 'TechDivision\Import\Cli\Configuration', 'json');
245
246
        // query whether or not an operation name has been specified as command line
247
        // option, if yes override the value from the configuration file
248
        if ($operationName = $input->getArgument(InputArgumentKeys::OPERATION_NAME)) {
249
            $instance->setOperationName($operationName);
250
        }
251
252
        // query whether or not a Magento installation directory has been specified as command line
253
        // option, if yes override the value from the configuration file
254
        if ($installationDir = $input->getOption(InputOptionKeys::INSTALLATION_DIR)) {
255
            $instance->setInstallationDir($installationDir);
256
        }
257
258
        // query whether or not a directory for the source files has been specified as command line
259
        // option, if yes override the value from the configuration file
260
        if ($sourceDir = $input->getOption(InputOptionKeys::SOURCE_DIR)) {
261
            $instance->setSourceDir($sourceDir);
262
        }
263
264
        // query whether or not a directory containing the imported files has been specified as command line
265
        // option, if yes override the value from the configuration file
266
        if ($targetDir = $input->getOption(InputOptionKeys::TARGET_DIR)) {
267
            $instance->setTargetDir($targetDir);
268
        }
269
270
        // query whether or not a source date format has been specified as command
271
        // line  option, if yes override the value from the configuration file
272
        if ($sourceDateFormat = $input->getOption(InputOptionKeys::SOURCE_DATE_FORMAT)) {
273
            $instance->setSourceDateFormat($sourceDateFormat);
274
        }
275
276
        // query whether or not a Magento edition has been specified as command line
277
        // option, if yes override the value from the configuration file
278
        if ($magentoEdition = $input->getOption(InputOptionKeys::MAGENTO_EDITION)) {
279
            $instance->setMagentoEdition($magentoEdition);
280
        }
281
282
        // query whether or not a Magento version has been specified as command line
283
        // option, if yes override the value from the configuration file
284
        if ($magentoVersion = $input->getOption(InputOptionKeys::MAGENTO_VERSION)) {
285
            $instance->setMagentoVersion($magentoVersion);
286
        }
287
288
        // query whether or not a PDO DSN has been specified as command line
289
        // option, if yes override the value from the configuration file
290
        if ($dsn = $input->getOption(InputOptionKeys::DB_PDO_DSN)) {
291
            $instance->getDatabase()->setDsn($dsn);
292
        }
293
294
        // query whether or not a DB username has been specified as command line
295
        // option, if yes override the value from the configuration file
296
        if ($username = $input->getOption(InputOptionKeys::DB_USERNAME)) {
297
            $instance->getDatabase()->setUsername($username);
298
        }
299
300
        // query whether or not a DB password has been specified as command line
301
        // option, if yes override the value from the configuration file
302
        if ($password = $input->getOption(InputOptionKeys::DB_PASSWORD)) {
303
            $instance->getDatabase()->setPassword($password);
304
        }
305
306
        // extend the subjects with the parent configuration instance
307
        /** @var \TechDivision\Import\Cli\Configuration\Subject $subject */
308
        foreach ($instance->getSubjects() as $subject) {
309
            // set the configuration instance on the subject
310
            $subject->setConfiguration($instance);
311
        }
312
313
        // return the initialized configuration instance
314
        return $instance;
315
    }
316
317
    /**
318
     * Return's the array with the subjects of the operation to use.
319
     *
320
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the subjects
321
     * @throws \Exception Is thrown, if no subjects are available for the actual operation
322
     */
323
    public function getSubjects()
324
    {
325
326
        // iterate over the operations and return the subjects of the actual one
327
        /** @var TechDivision\Import\Configuration\OperationInterface $operation */
328
        foreach ($this->getOperations() as $operation) {
329
            if ($this->getOperation()->equals($operation)) {
330
                return $operation->getSubjects();
331
            }
332
        }
333
334
        // throw an exception if no subjects are available
335
        throw new \Exception(sprintf('Can\'t find any subjects for operation %s', $this->getOperation()));
336
    }
337
338
    /**
339
     * Return's the operation, initialize from the actual operation name.
340
     *
341
     * @return \TechDivision\Import\Configuration\OperationInterface The operation instance
342
     */
343
    protected function getOperation()
344
    {
345
        return new Operation($this->getOperationName());
346
    }
347
348
    /**
349
     * Return's the operation name that has to be used.
350
     *
351
     * @param string $operationName The operation name that has to be used
352
     *
353
     * @return void
354
     */
355
    public function setOperationName($operationName)
356
    {
357
        return $this->operationName = $operationName;
358
    }
359
360
    /**
361
     * Return's the operation name that has to be used.
362
     *
363
     * @return string The operation name that has to be used
364
     */
365
    public function getOperationName()
366
    {
367
        return $this->operationName;
368
    }
369
370
    /**
371
     * Set's the Magento installation directory.
372
     *
373
     * @param string $installationDir The Magento installation directory
374
     *
375
     * @return void
376
     */
377
    public function setInstallationDir($installationDir)
378
    {
379
        $this->installationDir = $installationDir;
380
    }
381
382
    /**
383
     * Return's the Magento installation directory.
384
     *
385
     * @return string The Magento installation directory
386
     */
387
    public function getInstallationDir()
388
    {
389
        return $this->installationDir;
390
    }
391
392
    /**
393
     * Set's the source directory that has to be watched for new files.
394
     *
395
     * @param string $sourceDir The source directory
396
     *
397
     * @return void
398
     */
399
    public function setSourceDir($sourceDir)
400
    {
401
        $this->sourceDir = $sourceDir;
402
    }
403
404
    /**
405
     * Return's the source directory that has to be watched for new files.
406
     *
407
     * @return string The source directory
408
     */
409
    public function getSourceDir()
410
    {
411
        return $this->sourceDir;
412
    }
413
414
    /**
415
     * Return's the target directory with the files that has been imported.
416
     *
417
     * @return string The target directory
418
     */
419
    public function getTargetDir()
420
    {
421
        return $this->targetDir;
422
    }
423
424
    /**
425
     * Set's the target directory with the files that has been imported.
426
     *
427
     * @param string $targetDir The target directory
428
     *
429
     * @return void
430
     */
431
    public function setTargetDir($targetDir)
432
    {
433
        $this->targetDir = $targetDir;
434
    }
435
436
    /**
437
     * Return's the utility class with the SQL statements to use.
438
     *
439
     * @param string $utilityClassName The utility class name
440
     *
441
     * @return void
442
     */
443
    public function setUtilityClassName($utilityClassName)
444
    {
445
        return $this->utilityClassName = $utilityClassName;
446
    }
447
448
    /**
449
     * Return's the utility class with the SQL statements to use.
450
     *
451
     * @return string The utility class name
452
     */
453
    public function getUtilityClassName()
454
    {
455
        return $this->utilityClassName;
456
    }
457
458
    /**
459
     * Set's the Magento edition, EE or CE.
460
     *
461
     * @param string $magentoEdition The Magento edition
462
     *
463
     * @return void
464
     */
465
    public function setMagentoEdition($magentoEdition)
466
    {
467
        $this->magentoEdition = $magentoEdition;
468
    }
469
470
    /**
471
     * Return's the Magento edition, EE or CE.
472
     *
473
     * @return string The Magento edition
474
     */
475
    public function getMagentoEdition()
476
    {
477
        return $this->magentoEdition;
478
    }
479
480
    /**
481
     * Return's the Magento version, e. g. 2.1.0.
482
     *
483
     * @param string $magentoVersion The Magento version
484
     *
485
     * @return void
486
     */
487
    public function setMagentoVersion($magentoVersion)
488
    {
489
        $this->magentoVersion = $magentoVersion;
490
    }
491
492
    /**
493
     * Return's the Magento version, e. g. 2.1.0.
494
     *
495
     * @return string The Magento version
496
     */
497
    public function getMagentoVersion()
498
    {
499
        return $this->magentoVersion;
500
    }
501
502
    /**
503
     * Return's the subject's source date format to use.
504
     *
505
     * @return string The source date format
506
     */
507
    public function getSourceDateFormat()
508
    {
509
        return $this->sourceDateFormat;
510
    }
511
512
    /**
513
     * Set's the subject's source date format to use.
514
     *
515
     * @param string $sourceDateFormat The source date format
516
     *
517
     * @return void
518
     */
519
    public function setSourceDateFormat($sourceDateFormat)
520
    {
521
        $this->sourceDateFormat = $sourceDateFormat;
522
    }
523
524
    /**
525
     * Return's the multiple field delimiter character to use, default value is comma (,).
526
     *
527
     * @return string The multiple field delimiter character
528
     */
529
    public function getMultipleFieldDelimiter()
530
    {
531
        return $this->multipleFieldDelimiter;
532
    }
533
534
    /**
535
     * Return's the delimiter character to use, default value is comma (,).
536
     *
537
     * @return string The delimiter character
538
     */
539
    public function getDelimiter()
540
    {
541
        return $this->delimiter;
542
    }
543
544
    /**
545
     * The enclosure character to use, default value is double quotation (").
546
     *
547
     * @return string The enclosure character
548
     */
549
    public function getEnclosure()
550
    {
551
        return $this->enclosure;
552
    }
553
554
    /**
555
     * The escape character to use, default value is backslash (\).
556
     *
557
     * @return string The escape character
558
     */
559
    public function getEscape()
560
    {
561
        return $this->escape;
562
    }
563
564
    /**
565
     * The file encoding of the CSV source file, default value is UTF-8.
566
     *
567
     * @return string The charset used by the CSV source file
568
     */
569
    public function getFromCharset()
570
    {
571
        return $this->fromCharset;
572
    }
573
574
    /**
575
     * The file encoding of the CSV targetfile, default value is UTF-8.
576
     *
577
     * @return string The charset used by the CSV target file
578
     */
579
    public function getToCharset()
580
    {
581
        return $this->toCharset;
582
    }
583
584
    /**
585
     * The file mode of the CSV target file, either one of write or append, default is write.
586
     *
587
     * @return string The file mode of the CSV target file
588
     */
589
    public function getFileMode()
590
    {
591
        return $this->fileMode;
592
    }
593
594
    /**
595
     * Queries whether or not strict mode is enabled or not, default is TRUE.
596
     *
597
     * @return boolean TRUE if strict mode is enabled, else FALSE
598
     */
599
    public function isStrictMode()
600
    {
601
        return $this->strictMode;
602
    }
603
604
    /**
605
     * Return's the database configuration.
606
     *
607
     * @return \TechDivision\Import\Cli\Configuration\Database The database configuration
608
     */
609
    public function getDatabase()
610
    {
611
        return $this->database;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->database; (TechDivision\Import\Cli\...\Configuration\Database) is incompatible with the return type declared by the interface TechDivision\Import\Conf...nInterface::getDatabase of type TechDivision\Import\Tech...\Configuration\Database.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
612
    }
613
614
    /**
615
     * Return's the ArrayCollection with the configured operations.
616
     *
617
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations
618
     */
619
    public function getOperations()
620
    {
621
        return $this->operations;
622
    }
623
624
    /**
625
     * Return's the TRUE if the import artefacts have to be archived.
626
     *
627
     * @return boolean TRUE if the import artefacts have to be archived
628
     */
629
    public function haveArchiveArtefacts()
630
    {
631
        return $this->archiveArtefacts;
632
    }
633
634
    /**
635
     * The directory where the archives will be stored.
636
     *
637
     * @return string The archive directory
638
     */
639
    public function getArchiveDir()
640
    {
641
        return $this->archiveDir;
642
    }
643
}
644