Completed
Push — master ( 0f5983...882712 )
by Alexey
01:21
created

NotyWidget   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 25.64%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 146
ccs 10
cts 39
cp 0.2564
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
B run() 0 52 8
A register() 0 5 1
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 NotyWidget
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
            if (is_array($item)) {
105
106
                if (!isset($this->typeMap[$item[0]])) {
107
                    continue;
108
                }
109
110
                $typeAlert = $this->typeMap[$item[0]];
111
                $typeOptions = $this->typeOptions[$typeAlert];
112
                $oldOptions = $this->options;
113
114
                if (isset($item[2])) {
115
                    $this->typeOptions[$typeAlert] = array_merge($typeOptions, $item[2] ?? []);
116
                }
117
118
                if (isset($item[3])) {
119
                    $this->options = array_merge($oldOptions, $item[3] ?? []);
120
                }
121
122
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
123
124
                $options['type'] = $typeAlert;
125
                $options['text'] = $item[1];
126
127
                $this->register($options);
128
                $this->typeOptions[$typeAlert] = $typeOptions;
129
                $this->options = $oldOptions;
130
            } else {
131
132
                if (!isset($this->typeMap[$key])) {
133
                    continue;
134
                }
135
136
                $typeAlert = $this->typeMap[$key];
137
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
138
139
                foreach ((array)$item as $i => $message) {
140
                    $options['type'] = $typeAlert;
141
                    $options['text'] = $message;
142
143
                    $this->register($options);
144
                }
145
            }
146
            $session->removeFlash($key);
147
        }
148 2
    }
149
150
    /**
151
     * @param array $options
152
     */
153
    public function register($options = [])
154
    {
155
        $optionsEncoded = Json::encode($options);
156
        $this->_view->registerJs("new Noty($optionsEncoded).show();");
157
    }
158
}
159