Alert::init()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 4
nc 6
nop 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace site\widgets;
9
10
/**
11
 * Alert widget renders a message from session flash. All flash messages are displayed
12
 * in the sequence they were assigned using setFlash. You can set message as following:
13
 *
14
 * - \Yii::$app->session->setFlash('error', 'This is the message');
15
 * - \Yii::$app->session->setFlash('success', 'This is the message');
16
 * - \Yii::$app->session->setFlash('info', 'This is the message');
17
 *
18
 * @author Kartik Visweswaran <[email protected]>
19
 * @author Alexander Makarov <[email protected]>
20
 */
21
class Alert extends \yii\bootstrap\Widget
22
{
23
    /**
24
     * @var array the alert types configuration for the flash messages.
25
     * This array is setup as $key => $value, where:
26
     * - $key is the name of the session flash variable
27
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
28
     */
29
    public $alertTypes = [
30
        'error'   => 'alert-danger',
31
        'danger'  => 'alert-danger',
32
        'success' => 'alert-success',
33
        'info'    => 'alert-info',
34
        'warning' => 'alert-warning'
35
    ];
36
37
    /**
38
     * @var array the options for rendering the close button tag.
39
     */
40
    public $closeButton = [];
41
42
    public function init()
43
    {
44
        parent::init();
45
46
        $session = \Yii::$app->session;
47
        $flashes = $session->getAllFlashes();
0 ignored issues
show
Bug introduced by
The method getAllFlashes() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        /** @scrutinizer ignore-call */ 
48
        $flashes = $session->getAllFlashes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
49
50
        foreach ($flashes as $type => $message) {
51
            if (isset($this->alertTypes[$type])) {
52
                /* initialize css class for each alert box */
53
                $this->options['class'] = $this->alertTypes[$type] . $appendCss;
54
55
                /* assign unique id to each alert box */
56
                $this->options['id'] = $this->getId() . '-' . $type;
57
58
                echo \yii\bootstrap\Alert::widget([
59
                    'body' => $message,
60
                    'closeButton' => $this->closeButton,
61
                    'options' => $this->options,
62
                ]);
63
            }
64
        }
65
    }
66
}
67