Completed
Push — master ( 549d74...5f8498 )
by Tim
02:25
created

Configuration   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 2
cbo 4
dl 0
loc 314
ccs 0
cts 99
cp 0
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
F factory() 0 77 11
A getDatabase() 0 4 1
A getOperations() 0 4 1
A getSubjects() 0 14 3
A getOperation() 0 4 1
A setOperationName() 0 4 1
A getOperationName() 0 4 1
A setInstallationDir() 0 4 1
A getInstallationDir() 0 4 1
A setUtilityClassName() 0 4 1
A getUtilityClassName() 0 4 1
A setMagentoEdition() 0 4 1
A getMagentoEdition() 0 4 1
A setMagentoVersion() 0 4 1
A getMagentoVersion() 0 4 1
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 database configuration.
82
     *
83
     * @var TechDivision\Import\Configuration\Database
84
     * @Type("TechDivision\Import\Cli\Configuration\Database")
85
     */
86
    protected $database;
87
88
    /**
89
     * ArrayCollection with the information of the configured operations.
90
     *
91
     * @var \Doctrine\Common\Collections\ArrayCollection
92
     * @Type("ArrayCollection<TechDivision\Import\Cli\Configuration\Operation>")
93
     */
94
    protected $operations;
95
96
    /**
97
     * The subject's utility class with the SQL statements to use.
98
     *
99
     * @var string
100
     * @Type("string")
101
     * @SerializedName("utility-class-name")
102
     */
103
    protected $utilityClassName;
104
105
    /**
106
     * Factory implementation to create a new initialized configuration instance.
107
     *
108
     * If command line options are specified, they will always override the
109
     * values found in the configuration file.
110
     *
111
     * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance
112
     *
113
     * @return \TechDivision\Import\Cli\Configuration The configuration instance
114
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
115
     */
116
    public static function factory(InputInterface $input)
117
    {
118
119
        // load the configuration filename we want to use
120
        $filename = $input->getOption(InputOptionKeys::CONFIGURATION);
121
122
        // load the JSON data
123
        if (!$jsonData = file_get_contents($filename)) {
124
            throw new \Exception('Can\'t load configuration file $filename');
125
        }
126
127
        // initialize the JMS serializer and load the configuration
128
        $serializer = SerializerBuilder::create()->build();
129
        /** @var \TechDivision\Import\Cli\Configuration $instance */
130
        $instance = $serializer->deserialize($jsonData, 'TechDivision\Import\Cli\Configuration', 'json');
131
132
        // query whether or not an operation name has been specified as command line
133
        // option, if yes override the value from the configuration file
134
        if ($operationName = $input->getArgument(InputArgumentKeys::OPERATION_NAME)) {
135
            $instance->setOperationName($operationName);
136
        }
137
138
        // query whether or not a Magento installation directory has been specified as command line
139
        // option, if yes override the value from the configuration file
140
        if ($installationDir = $input->getOption(InputOptionKeys::INSTALLATION_DIR)) {
141
            $instance->setInstallationDir($installationDir);
142
        }
143
144
        // query whether or not a Magento edition has been specified as command line
145
        // option, if yes override the value from the configuration file
146
        if ($magentoEdition = $input->getOption(InputOptionKeys::MAGENTO_EDITION)) {
147
            $instance->setMagentoEdition($magentoEdition);
148
        }
149
150
        // query whether or not a Magento version has been specified as command line
151
        // option, if yes override the value from the configuration file
152
        if ($magentoVersion = $input->getOption(InputOptionKeys::MAGENTO_VERSION)) {
153
            $instance->setMagentoVersion($magentoVersion);
154
        }
155
156
        // query whether or not a PDO DSN has been specified as command line
157
        // option, if yes override the value from the configuration file
158
        if ($dsn = $input->getOption(InputOptionKeys::DB_PDO_DSN)) {
159
            $instance->getDatabase()->setDsn($dsn);
160
        }
161
162
        // query whether or not a DB username has been specified as command line
163
        // option, if yes override the value from the configuration file
164
        if ($username = $input->getOption(InputOptionKeys::DB_USERNAME)) {
165
            $instance->getDatabase()->setUsername($username);
166
        }
167
168
        // query whether or not a DB password has been specified as command line
169
        // option, if yes override the value from the configuration file
170
        if ($password = $input->getOption(InputOptionKeys::DB_PASSWORD)) {
171
            $instance->getDatabase()->setPassword($password);
172
        }
173
174
        // load the source date format to use
175
        $sourceDateFormat = $input->getOption(InputOptionKeys::SOURCE_DATE_FORMAT);
176
177
        // extend the subjects with the parent configuration instance
178
        /** @var \TechDivision\Import\Cli\Configuration\Subject $subject */
179
        foreach ($instance->getSubjects() as $subject) {
180
            // set the configuration instance on the subject
181
            $subject->setConfiguration($instance);
182
183
            // query whether or not a source date format has been specified as command
184
            // line  option, if yes override the value from the configuration file
185
            if (!empty($sourceDateFormat)) {
186
                $subject->setSourceDateFormat($sourceDateFormat);
187
            }
188
        }
189
190
        // return the initialized configuration instance
191
        return $instance;
192
    }
