Completed
Push — master ( 83baf5...2a3256 )
by Andrii
13:19
created

Application   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.48%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 4
dl 0
loc 191
ccs 66
cts 82
cp 0.8048
rs 9.2
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A loadExtraConfig() 0 4 1
A __construct() 0 5 1
A main() 0 17 2
A create() 0 10 1
A readVendorConfig() 0 6 2
A buildVendorPath() 0 4 1
A loadExtraVendor() 0 5 1
A setExtraEnv() 0 6 2
A setExtraConfig() 0 14 3
A setExtraParams() 0 6 3
A setExtraAliases() 0 6 3
A setExtraModules() 0 6 3
B setExtraComponents() 0 11 5
A createControllerByID() 0 20 4
A runRequest() 0 9 2
1
<?php
2
/**
3
 * Automation tool mixed with code generator for easier continuous development.
4
 *
5
 * @link      https://github.com/hiqdev/hidev
6
 * @package   hidev
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\base;
12
13
use Exception;
14
use hiqdev\composer\config\Builder;
15
use Yii;
16
use yii\base\InvalidParamException;
17
use yii\base\ViewContextInterface;
18
use yii\console\Exception as ConsoleException;
19
use yii\helpers\ArrayHelper;
20
21
/**
22
 * The Application.
23
 */
24
class Application extends \yii\console\Application implements ViewContextInterface
25
{
26
    protected $_viewPath;
27
28
    protected $_config;
29
30
    protected $_first = true;
31 5
32
    public function __construct($config = [])
33 5
    {
34 5
        $this->_config = $config;
35 5
        parent::__construct($config);
36
    }
37
38
    /**
39
     * Creates application with given config and runs it.
40
     * @param array $config
41
     * @return int exit code
42
     */
43
    public static function main(array $config)
44
    {
45
        try {
46
            $app = static::create($config);
47
            $exitCode = $app->run();
48
        } catch (Exception $e) {
49
            /*if ($e instanceof InvalidParamException || $e instanceof ConsoleException) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
                Yii::error($e->getMessage());
51
                $exitCode = 1;
52
            } else {
53
                throw $e;
54
            }*/
55
                throw $e;
56
        }
57
58
        return $exitCode;
59
    }
60 5
61
    public static function create(array $config)
62 5
    {
63 5
        Yii::setLogger(Yii::createObject('hidev\base\Logger'));
64 5
        $config = ArrayHelper::merge(
65
            static::readVendorConfig($config['vendorPath'], 'hidev'),
66 5
            $config
67
        );
68 5
69
        return new static($config);
70
    }
71 5
72
    public static function readVendorConfig($vendor, $name)
73 5
    {
74
        $path = Yii::getAlias(static::buildVendorPath($vendor, $name));
75
76 5
        return file_exists($path) ? require $path : [];
77
    }
78 5
79
    public static function buildVendorPath($vendor, $name)
80 5
    {
81
        return Builder::path($name, $vendor);
0 ignored issues
show
Unused Code introduced by
The call to Builder::path() has too many arguments starting with $vendor.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
82
    }
83 2
84
    public function loadExtraConfig($path)
85 2
    {
86 2
        $this->setExtraConfig(static::readExtraConfig($path));
0 ignored issues
show
Bug introduced by
The method readExtraConfig() does not seem to exist on object<hidev\base\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
    }
88
89
    /**
90
     * Load extra config files.
91
     * @param string $vendor path to vendor dir
92
     */
93
    public function loadExtraVendor($vendor)
94
    {
95
        $this->setExtraEnv(static::readVendorConfig($vendor, 'dotenv'));
96
        $this->setExtraConfig(static::readVendorConfig($vendor, 'hidev'));
97
    }
98
99
    /**
100
     * Sets extra environment variables.
101 2
     * @param array $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
102
     */
103 2
    public function setExtraEnv($vars)
0 ignored issues
show
Coding Style introduced by
setExtraEnv uses the super-global variable $_ENV 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...
104 2
    {
105 2
        foreach ($vars as $key => $value) {
106
            $_ENV[$key] = $value;
107 2
        }
108 2
    }
109 2
110 2
    /**
111 2
     * Implements extra configuration.
112
     * @param array $config
113 2
     */
114 2
    public function setExtraConfig($config)
115
    {
116
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
117
        $backup = $this->get('config')->getItems();
118
        $this->clear('config');
119
120 2
        foreach (['params', 'aliases', 'modules', 'components'] as $key) {
121
            if (isset($config[$key])) {
122 2
                $this->{'setExtra' . ucfirst($key)}($config[$key]);
123
            }
124
        }
125 2
126
        $this->get('config')->mergeItems($backup);
127
    }
128
129
    /**
130
     * Implements extra params.
131 2
     * @param array $params
132
     */
133 2
    public function setExtraParams($params)
134 2
    {
135 2
        if (is_array($params) && !empty($params)) {
136 2
            $this->params = ArrayHelper::merge($this->params, $params);
137
        }
138
    }
139
140
    /**
141
     * Implements extra aliases.
142
     * @param array $aliases
143
     */
144
    public function setExtraAliases($aliases)
145
    {
146
        if (is_array($aliases) && !empty($aliases)) {
147
            $this->setAliases($aliases);
148
        }
149
    }
150
151
    /**
152
     * Implements extra modules.
153
     * @param array $modules
154 2
     */
155
    public function setExtraModules($modules)
156 2
    {
157 2
        if (is_array($modules) && !empty($modules)) {
158 2
            $this->setModules($modules);
159 2
        }
160 2
    }
161 2
162 2
    /**
163 2
     * Implements extra components.
164 2
     * Does NOT touch already instantiated components.
165
     * @param array $components
166 5
     */
167
    public function setExtraComponents($components)
168
    {
169 5
        if (is_array($components) && !empty($components)) {
170 5
            foreach ($components as $id => $component) {
171 5
                if ($this->has($id, true)) {
172 5
                    unset($components[$id]);
173 2
                }
174 2
            }
175 5
            $this->setComponents($components);
176
        }
177 5
    }
178 5
179
    public function createControllerByID($id)
180
    {
181 4
        /// skip start for init goal
182 4
        if ($this->_first) {
183
            $this->_first = false;
184 4
            static $skips = ['init' => 1, 'clone' => 1, '--version' => 1];
185
            if (!isset($skips[$id])) {
186
                $this->runRequest('start');
187
            }
188
        }
189
190
        if ($this->get('config')->hasGoal($id)) {
191
            return $this->get('config')->get($id);
192 5
        }
193
194 5
        $controller = parent::createControllerByID($id);
195 5
        $this->get('config')->set($id, $controller);
196 5
197 5
        return $controller;
198
    }
199 5
200
    /**
201
     * Run request.
202
     * @param string|array $query
203
     * @return Response
204
     */
205
    public function runRequest($query)
206
    {
207
        $request = Yii::createObject([
208
            'class'  => 'hidev\base\Request',
209
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
210
        ]);
211
212
        return $this->handleRequest($request);
213
    }
214
}
215