Completed
Push — master ( fbb4c3...48aefa )
by Andrii
04:08
created

Controller::isResponseOk()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
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 hidev\controllers\CommonBehavior;
14
15
/**
16
 * Basic controller.
17
 */
18
abstract class Controller extends \yii\console\Controller
19
{
20
    use GettersTrait;
21
22
    public $layout = false;
23
24
    protected $_before = [];
25
    protected $_after  = [];
26
27 1
    public function behaviors()
28
    {
29
        return [
30 1
            CommonBehavior::class,
31
        ];
32
    }
33
34 1
    public function setBefore($requests)
35
    {
36 1
        $this->_before = array_merge($this->getBefore(), $this->normalizeTasks($requests));
0 ignored issues
show
Documentation Bug introduced by
The method normalizeTasks does not exist on object<hidev\base\Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
37 1
    }
38
39 1
    public function getBefore()
40
    {
41 1
        return $this->_before;
42
    }
43
44
    public function setAfter($requests)
45
    {
46
        $this->_after = array_merge($this->getAfter(), $this->normalizeTasks($requests));
0 ignored issues
show
Documentation Bug introduced by
The method normalizeTasks does not exist on object<hidev\base\Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
    }
48
49
    public function getAfter()
50
    {
51
        return $this->_after;
52
    }
53
54
    public function readline($prompt)
55
    {
56
        return readline($prompt);
57
    }
58
59
    public function readpassword($prompt)
60
    {
61
        echo $prompt;
62
        system('stty -echo');
63
        $password = rtrim(fgets(STDIN), PHP_EOL);
64
        system('stty echo');
65
        echo "\n";
66
67
        return $password;
68
    }
69
70
    /**
71
     * Runs list of actions.
72
     * @param null|string|array $actions
73
     * @return int|Response exit code
74
     */
75
    public function runActions($actions)
76
    {
77
        foreach ($this->normalizeTasks($actions) as $action => $enabled) {
0 ignored issues
show
Documentation Bug introduced by
The method normalizeTasks does not exist on object<hidev\base\Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
78
            if ($enabled) {
79
                $res = $this->runAction($action);
80
                if (!static::isResponseOk($res)) {
81
                    return $res;
82
                }
83
            }
84
        }
85
86
        return 0;
87
    }
88
89
    /**
90
     * Is response Ok.
91
     * @param Response|int $response
92
     * @return bool
93
     */
94
    public static function isResponseOk($response)
95
    {
96
        return !(is_object($response) ? $response->exitStatus : $response);
97
    }
98
}
99