193
194
    /**
195
     * Return's the database configuration.
196
     *
197
     * @return \TechDivision\Import\Cli\Configuration\Database The database configuration
198
     */
199
    public function getDatabase()
200
    {
201
        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...
202
    }
203
204
    /**
205
     * Return's the ArrayCollection with the configured operations.
206
     *
207
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operations
208
     */
209
    public function getOperations()
210
    {
211
        return $this->operations;
212
    }
213
214
    /**
215
     * Return's the array with the subjects of the operation to use.
216
     *
217
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the subjects
218
     * @throws \Exception Is thrown, if no subjects are available for the actual operation
219
     */
220
    public function getSubjects()
221
    {
222
223
        // iterate over the operations and return the subjects of the actual one
224
        /** @var TechDivision\Import\Configuration\OperationInterface $operation */
225
        foreach ($this->getOperations() as $operation) {
226
            if ($this->getOperation()->equals($operation)) {
227
                return $operation->getSubjects();
228
            }
229
        }
230
231
        // throw an exception if no subjects are available
232
        throw new \Exception(sprintf('Can\'t find any subjects for operation %s', $this->getOperation()));
233
    }
234
235
    /**
236
     * Return's the operation, initialize from the actual operation name.
237
     *
238
     * @return \TechDivision\Import\Configuration\OperationInterface The operation instance
239
     */
240
    protected function getOperation()
241
    {
242
        return new Operation($this->getOperationName());
243
    }
244
245
    /**
246
     * Return's the operation name that has to be used.
247
     *
248
     * @param string $operationName The operation name that has to be used
249
     *
250
     * @return void
251
     */
252
    public function setOperationName($operationName)
253
    {
254
        return $this->operationName = $operationName;
255
    }
256
257
    /**
258
     * Return's the operation name that has to be used.
259
     *
260
     * @return string The operation name that has to be used
261
     */
262
    public function getOperationName()
263
    {
264
        return $this->operationName;
265
    }
266
267
    /**
268
     * Set's the Magento installation directory.
269
     *
270
     * @param string $installationDir The Magento installation directory
271
     *
272
     * @return void
273
     */
274
    public function setInstallationDir($installationDir)
275
    {
276
        $this->installationDir = $installationDir;
277
    }
278
279
    /**
280
     * Return's the Magento installation directory.
281
     *
282
     * @return string The Magento installation directory
283
     */
284
    public function getInstallationDir()
285
    {
286
        return $this->installationDir;
287
    }
288
289
    /**
290
     * Return's the utility class with the SQL statements to use.
291
     *
292
     * @param string $utilityClassName The utility class name
293
     *
294
     * @return void
295
     */
296
    public function setUtilityClassName($utilityClassName)
297
    {
298
        return $this->utilityClassName = $utilityClassName;
299
    }
300
301
    /**
302
     * Return's the utility class with the SQL statements to use.
303
     *
304
     * @return string The utility class name
305
     */
306
    public function getUtilityClassName()
307
    {
308
        return $this->utilityClassName;
309
    }
310
311
    /**
312
     * Set's the Magento edition, EE or CE.
313
     *
314
     * @param string $magentoEdition The Magento edition
315
     *
316
     * @return void
317
     */
318
    public function setMagentoEdition($magentoEdition)
319
    {
320
        $this->magentoEdition = $magentoEdition;
321
    }
322
323
    /**
324
     * Return's the Magento edition, EE or CE.
325
     *
326
     * @return string The Magento edition
327
     */
328
    public function getMagentoEdition()
329
    {
330
        return $this->magentoEdition;
331
    }
332
333
    /**
334
     * Return's the Magento version, e. g. 2.1.0.
335
     *
336
     * @param string $magentoVersion The Magento version
337
     *
338
     * @return void
339
     */
340
    public function setMagentoVersion($magentoVersion)
341
    {
342
        $this->magentoVersion = $magentoVersion;
343
    }
344
345
    /**
346
     * Return's the Magento version, e. g. 2.1.0.
347
     *
348
     * @return string The Magento version
349
     */
350
    public function getMagentoVersion()
351
    {
352
        return $this->magentoVersion;
353
    }
354
}
355