Completed
Push — master ( d7c45f...0f5983 )
by Alexey
01:53
created

NotyWidget::run()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 8.0189

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 28
cts 30
cp 0.9333
rs 7.8028
c 0
b 0
f 0
cc 8
nc 9
nop 0
crap 8.0189

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace dominus77\noty;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Json;
8
9
/**
10
 * Class Noty
11
 * @package dominus77\noty
12
 */
13
class NotyWidget extends Widget
14
{
15
    /**
16
     * Themes
17
     */
18
    const THEME_BOOTSTRAP_3 = 'bootstrap-v3';
19
    const THEME_BOOTSTRAP_4 = 'bootstrap-v4';
20
    const THEME_LIGHT = 'light';
21
    const THEME_METROUI = 'metroui';
22
    const THEME_MINT = 'mint';
23
    const THEME_NEST = 'nest';
24
    const THEME_RELAX = 'relax';
25
    const THEME_SEMANTICUI = 'semanticui';
26
    const THEME_SUNSET = 'sunset';
27
28
    /**
29
     * Types
30
     */
31
    const TYPE_ALERT = 'alert';
32
    const TYPE_SUCCESS = 'success';
33
    const TYPE_WARNING = 'warning';
34
    const TYPE_DANGER = 'error';
35
    const TYPE_ERROR = 'error';
36
    const TYPE_INFO = 'info';
37
38
    /**
39
     * Layouts
40
     */
41
    const LAYOUT_TOP = 'top';
42
    const LAYOUT_TOP_LEFT = 'topLeft';
43
    const LAYOUT_TOP_CENTER = 'topCenter';
44
    const LAYOUT_TOP_RIGHT = 'topRight';
45
    const LAYOUT_CENTER = 'center';
46
    const LAYOUT_CENTER_LEFT = 'centerLeft';
47
    const LAYOUT_CENTER_RIGHT = 'centerRight';
48
    const LAYOUT_BOTTOM = 'bottom';
49
    const LAYOUT_BOTTOM_LEFT = 'bottomLeft';
50
    const LAYOUT_BOTTOM_CENTER = 'bottomCenter';
51
    const LAYOUT_BOTTOM_RIGHT = 'bottomRight';
52
53
    /**
54
     * @var array
55
     */
56
    private $typeMap = [
57
        'alert' => self::TYPE_ALERT,
58
        'success' => self::TYPE_SUCCESS,
59
        'warning' => self::TYPE_WARNING,
60
        'danger' => self::TYPE_DANGER,
61
        'error' => self::TYPE_ERROR,
62
        'info' => self::TYPE_INFO,
63
    ];
64
65
    /**
66
     * @var array
67
     */
68
    public $typeOptions = [];
69
70
    /**
71
     * @var array
72
     */
73
    public $options = [
74
        'progressBar' => true,
75
        'timeout' => false,
76
        'layout' => self::LAYOUT_TOP_RIGHT,
77
        'dismissQueue' => true,
78
        'theme' => self::THEME_RELAX,
79
    ];
80
81
    /** @var yii\web\View */
82
    private $_view;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
83
84
    /**
85
     * @inheritdoc
86
     */
87 2
    public function init()
88
    {
89 2
        parent::init();
90 2
        $this->_view = $this->getView();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getView() of type object<yii\web\View> is incompatible with the declared type object<dominus77\noty\yii\web\View> of property $_view.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91 2
        NotyAsset::register($this->_view);
92 2
    }
93
94
    /**
95
     * @return string|void
96
     */
97 2
    public function run()
98
    {
99 2
        $session = Yii::$app->session;
100 2
        $flashes = $session->getAllFlashes();
101
102
103 2
        foreach ($flashes as $key => $item) {
104 2
            if (is_array($item)) {
105
106 1
                if (!isset($this->typeMap[$item[0]])) {
107
                    continue;
108
                }
109
110 1
                $typeAlert = $this->typeMap[$item[0]];
111 1
                $typeOptions = $this->typeOptions[$typeAlert];
112 1
                $oldOptions = $this->options;
113
114 1
                if (isset($item[2])) {
115 1
                    $this->typeOptions[$typeAlert] = array_merge($typeOptions, $item[2] ?? []);
116
                }
117
118 1
                if (isset($item[3])) {
119 1
                    $this->options = array_merge($oldOptions, $item[3] ?? []);
120
                }
121
122 1
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
123
124 1
                $options['type'] = $typeAlert;
125 1
                $options['text'] = $item[1];
126
127 1
                $this->register($options);
128 1
                $this->typeOptions[$typeAlert] = $typeOptions;
129 1
                $this->options = $oldOptions;
130
            } else {
131
132 1
                if (!isset($this->typeMap[$key])) {
133
                    continue;
134
                }
135
136 1
                $typeAlert = $this->typeMap[$key];
137 1
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
138
139 1
                foreach ((array)$item as $i => $message) {
140 1
                    $options['type'] = $typeAlert;
141 1
                    $options['text'] = $message;
142
143 1
                    $this->register($options);
144
                }
145
            }
146 2
            $session->removeFlash($key);
147
        }
148 2
    }
149
150
    /**
151
     * @param array $options
152
     */
153 2
    public function register($options = [])
154
    {
155 2
        $optionsEncoded = Json::encode($options);
156 2
        $this->_view->registerJs("new Noty($optionsEncoded).show();");
157 2
    }
158
}
159