Completed
Push — master ( 4f3b30...310b7e )
by Alexey
06:05
created

Alert   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 7
dl 0
loc 165
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A initSwal() 0 6 1
A run() 0 8 2
A getOptions() 0 16 4
A registerAnimateCss() 0 8 4
A getSession() 0 4 2
A initFlashWidget() 0 10 4
A initSwalQueue() 0 6 1
A processFlashSession() 0 13 3
A processFlashWidget() 0 11 4
1
<?php
2
3
namespace dominus77\sweetalert2;
4
5
use Yii;
6
use yii\bootstrap\Widget;
7
use yii\helpers\Json;
8
use yii\helpers\ArrayHelper;
9
use dominus77\sweetalert2\assets\SweetAlert2Asset;
10
use dominus77\sweetalert2\assets\AnimateCssAsset;
11
12
/**
13
 * Alert widget renders a message from session flash or custom messages.
14
 * @see https://sweetalert2.github.io/
15
 * @package dominus77\sweetalert2
16
 */
17
class Alert extends Widget
18
{
19
    // Modal Type
20
    const TYPE_INFO = 'info';
21
    const TYPE_ERROR = 'error';
22
    const TYPE_SUCCESS = 'success';
23
    const TYPE_WARNING = 'warning';
24
    const TYPE_QUESTION = 'question';
25
26
    // Input Type
27
    const INPUT_TYPE_TEXT = 'text';
28
    const INPUT_TYPE_EMAIL = 'email';
29
    const INPUT_TYPE_PASSWORD = 'password';
30
    const INPUT_TYPE_NUMBER = 'number';
31
    const INPUT_TYPE_RANGE = 'range';
32
    const INPUT_TYPE_TEXTAREA = 'textarea';
33
    const INPUT_TYPE_SELECT = 'select';
34
    const INPUT_TYPE_RADIO = 'radio';
35
    const INPUT_TYPE_CHECKBOX = 'checkbox';
36
    const INPUT_TYPE_FILE = 'file';
37
38
    /**
39
     * All the flash messages stored for the session are displayed and removed from the session
40
     * Defaults to false.
41
     * @var bool
42
     */
43
    public $useSessionFlash = false;
44
45
    /**
46
     * @var string alert callback
47
     */
48
    public $callback = 'function() {}';
49
50
    /**
51
     * @inheritdoc
52
     */
53 4
    public function init()
54
    {
55 4
        parent::init();
56 4
        SweetAlert2Asset::register($this->view);
57 4
    }
58
59
    /**
60
     * @param array $steps
61
     */
62 2
    public function initFlashWidget($steps = [])
63
    {
64 2
        if (!empty($steps)) {
65 2
            if (isset($steps[0]['text']) && !is_array($steps[0]['text'])) {
66 1
                $this->initSwalQueue($steps);
67
            } else {
68 1
                $this->processFlashWidget($steps);
69
            }
70
        }
71 2
    }
72
73
    /**
74
     * @param array $steps
75
     */
76 1
    public function initSwalQueue($steps = [])
77
    {
78 1
        $view = $this->getView();
79 1
        $js = "swal.queue(" . Json::encode($steps) . ");";
80 1
        $this->view->registerJs($js, $view::POS_END);
81 1
    }
82
83
    /**
84
     * @param string $options
85
     * @param string $callback
86
     */
87 2
    public function initSwal($options = '', $callback = '')
88
    {
89 2
        $view = $this->getView();
90 2
        $js = "swal({$options}).then({$callback}).catch(swal.noop);";
91 2
        $this->view->registerJs($js, $view::POS_END);
92 2
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97 3
    public function run()
98
    {
99 3
        if ($session = $this->getSession()) {
100 2
            $this->initFlashWidget($this->processFlashSession($session));
101
        } else {
102 1
            $this->initSwal($this->getOptions(), $this->callback);
103
        }
104 3
    }
105
106
    /**
107
     * @param $session bool|mixed|\yii\web\Session
108
     * @return array
109
     */
110 2
    public function processFlashSession($session)
111
    {
112 2
        $flashes = $session->getAllFlashes();
113 2
        $steps = [];
114 2
        foreach ($flashes as $type => $data) {
115 2
            $data = (array)$data;
116 2
            foreach ($data as $message) {
117 2
                array_push($steps, ['type' => $type, 'text' => $message]);
118
            }
119 2
            $session->removeFlash($type);
120
        }
121 2
        return $steps;
122
    }
123
124
    /**
125
     * @param array $steps
126
     */
127 1
    public function processFlashWidget($steps = [])
128
    {
129 1
        $params = [];
130 1
        if ($params['options'] = $steps[0]['text']) {
131 1
            $params['options']['type'] = isset($params['options']['type']) ? $params['options']['type'] : $steps[0]['type'];
132 1
            $params['callback'] = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback;
133 1
            $this->options = $params['options'];
134 1
            $this->callback = $params['callback'];
135 1
            $this->initSwal($this->getOptions(), $this->callback);
136
        }
137 1
    }
138
139
    /**
140
     * Get widget options
141
     *
142
     * @return string
143
     */
144 3
    public function getOptions()
145
    {
146 3
        if (isset($this->options['id']))
147 2
            unset($this->options['id']);
148
149 3
        $this->registerAnimateCss();
150
151 3
        if (ArrayHelper::isIndexed($this->options)) {
152 1
            $str = '';
153 1
            foreach ($this->options as $value) {
154 1
                $str .= '"' . $value . '",';
155
            }
156 1
            return chop($str, ' ,');
157
        }
158 2
        return Json::encode($this->options);
159
    }
160
161
    /**
162
     * Add support Animate.css
163
     * @see https://daneden.github.io/animate.css/
164
     */
165 3
    public function registerAnimateCss()
166
    {
167 3
        if (isset($this->options['animation']) && $this->options['animation'] === false) {
168 2
            if (isset($this->options['customClass'])) {
169 2
                AnimateCssAsset::register($this->view);
170
            }
171
        }
172 3
    }
173
174
    /**
175
     * @return bool|mixed|\yii\web\Session
176
     */
177 3
    private function getSession()
178
    {
179 3
        return $this->useSessionFlash ? Yii::$app->session : false;
180
    }
181
}
182