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

ConfigurationFactory   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 5
dl 0
loc 142
rs 10
c 0
b 0
f 0
ccs 0
cts 64
cp 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F load() 0 127 20
1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\ConfigurationFactory
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 Rhumsaa\Uuid\Uuid;
25
use Symfony\Component\Console\Input\InputInterface;
26
use TechDivision\Import\Cli\Command\InputOptionKeys;
27
use TechDivision\Import\Cli\Command\InputArgumentKeys;
28
use TechDivision\Import\Configuration\Jms\Configuration\Database;
29
30
/**
31
 * The configuration factory 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 ConfigurationFactory extends \TechDivision\Import\Configuration\Jms\ConfigurationFactory
40
{
41
42
    /**
43
     * Factory implementation to create a new initialized configuration instance.
44
     *
45
     * If command line options are specified, they will always override the
46
     * values found in the configuration file.
47
     *
48
     * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance
49
     *
50
     * @return \TechDivision\Import\Cli\Configuration The configuration instance
51
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
52
     */
53
    public static function load(InputInterface $input)
54
    {
55
56
        // load the configuration from the file with the given filename
57
        $instance = static::factory($input->getOption(InputOptionKeys::CONFIGURATION));
58
59
        // query whether or not an operation name has been specified as command line
60
        // option, if yes override the value from the configuration file
61
        if ($operationName = $input->getArgument(InputArgumentKeys::OPERATION_NAME)) {
62
            $instance->setOperationName($operationName);
63
        }
64
65
        // query whether or not a Magento installation directory has been specified as command line
66
        // option, if yes override the value from the configuration file
67
        if ($installationDir = $input->getOption(InputOptionKeys::INSTALLATION_DIR)) {
68
            $instance->setInstallationDir($installationDir);
69
        }
70
71
        // query whether or not a directory for the source files has been specified as command line
72
        // option, if yes override the value from the configuration file
73
        if ($sourceDir = $input->getOption(InputOptionKeys::SOURCE_DIR)) {
74
            $instance->setSourceDir($sourceDir);
75
        }
76
77
        // query whether or not a directory containing the imported files has been specified as command line
78
        // option, if yes override the value from the configuration file
79
        if ($targetDir = $input->getOption(InputOptionKeys::TARGET_DIR)) {
80
            $instance->setTargetDir($targetDir);
81
        }
82
83
        // query whether or not a source date format has been specified as command
84
        // line  option, if yes override the value from the configuration file
85
        if ($sourceDateFormat = $input->getOption(InputOptionKeys::SOURCE_DATE_FORMAT)) {
86
            $instance->setSourceDateFormat($sourceDateFormat);
87
        }
88
89
        // query whether or not a Magento edition has been specified as command line
90
        // option, if yes override the value from the configuration file
91
        if ($magentoEdition = $input->getOption(InputOptionKeys::MAGENTO_EDITION)) {
92
            $instance->setMagentoEdition($magentoEdition);
93
        }
94
95
        // query whether or not a Magento version has been specified as command line
96
        // option, if yes override the value from the configuration file
97
        if ($magentoVersion = $input->getOption(InputOptionKeys::MAGENTO_VERSION)) {
98
            $instance->setMagentoVersion($magentoVersion);
99
        }
100
101
        // query whether or not a DB ID has been specified as command line
102
        // option, if yes override the value from the configuration file
103
        if ($useDbId = $input->getOption(InputOptionKeys::USE_DB_ID)) {
104
            $instance->setUseDbId($useDbId);
105
        } else {
106
            // query whether or not a PDO DSN has been specified as command line
107
            // option, if yes override the value from the configuration file
108
            if ($dsn = $input->getOption(InputOptionKeys::DB_PDO_DSN)) {
109
                // first REMOVE all other database configurations
110
                $instance->clearDatabases();
111
112
                // initialize a new database configuration
113
                $database = new Database();
114
                $database->setId(Uuid::uuid4()->__toString());
115
                $database->setDefault(true);
116
                $database->setDsn($dsn);
117
118
                // query whether or not a DB username has been specified as command line
119
                // option, if yes override the value from the configuration file
120
                if ($username = $input->getOption(InputOptionKeys::DB_USERNAME)) {
121
                    $database->setUsername($username);
122
                }
123
124
                // query whether or not a DB password has been specified as command line
125
                // option, if yes override the value from the configuration file
126
                if ($password = $input->getOption(InputOptionKeys::DB_PASSWORD)) {
127
                    $database->setPassword($password);
128
                }
129
130
                // add the database configuration
131
                $instance->addDatabase($database);
132
            }
133
        }
134
135
        // query whether or not the debug mode has been specified as command line
136
        // option, if yes override the value from the configuration file
137
        if ($debugMode = $input->getOption(InputOptionKeys::DEBUG_MODE)) {
138
            $instance->setDebugMode($instance->mapBoolean($debugMode));
139
        }
140
141
        // query whether or not the log level has been specified as command line
142
        // option, if yes override the value from the configuration file
143
        if ($logLevel = $input->getOption(InputOptionKeys::LOG_LEVEL)) {
144
            $instance->setLogLevel($logLevel);
145
        }
146
147
        // query whether or not a PID filename has been specified as command line
148
        // option, if yes override the value from the configuration file
149
        if ($pidFilename = $input->getOption(InputOptionKeys::PID_FILENAME)) {
150
            $instance->setPidFilename($pidFilename);
151
        }
152
153
        // extend the plugins with the main configuration instance
154
        /** @var \TechDivision\Import\Cli\Configuration\Subject $subject */
155
        foreach ($instance->getPlugins() as $plugin) {
156
            // set the configuration instance on the plugin
157
            $plugin->setConfiguration($instance);
158
159
            // query whether or not the plugin has subjects configured
160
            if ($subjects = $plugin->getSubjects()) {
161
                // extend the plugin's subjects with the main configuration instance
162
                /** @var \TechDivision\Import\Cli\Configuration\Subject $subject */
163
                foreach ($subjects as $subject) {
164
                    // set the configuration instance on the subject
165
                    $subject->setConfiguration($instance);
166
                }
167
            }
168
        }
169
170
        // query whether or not the debug mode is enabled and log level
171
        // has NOT been overwritten with a commandline option
172
        if ($instance->isDebugMode() && !$input->getOption(InputOptionKeys::LOG_LEVEL)) {
173
            // set debug log level, if log level has NOT been overwritten on command line
174
            $instance->setLogLevel(LogLevel::DEBUG);
175
        }
176
177
        // return the initialized configuration instance
178
        return $instance;
179
    }
180
}
181