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

SwitchAction::run()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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