Completed
Pull Request — develop (#1)
by Tom
13:24 queued 06:18
created

Config::checkConfigCommandAlias()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 27
rs 8.439
cc 6
eloc 16
nc 6
nop 1
1
<?php
2
/*
3
 * @author Tom Klingenberg <[email protected]>
4
 */
5
6
namespace N98\Magento\Application;
7
8
use Composer\Autoload\ClassLoader;
9
use N98\Magento\Application;
10
use N98\Magento\Application\ConfigurationLoader;
11
use N98\Util\ArrayFunctions;
12
use N98\Util\BinaryString;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\ArgvInput;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\NullOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Class Config
21
 *
22
 * Class representing the application configuration. Created to factor out configuration related application
23
 * functionality from @see N98\Magento\Application
24
 *
25
 * @package N98\Magento\Application
26
 */
27
class Config
28
{
29
    /**
30
     * @var array config data
31
     */
32
    private $config = array();
33
34
    /**
35
     * @var ConfigurationLoader
36
     */
37
    private $loader;
38
39
    /**
40
     * @var array
41
     */
42
    private $initConfig;
43
44
    /**
45
     * @var boolean
46
     */
47
    private $isPharMode;
48
49
    /**
50
     * @var OutputInterface
51
     */
52
    private $output;
53
54
55
    /**
56
     * Config constructor.
57
     * @param array $initConfig
58
     * @param bool $isPharMode
59
     * @param OutputInterface $output [optional]
1 ignored issue
show
Documentation introduced by
Should the type for parameter $output not be null|OutputInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
60
     */
61
    public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null)
62
    {
63
        $this->initConfig = $initConfig;
64
        $this->isPharMode = (bool) $isPharMode;
65
        $this->output = $output ?: new NullOutput();
66
    }
67
68
    /**
69
     * alias magerun command in input from config
70
     *
71
     * @param InputInterface $input
72
     * @return ArgvInput|InputInterface
73
     */
74
    public function checkConfigCommandAlias(InputInterface $input)
0 ignored issues
show
Coding Style introduced by
checkConfigCommandAlias uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
75
    {
76
        if ($this->hasConfigCommandAliases()) {
77
            foreach ($this->config['commands']['aliases'] as $alias) {
78
                if (is_array($alias)) {
79
                    $aliasCommandName = key($alias);
80
                    if ($input->getFirstArgument() == $aliasCommandName) {
81
                        $aliasCommandParams = array_slice(BinaryString::trimExplodeEmpty(' ',
82
                            $alias[$aliasCommandName]), 1);
83
                        if (count($aliasCommandParams) > 0) {
84
                            // replace with aliased data
85
                            $mergedParams = array_merge(
86
                                array_slice($_SERVER['argv'], 0, 2),
87
                                $aliasCommandParams,
88
                                array_slice($_SERVER['argv'], 2)
89
                            );
90
                            $input        = new ArgvInput($mergedParams);
91
                        }
92
                    }
93
                }
94
            }
95
96
            return $input;
97
        }
98
99
        return $input;
100
    }
101
102
    /**
103
     * @param Command $command
104
     */
105
    public function registerConfigCommandAlias(Command $command)
106
    {
107
        if ($this->hasConfigCommandAliases()) {
108
            foreach ($this->config['commands']['aliases'] as $alias) {
109
                if (!is_array($alias)) {
110
                    continue;
111
                }
112
113
                $aliasCommandName = key($alias);
114
                $commandString    = $alias[$aliasCommandName];
115
116
                list($originalCommand) = explode(' ', $commandString);
117
                if ($command->getName() == $originalCommand) {
118
                    $currentCommandAliases   = $command->getAliases();
119
                    $currentCommandAliases[] = $aliasCommandName;
120
                    $command->setAliases($currentCommandAliases);
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    private function hasConfigCommandAliases()
130
    {
131
        return isset($this->config['commands']['aliases']) && is_array($this->config['commands']['aliases']);
132
    }
133
134
    /**
135
     * @param Application $application
136
     */
137
    public function registerCustomCommands(Application $application)
138
    {
139
        if (isset($this->config['commands']['customCommands'])
140
            && is_array($this->config['commands']['customCommands'])
141
        ) {
142
            foreach ($this->config['commands']['customCommands'] as $commandClass) {
143
                if (is_array($commandClass)) { // Support for key => value (name -> class)
144
                    $resolvedCommandClass = current($commandClass);
145
                    /** @var Command $command */
146
                    $command = new $resolvedCommandClass();
147
                    $command->setName(key($commandClass));
148
                } else {
149
                    /** @var Command $command */
150
                    $command = new $commandClass();
151
                }
152
                $application->add($command);
153
154
                $output = $this->output;
155
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
156
                    $output->writeln(
157
                        '<debug>Added command </debug><comment>' . get_class($command) . '</comment>'
158
                    );
159
                }
160
            }
161
        }
162
    }
163
164
    /**
165
     * Adds autoloader prefixes from user's config
166
     *
167
     * @param ClassLoader $autoloader
168
     */
169
    public function registerCustomAutoloaders(ClassLoader $autoloader)
170
    {
171
        $output = $this->output;
172
173 View Code Duplication
        if (isset($this->config['autoloaders']) && is_array($this->config['autoloaders'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
            foreach ($this->config['autoloaders'] as $prefix => $path) {
175
                $autoloader->add($prefix, $path);
176
177
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
178
                    $output->writeln(
179
                        '<debug>Registrered PSR-2 autoloader </debug> <info>'
180
                        . $prefix . '</info> -> <comment>' . $path . '</comment>'
181
                    );
182
                }
183
            }
184
        }
185
186 View Code Duplication
        if (isset($this->config['autoloaders_psr4']) && is_array($this->config['autoloaders_psr4'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
            foreach ($this->config['autoloaders_psr4'] as $prefix => $path) {
188
                $autoloader->addPsr4($prefix, $path);
189
190
                if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
191
                    $output->writeln(
192
                        '<debug>Registrered PSR-4 autoloader </debug> <info>'
193
                        . $prefix . ' </info> -> <comment>' . $path . '</comment>'
194
                    );
195
                }
196
            }
197
        }
198
    }
199
200
    /**
201
     * @param array $config
202
     */
203
    public function setConfig(array $config)
204
    {
205
        $this->config = $config;
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    public function getConfig()
212
    {
213
        return $this->config;
214
    }
215
216
    /**
217
     * @param ConfigurationLoader $configurationLoader
218
     *
219
     * @return $this
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
220
     */
221
    public function setConfigurationLoader(ConfigurationLoader $configurationLoader) {
222
        $this->loader = $configurationLoader;
223
    }
224
225
    /**
226
     * @return ConfigurationLoader
227
     */
228
    public function getLoader()
229
    {
230
        if (!$this->loader) {
231
            $this->loader = $this->createLoader($this->initConfig, $this->isPharMode, $this->output);
232
            $this->initConfig = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $initConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
233
        }
234
235
        return $this->loader;
236
    }
237
238
    public function load()
239
    {
240
        $this->config = $this->getLoader()->toArray();
241
    }
242
243
    /**
244
     * @param array $initConfig
245
     * @param bool $isPharMode
246
     * @param OutputInterface $output
247
     *
248
     * @return ConfigurationLoader
249
     */
250
    public function createLoader(array $initConfig, $isPharMode, OutputInterface $output)
251
    {
252
        $config = ArrayFunctions::mergeArrays($this->config, $initConfig);
253
254
        $loader = new ConfigurationLoader($config, $isPharMode, $output);
255
256
        return $loader;
257
    }
258
}
259