Completed
Push — master ( 89135c...cdc2ca )
by Alexey
06:26
created

Alert::run()   C

Complexity

Conditions 12
Paths 44

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 22
cts 22
cp 1
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 23
nc 44
nop 0
crap 12

How to fix   Complexity   

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