Completed
Push — master ( 3c8880...e2c218 )
by Dmitry
06:42 queued 02:46
created

SwitchRule::setError()   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
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\actions;
13
14
use Yii;
15
16
/**
17
 * HiPanel basic action.
18
 *
19
 * @property mixed condition
20
 */
21
class SwitchRule extends \yii\base\Component
22
{
23
    /**
24
     * @var Action parent action.
25
     */
26
    public $switch;
27
28
    /**
29
     * @var string rule unique name and condition if not given explicitly.
30
     */
31
    public $name;
32
33
    /**
34
     * @var boolean whether to save data before running action
35
     */
36
    public $save = false;
37
38
    /**
39
     * @var boolean whether to generate a flash notification with success or error message
40
     */
41
    public $flash = true;
42
43
    /**
44
     * @var mixed rule condition, can be object in future.
45
     */
46
    protected $_condition;
47
48
    public function setCondition($condition)
49
    {
50
        $this->_condition = $condition;
51
    }
52
53
    /**
54
     * If no condition the name is used instead.
55
     */
56
    public function getCondition()
57
    {
58
        return $this->_condition ?: $this->name;
59
    }
60
61
    /**
62
     * Synthetic ID for the ruled action.
63
     *
64
     * @param string $postfix
65
     * @return string
66
     */
67
    public function getId($postfix = null)
68
    {
69
        return $this->switch->id . ' ' . $this->name . ($postfix ? ' ' . $postfix : '');
70
    }
71
72
    /**
73
     * Run action.
74
     *
75
     * @param string $postfix
76
     * @return mixed result of the action
77
     */
78
    public function runAction($postfix = null)
79
    {
80
        $longId = $this->getId($postfix);
81
        $action = $this->switch->controller->hasInternalAction($longId) ? $longId : $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\actions\SwitchRule>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
        return $this->switch->controller->runAction($action);
83
    }
84
85
    public function run($postfix = null)
86
    {
87
        return $this->runAction($postfix);
88
    }
89
90
    /**
91
     * Setter for action. Saves the action to the controller.
92
     *
93
     * @param mixed $action action config.
94
     * @param null $postfix
95
     */
96
    public function setAction($action, $postfix = null)
97
    {
98
        if (!$action) {
99
            return;
100
        }
101
        if (!isset($action['parent'])) {
102
            $action['parent'] = $this->switch;
103
        }
104
        $this->switch->controller->setInternalAction($this->getId($postfix), $action);
105
    }
106
107
    public function setSuccess($success)
108
    {
109
        $this->setAction($success);
110
    }
111
112
    public function setError($error)
113
    {
114
        $this->setAction($error, 'error');
115
    }
116
117
    public function isApplicable()
118
    {
119
        if ($this->condition === 'default') {
120
            return true;
121
        }
122
123
        $requestMethod = Yii::$app->request->method;
124
        $requestType   = $this->getRequestType();
125
126
        $conditions = array_map('trim', explode('|', $this->condition));
127
        foreach ($conditions as $condition) {
128
            $condition = explode(' ', $condition);
129
130
            if (!empty($condition[1])) { // Condition if full. Examples: GET html, POST ajax
131
                $method = $condition[0];
132
                $type   = $condition[1];
133
            } else { // Condition is partial. Examples: GET, POST, html, ajax
134
                if (ctype_upper($condition[0])) {
135
                    // All letters are uppercase - then it is a request Method (POST, GET)
136
                    $method = $condition[0];
137
                    $type   = $requestType;
138
                } else { // If not - then it is a request type. Examples: html, json
139
                    $method = $requestMethod;
140
                    $type   = $condition[0];
141
                }
142
            }
143
144
            if ($method === $requestMethod && $type === $requestType) {
145
                return true;
146
            }
147
        }
148
149
        return false;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getRequestType()
156
    {
157
        $request = Yii::$app->request;
158
        if (is_array($request->post('selection'))) {
159
            return 'selection';
160
        } elseif ($request->isPjax) {
161
            return 'pjax';
162
        } elseif ($request->isAjax) {
163
            if ($request->post('hasEditable')) {
164
                return 'editableAjax';
165
            } elseif ($request->post('pk') && $request->post('name')) {
166
                return 'xeditable';
167
            } else {
168
                return 'ajax';
169
            }
170
        } else {
171
            return 'html';
172
        }
173
    }
174
}
175