Passed
Push — master ( 291834...7697ac )
by Wilmer
03:06
created

Alert   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 4
1
<?php
2
3
namespace terabytesoft\widgets;
4
5
/**
6
 * Alert widget renders a message from session flash. All flash messages are displayed
7
 * in the sequence they were assigned using setFlash. You can set message as following:.
8
 *
9
 * @author Kartik Visweswaran <[email protected]>
10
 * @author Alexander Makarov <[email protected]>
11
 */
12
13
class Alert extends \yii\bootstrap4\Widget
14
{
15
    /**
16
     * @var array the alert types configuration for the flash messages.
17
     * This array is setup as $key => $value, where:
18
     * - $key is the name of the session flash variable
19
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
20
     **/
21
    public $alertTypes = [
22
        'danger' => 'alert-danger',
23
        'dark' => 'alert-dark',
24
        'info' => 'alert-info',
25
        'light' => 'alert-light',
26
        'primary' => 'alert-primary',
27
        'secondary' => 'alert-secondary',
28
        'success' => 'alert-success',
29
        'warning' => 'alert-warning',
30
    ];
31
32
    /**
33
     * @var array the options for rendering the close button tag.
34
     **/
35
    public $closeButton = [];
36
37
    public function run()
38
    {
39
        $session = \Yii::$app->getSession();
40
        $flashes = $session->getAllFlashes();
41
42
        foreach ($flashes as $type => $data) {
43
            if (isset($this->alertTypes[$type])) {
44
                $data = (array) $data;
45
                foreach ($data as $message) {
46
                    /* initialize css class for each alert box */
47
                    $this->options['class'] = $this->alertTypes[$type];
48
                    /* assign unique id to each alert box */
49
                    $this->options['id'] = $this->getId() . '-' . $type;
50
                    echo \yii\bootstrap4\Alert::widget([
51
                        'body' => $message,
52
                        'closeButton' => $this->closeButton,
53
                        'options' => $this->options,
54
                    ]);
55
                }
56
                $session->removeFlash($type);
57
            }
58
        }
59
    }
60
}
61