CommonBehavior::normalizeTasks()   A
last analyzed

Complexity

Conditions 6
Paths 11

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
cc 6
eloc 8
nc 11
nop 1
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 6.0493
rs 9.2222
c 0
b 0
f 0
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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\console;
12
13
use hidev\base\Controller;
14
use hidev\helpers\Helper;
15
use Yii;
16
use yii\console\Request;
17
18
/**
19
 * Common controller behavior.
20
 */
21
class CommonBehavior extends \yii\base\Behavior
22
{
23 1
    public function events()
24
    {
25
        return [
26 1
            Controller::EVENT_BEFORE_ACTION => 'onBeforeAction',
27
            Controller::EVENT_AFTER_ACTION  => 'onAfterAction',
28
        ];
29
    }
30
31
    protected $beforeResult;
32
33
    public function onBeforeAction($event)
34
    {
35
        $result = $this->runRequests($event->sender->before);
36
        if (!Helper::isResponseOk($result)) {
37
            $this->beforeResult = $result;
38
            $event->isValid = false;
39
        }
40
    }
41
42
    public function getBeforeResult()
43
    {
44
        return $this->beforeResult;
45
    }
46
47
    public function onAfterAction($event)
48
    {
49
        $this->runRequests($event->sender->after);
50
    }
51
52
    public function runRequests($requests)
53
    {
54
        foreach ($this->normalizeTasks($requests) as $request => $enabled) {
55
            if ($enabled) {
56
                $response = $this->runRequest($request);
57
                if (!Helper::isResponseOk($response)) {
58
                    return $response;
59
                }
60
            }
61
        }
62
    }
63
64 1
    public function normalizeTasks($tasks)
65
    {
66 1
        if (!$tasks) {
67
            return [];
68 1
        } elseif (!is_array($tasks)) {
69 1
            $tasks = [(string) $tasks => 1];
70
        }
71 1
        $res = [];
72 1
        foreach ($tasks as $dep => $enabled) {
73 1
            $res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled);
74
        }
75
76 1
        return $res;
77
    }
78
79
    /**
80
     * Run request.
81
     * @param string|array $query
82
     * @return Response
0 ignored issues
show
Bug introduced by
The type hidev\console\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
     */
84
    public function runRequest($query)
85
    {
86
        $request = Yii::createObject([
87
            'class'  => Request::class,
88
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
89
        ]);
90
91
        return Yii::$app->handleRequest($request);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Yii::app->handleRequest($request) returns the type yii\console\Response|yii\web\Response which is incompatible with the documented return type hidev\console\Response.
Loading history...
92
    }
93
}
94