SwitchAction::run()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 19
cp 0
rs 8.6506
cc 7
nc 6
nop 0
crap 56
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\actions;
12
13
use hiqdev\hiart\Collection;
14
use yii\base\InvalidConfigException;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Class SwitchAction.
19
 *
20
 * @property Collection|mixed $collection
21
 */
22
class SwitchAction extends Action implements \ArrayAccess, \IteratorAggregate, \yii\base\Arrayable
23
{
24
    use \hiqdev\yii2\collection\ManagerTrait;
25
26
    /**
27
     * @var string|callable the success message or a callback, that returns string.
28
     * Gets arguments
29
     */
30
    public $success;
31
32
    /**
33
     * @var string the error message
34
     */
35
    public $error;
36
37
    /**
38
     * @var SwitchRule instance of the current running rule
39
     */
40
    public $rule;
41
42
    /** {@inheritdoc} */
43
    public function init()
44
    {
45
        parent::init();
46
47
        $this->addItems($this->getDefaultRules());
48
    }
49
50
    /**
51
     * @return array the default rules for the action.
52
     * You can override this method in child classes to set own default rules.
53
     */
54
    protected function getDefaultRules()
55
    {
56
        return [];
57
    }
58
59
    public function getItemConfig($name = null, array $config = [])
60
    {
61
        return [
62
            'class'   => SwitchRule::class,
63
            'name'    => $name,
64
            'switch'  => $this,
65
            'save'    => ArrayHelper::remove($config, 'save'),
66
            'success' => ArrayHelper::remove($config, 'success', $config),
67
            'error'   => ArrayHelper::remove($config, 'error'),
68
            'flash'   => ArrayHelper::remove($config, 'flash', true),
69
        ];
70
    }
71
72
    public function run()
73
    {
74
        foreach ($this->keys() as $k) {
75
            $rule = $this->getItem($k);
76
            if ($rule instanceof SwitchRule && $rule->isApplicable()) {
77
                $oldRule    = $this->rule;
78
                $this->rule = $rule;
79
                $error      = $this->perform();
80
                $type       = $error ? 'error' : 'success';
81
                if ($rule->save && $rule->flash) {
82
                    $this->addFlash($type, $error);
0 ignored issues
show
Bug introduced by
It seems like $error defined by $this->perform() on line 79 can also be of type boolean; however, hipanel\actions\Action::addFlash() does only seem to accept string|null, 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...
83
                }
84
                $result     = $rule->run($type);
85
                $this->rule = $oldRule;
86
87
                return $result;
88
            }
89
        }
90
91
        throw new InvalidConfigException('Broken SwitchAction, no applicable rule found');
92
    }
93
94
    /**
95
     * Does perform only if rule has 'save' enabled.
96
     */
97
    public function perform()
98
    {
99
        if (!$this->rule->save) {
100
            return false;
101
        }
102
103
        return parent::perform();
104
    }
105
}
106