Completed
Push — master ( e2bab1...fc7bc8 )
by Andrii
15:03
created

Application::createControllerByID()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 8.8571
cc 5
eloc 10
nc 6
nop 1
crap 30
1
<?php
2
3
/*
4
 * Task runner, code generator and build tool for easier continuos integration
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\base;
13
14
use Exception;
15
use Yii;
16
use yii\base\InvalidParamException;
17
use yii\base\Module;
18
use yii\base\ViewContextInterface;
19
use yii\console\Exception as ConsoleException;
20
use yii\helpers\ArrayHelper;
21
22
/**
23
 * The Application.
24
 */
25
class Application extends \yii\console\Application implements ViewContextInterface
26
{
27
    protected $_viewPath;
28
29
    protected $_config;
30
31
    protected $_first = true;
32
33
    public function __construct($config = [])
34
    {
35
        $this->_config = $config;
36
        parent::__construct($config);
37
    }
38
39
    /**
40
     * Creates application with given config and runs it.
41
     * @param array $config
42
     * @return int exit code
43
     */
44
    public static function main(array $config)
45
    {
46
        try {
47
            Yii::setLogger(Yii::createObject('hidev\base\Logger'));
48
            $config = ArrayHelper::merge(
49
                static::readExtraVendor($config['vendorPath']),
50
                $config
51
            );
52
            $exitCode = (new static($config))->run();
53
        } catch (Exception $e) {
54
            if ($e instanceof InvalidParamException || $e instanceof ConsoleException) {
55
                Yii::error($e->getMessage());
56
                $exitCode = 1;
57
            } else {
58
                throw $e;
59
            }
60
        }
61
62
        return $exitCode;
63
    }
64
65
    public static function readExtraVendor($dir)
66
    {
67
        return require $dir . '/yiisoft/yii2-extraconfig.php';
68
    }
69
70
    /**
71
     * Load extra config files.
72
     * @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...
73
     * @return void
74
     */
75
    public function loadExtraVendor($dir)
76
    {
77
        $this->setExtraConfig(static::readExtraVendor($dir));
78
    }
79
80
    /**
81
     * Implements extra configuration.
82
     * @param array $config
83
     * @return void
84
     */
85
    public function setExtraConfig($config)
86
    {
87
        $this->_config = $config = ArrayHelper::merge($config, $this->_config);
88
89
        if (!empty($config['aliases'])) {
90
            $this->setAliases($config['aliases']);
91
        }
92
        if (!empty($config['modules'])) {
93
            $this->setModules($config['modules']);
94
            /*$this->setModules(ArrayHelper::merge(
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
95
                $config['modules'],
96
                ArrayHelper::getItems($this->modules, array_keys($config['modules']))
97
            ));*/
98
        }
99
        if (!empty($config['components'])) {
100
            foreach ($config['components'] as $id => $component) {
101
                if ($this->has($id, true)) {
102
                    unset($config['components'][$id]);
103
                }
104
            }
105
            $this->setComponents($config['components']);
106
        }
107
    }
108
109
    /*public function getViewPath()
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% 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...
110
    {
111
        if ($this->_viewPath === null) {
112
            $this->_viewPath = Yii::getAlias('@app/views');
113
        }
114
115
        return $this->_viewPath;
116
    }*/
117
118
    public function createControllerByID($id)
119
    {
120
        if ($this->_first) {
121
            $this->_first = false;
122
            if ($id && !$this->get('config')->hasGoal($id)) {
123
                $this->runRequest('start');
124
            }
125
        }
126
127
        if ($this->get('config')->hasGoal($id)) {
128
            return $this->get('config')->get($id);
129
        }
130
131
        $controller = parent::createControllerByID($id);
132
        $this->get('config')->set($id, $controller);
133
134
        return $controller;
135
    }
136
137
    public function runRequest($string)
138
    {
139
        $request = Yii::createObject([
140
            'class'  => 'hidev\base\Request',
141
            'params' => array_filter(explode(' ', $string)),
142
        ]);
143
144
        return $this->handleRequest($request);
145
    }
146
147
    public function runAction($route, $params = [])
148
    {
149
        try {
150
            return Module::runAction($route, $params);
151
        } catch (InvalidRouteException $e) {
0 ignored issues
show
Bug introduced by
The class hidev\base\InvalidRouteException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
152
            throw new Exception("Unknown command \"$route\".", 0, $e);
153
        }
154
    }
155
}
156