PNotify   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 67
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setClientOptions() 0 4 1
A getClientOptions() 0 4 1
A run() 0 11 3
A registerNotification() 0 7 1
A registerClientScript() 0 5 1
1
<?php
2
/**
3
 * Yii2 adapter for PNotify JQuery extension
4
 *
5
 * @link      https://github.com/hiqdev/yii2-pnotify
6
 * @package   yii2-pnotify
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\pnotify;
12
13
use hiqdev\assets\pnotify\PNotifyAsset;
14
use yii\base\InvalidConfigException;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Json;
17
18
/**
19
 * Class PNotify.
20
 * Yii2 support for PNotify JS plugin.
21
 *
22
 * Please, refer to https://github.com/sciactive/pnotify for detailed information about possible call options.
23
 */
24
class PNotify extends \yii\base\Widget
25
{
26
    /**
27
     * @var array options to be passed to PNotify JS call
28
     */
29
    protected $_clientOptions = [
30
        'hide' => true,
31
        'styling' => 'bootstrap3',
32
        'buttons' => [
33
            'sticker' => false,
34
        ],
35
    ];
36
37
    /**
38
     * @var array list of notifications. Will be merged with [[clientOptions]] before notification render
39
     */
40
    public $notifications = [];
41
42
    /**
43
     * @param array $value
44
     */
45
    public function setClientOptions($value)
46
    {
47
        $this->_clientOptions = $value;
48
    }
49
50
    public function getClientOptions()
51
    {
52
        return $this->_clientOptions;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function run()
59
    {
60
        $this->registerClientScript();
61
62
        if (!is_array($this->notifications)) {
63
            throw new InvalidConfigException('Notifications list should be an array');
64
        }
65
        foreach ($this->notifications as $notification) {
66
            $this->registerNotification($notification);
67
        }
68
    }
69
70
    /**
71
     * Registers JS for PNotify plugin.
72
     * @param array $notification configuration array
73
     */
74
    protected function registerNotification(array $notification)
75
    {
76
        $view = $this->getView();
77
78
        $options = Json::encode(ArrayHelper::merge($this->getClientOptions(), $notification));
79
        $view->registerJs("new PNotify({$options});");
80
    }
81
82
    /**
83
     * Registers the needed JavaScript.
84
     */
85
    public function registerClientScript()
86
    {
87
        $view = $this->getView();
88
        PNotifyAsset::register($view);
89
    }
90
}
91