Completed
Push — master ( 61512d...295de3 )
by Andrii
03:40 queued 14s
created

CommonBehavior::getBeforeResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hidev\console;
12
13
use hidev\base\Controller;
14
use Yii;
15
use yii\console\Request;
16
17
/**
18
 * Common controller behavior.
19
 */
20
class CommonBehavior extends \yii\base\Behavior
21
{
22 1
    public function events()
23
    {
24
        return [
25 1
            Controller::EVENT_BEFORE_ACTION => 'onBeforeAction',
26
            Controller::EVENT_AFTER_ACTION  => 'onAfterAction',
27
        ];
28
    }
29
30
    protected $beforeResult;
31
32
    public function onBeforeAction($event)
33
    {
34
        $result = $this->runRequests($event->sender->before);
35
        if (!Controller::isResponseOk($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->runRequests($event->sender->before) on line 34 can also be of type null; however, hidev\base\Controller::isResponseOk() does only seem to accept object<hidev\base\Response>|integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36
            $this->beforeResult = $result;
37
            $event->isValid = false;
38
        }
39
    }
40
41
    public function getBeforeResult()
42
    {
43
        return $this->beforeResult;
44
    }
45
46
    public function onAfterAction($event)
47
    {
48
        $this->runRequests($event->sender->after);
49
    }
50
51
    public function runRequests($requests)
52
    {
53
        foreach ($this->normalizeTasks($requests) as $request => $enabled) {
54
            if ($enabled) {
55
                $response = $this->runRequest($request);
56
                if (!Controller::isResponseOk($response)) {
57
                    return $response;
58
                }
59
            }
60
        }
61
    }
62
63 1
    public function normalizeTasks($tasks)
64
    {
65 1
        if (!$tasks) {
66
            return [];
67 1
        } elseif (!is_array($tasks)) {
68 1
            $tasks = [(string) $tasks => 1];
69
        }
70 1
        $res = [];
71 1
        foreach ($tasks as $dep => $enabled) {
72 1
            $res[(string) (is_int($dep) ? $enabled : $dep)] = (bool) (is_int($dep) ? 1 : $enabled);
73
        }
74
75 1
        return $res;
76
    }
77
78
    /**
79
     * Run request.
80
     * @param string|array $query
81
     * @return Response
82
     */
83
    public function runRequest($query)
84
    {
85
        $request = Yii::createObject([
86
            'class'  => Request::class,
87
            'params' => is_array($query) ? $query : array_filter(explode(' ', $query)),
88
        ]);
89
90
        return Yii::$app->handleRequest($request);
91
    }
92
}
93