UloginWidget   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 105
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 23 4
A run() 0 17 2
A setStateListener() 0 12 1
1
<?php
2
/**
3
 * @link https://github.com/Konstantin-Vl/yii2-ulogin-widget
4
 * @copyright Copyright (c) 2016-2019 Konstantin Voloshchuk
5
 * @license https://github.com/Konstantin-Vl/yii2-ulogin-widget/blob/master/LICENSE
6
 */
7
8
namespace kosv\ulogin\widget;
9
10
use yii\base\Widget;
11
12
/**
13
 * Ulogin widget for yii2.
14
 *
15
 * @author Konstantin Voloshchuk <[email protected]>
16
 * @since 2.0
17
 */
18
class UloginWidget extends Widget
19
{
20
    /**
21
     * @var array Layouts buttons
22
     * @see http://ulogin.ru/help.php#buttons
23
     */
24
    public $buttons = [];
25
26
    /**
27
     * @var string HTML The class attribute
28
     * @see http://htmlbook.ru/html/attr/class
29
     */
30
    public $classAttr = '';
31
32
    /**
33
     * @var array Js event handlers
34
     * @see https://ulogin.ru/help.php#listeners
35
     */
36
    public $eventListeners = [];
37
38
    /**
39
     * @var array Ulogin options
40
     * @see https://ulogin.ru/help.php
41
     */
42
    public $options = [];
43
44
    /**
45
     * @var string Ulogin init buttons
46
     */
47
    private $_initButtons;
48
49
    /**
50
     * @var string Ulogin init options
51
     */
52
    private $_initOptions;
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function init()
58
    {
59
        $initOptions = '';
60
        foreach ($this->options as $option => $value) {
61
            if (is_array($value)) {
62
                $initOptions .= $option . '=' . implode(',', $value) . ';';
63
            } else {
64
                $initOptions .= $option . '=' . $value . ';';
65
            }
66
        }
67
68
        $initButtons = [];
69
        foreach ($this->buttons as $button) {
70
            $data = "data-uloginbutton=\"{$button['provider']}\"";
71
            $initButtons[] = $button['layout']($data);
72
        }
73
74
        $this->_initOptions = $initOptions;
75
        $this->_initButtons = $initButtons;
0 ignored issues
show
Documentation Bug introduced by
It seems like $initButtons of type array is incompatible with the declared type string of property $_initButtons.

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
77
78
        parent::init();
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function run()
85
    {
86
87
        UloginAsset::register($this->getView());
88
89
        // Register event listeners
90
        foreach ($this->eventListeners as $event => $listener) {
91
            $this->setStateListener($event, $listener);
92
        }
93
94
        return $this->render('widget', [
95
            'class' => $this->classAttr,
96
            'buttons' => $this->_initButtons,
97
            'id' => $this->id,
98
            'initOptions' => $this->_initOptions,
99
        ]);
100
    }
101
102
    /**
103
     * Sets the event handler in the event ulogin
104
     *
105
     * @param string $event Js event name
106
     * @param string $listener Js event handler
107
     * @return $this
108
     * @see https://ulogin.ru/help.php#listeners
109
     */
110
    public function setStateListener($event, $listener)
111
    {
112
        $js = sprintf(
113
            'uLogin.setStateListener("%s", "%s", %s);',
114
            $this->id,
115
            $event,
116
            $listener
117
        );
118
119
        $this->getView()->registerJs($js);
120
        return $this;
121
    }
122
}