NotyWidget   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 152
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 152
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
        self::TYPE_SUCCESS => [],
70
        self::TYPE_INFO => [],
71
        self::TYPE_ALERT => [],
72
        self::TYPE_ERROR => [],
73
        self::TYPE_WARNING => []
74
    ];
75
76
    /**
77
     * @var array
78
     */
79
    public $options = [
80
        'progressBar' => true,
81
        'timeout' => false,
82
        'layout' => self::LAYOUT_TOP_RIGHT,
83
        'dismissQueue' => true,
84
        'theme' => self::THEME_RELAX,
85
    ];
86
87
    /** @var yii\web\View */
88
    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...
89
90
    /**
91
     * @inheritdoc
92
     */
93 2
    public function init()
94
    {
95 2
        parent::init();
96 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...
97 2
        NotyAsset::register($this->_view);
98 2
    }
99
100
    /**
101
     * @return string|void
102
     */
103 2
    public function run()
104
    {
105 2
        $session = Yii::$app->session;
106 2
        $flashes = $session->getAllFlashes();
107
108
109 2
        foreach ($flashes as $key => $item) {
110
            if (is_array($item)) {
111
112
                if (!isset($this->typeMap[$item[0]])) {
113
                    continue;
114
                }
115
116
                $typeAlert = $this->typeMap[$item[0]];
117
                $typeOptions = $this->typeOptions[$typeAlert] ?? [];
118
                $oldOptions = $this->options;
119
120
                if (isset($item[2])) {
121
                    $this->typeOptions[$typeAlert] = array_merge($typeOptions, $item[2] ?? []);
122
                }
123
124
                if (isset($item[3])) {
125
                    $this->options = array_merge($oldOptions, $item[3] ?? []);
126
                }
127
128
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
129
130
                $options['type'] = $typeAlert;
131
                $options['text'] = $item[1];
132
133
                $this->register($options);
134
                $this->typeOptions[$typeAlert] = $typeOptions;
135
                $this->options = $oldOptions;
136
            } else {
137
138
                if (!isset($this->typeMap[$key])) {
139
                    continue;
140
                }
141
142
                $typeAlert = $this->typeMap[$key];
143
                $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
144
145
                foreach ((array)$item as $i => $message) {
146
                    $options['type'] = $typeAlert;
147
                    $options['text'] = $message;
148
149
                    $this->register($options);
150
                }
151
            }
152
            $session->removeFlash($key);
153
        }
154 2
    }
155
156
    /**
157
     * @param array $options
158
     */
159
    public function register($options = [])
160
    {
161
        $optionsEncoded = Json::encode($options);
162
        $this->_view->registerJs("new Noty($optionsEncoded).show();");
163
    }
164
}
165