Completed
Push — master ( a47edd...b4e878 )
by Alexey
01:55 queued 24s
created

NotyWidget   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 91
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A run() 0 21 4
1
<?php
2
3
namespace dominus77\noty;
4
5
use Yii;
6
use yii\base\Widget;
7
use yii\helpers\Json;
8
9
/**
10
 * Class Noty
11
 * @package dominus77\noty
12
 */
13
class NotyWidget extends Widget
14
{
15
    /**
16
     * Themes
17
     */
18
    const THEME_BOOTSTRAP_3 = 'bootstrap-v3';
19
    const THEME_BOOTSTRAP_4 = 'bootstrap-v4';
20
    const THEME_LIGHT = 'light';
21
    const THEME_METROUI = 'metroui';
22
    const THEME_MINT = 'mint';
23
    const THEME_NEST = 'nest';
24
    const THEME_RELAX = 'relax';
25
    const THEME_SEMANTICUI = 'semanticui';
26
    const THEME_SUNSET = 'sunset';
27
28
    /**
29
     * Types
30
     */
31
    const TYPE_ALERT = 'alert';
32
    const TYPE_SUCCESS = 'success';
33
    const TYPE_WARNING = 'warning';
34
    const TYPE_DANGER = 'error';
35
    const TYPE_ERROR = 'error';
36
    const TYPE_INFO = 'info';
37
38
    /**
39
     * @var array
40
     */
41
    private $typeMap = [
42
        'alert' => self::TYPE_ALERT,
43
        'success' => self::TYPE_SUCCESS,
44
        'warning' => self::TYPE_WARNING,
45
        'danger' => self::TYPE_DANGER,
46
        'error' => self::TYPE_ERROR,
47
        'info' => self::TYPE_INFO,
48
    ];
49
50
    /**
51
     * @var array
52
     */
53
    public $typeOptions = [];
54
55
    /**
56
     * @var array
57
     */
58
    public $options = [
59
        'progressBar' => true,
60
        'timeout' => false,
61
        'layout' => 'topRight',
62
        'dismissQueue' => true,
63
        'theme' => self::THEME_RELAX,
64
    ];
65
66
    /** @var yii\web\View */
67
    private $_view;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
68
69
    /**
70
     * @inheritdoc
71
     */
72 1
    public function init()
73
    {
74 1
        parent::init();
75 1
        $this->_view = $this->getView();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getView() of type object<yii\web\View> is incompatible with the declared type object<dominus77\noty\yii\web\View> of property $_view.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76 1
        NotyAsset::register($this->_view);
77 1
    }
78
79
    /**
80
     * @return string|void
81
     */
82 1
    public function run()
83
    {
84 1
        $session = Yii::$app->session;
85 1
        $flashes = $session->getAllFlashes();
86
87 1
        foreach ($flashes as $type => $flash) {
88 1
            if (!isset($this->typeMap[$type])) {
89
                continue;
90
            }
91 1
            $typeAlert = $this->typeMap[$type];
92 1
            $options = array_merge($this->options, $this->typeOptions[$typeAlert] ?? []);
93
94 1
            foreach ((array)$flash as $i => $message) {
95 1
                $options['type'] = $typeAlert;
96 1
                $options['text'] = $message;
97 1
                $optionsEncoded = Json::encode($options);
98 1
                $this->_view->registerJs("new Noty($optionsEncoded).show();");
99
            }
100 1
            $session->removeFlash($type);
101
        }
102 1
    }
103
}
104