Alert   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 24
c 1
b 0
f 0
dl 0
loc 44
ccs 14
cts 14
cp 1
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 8
    public function run()
38
    {
39 8
        $session = \Yii::$app->getSession();
40 8
        $flashes = $session->getAllFlashes();
41
42 8
        foreach ($flashes as $type => $data) {
43 8
            if (isset($this->alertTypes[$type])) {
44 8
                $data = (array) $data;
45 8
                foreach ($data as $message) {
46
                    /* initialize css class for each alert box */
47 8
                    $this->options['class'] = $this->alertTypes[$type];
48
                    /* assign unique id to each alert box */
49 8
                    $this->options['id'] = $this->getId() . '-' . $type;
50 8
                    echo \yii\bootstrap4\Alert::widget([
51 8
                        'body' => $message,
52 8
                        'closeButton' => $this->closeButton,
53 8
                        'options' => $this->options,
54
                    ]);
55
                }
56 8
                $session->removeFlash($type);
57
            }
58
        }
59 8
    }
60
}
61