Completed
Push — master ( 79019d...4b80af )
by Alexey
02:01 queued 21s
created

SocialLikes   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 0
loc 152
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A renderItems() 0 7 4
A getContentTitle() 0 4 2
A registerAssets() 0 8 1
A registerClientCss() 0 6 2
A registerClientButton() 0 10 2
A init() 0 12 4
A run() 0 9 2
A registerClientOptions() 0 13 2
1
<?php
2
3
namespace dominus77\sociallikesnext;
4
5
use yii\web\View;
6
use yii\web\JsExpression;
7
use yii\helpers\Json;
8
use yii\helpers\Html;
9
use dominus77\sociallikesnext\assets\SocialLikesAsset;
10
11
/**
12
 * Class SocialLikes
13
 * @package dominus77\sociallikesnext
14
 */
15
class SocialLikes extends \yii\base\Widget
16
{
17
    const THEME_FLAT = 'flat';
18
    const THEME_LIGHT = 'light';
19
    const THEME_BIRMAN = 'birman';
20
21
    /**
22
     * Theme buttons
23
     * @var string flat, light, birman
24
     */
25
    public $theme = 'flat';
26
27
    /**
28
     * Display name of social networks, default true
29
     * @var bool
30
     */
31
    public $title = true;
32
33
    /**
34
     * @var array
35
     */
36
    public $items = [];
37
38
    /**
39
     * @var array
40
     */
41
    public $containerOptions = [];
42
43
    /**
44
     * @var array
45
     */
46
    public $clientOptions = [];
47
48
    /**
49
     * @var array
50
     */
51
    public $clientButtons = [];
52
53
    /**
54
     * @var string
55
     */
56
    public $clientCss = '';
57
58
    /**
59
     * @var string
60
     */
61
    public $id;
62
63
    /**
64
     * Initializes the widget
65
     */
66 3
    public function init()
67
    {
68 3
        parent::init();
69 3
        $this->id = $this->id ?: $this->getId();
70
        $containerOptions = [
71 3
            'id' => $this->id,
72
        ];
73 3
        $this->containerOptions = array_merge($this->containerOptions, $containerOptions);
74 3
        $this->containerOptions['class'] = (isset($this->containerOptions['class']) && !empty($this->containerOptions['class'])) ?
75 1
            $this->theme . ' social-likes ' . $this->containerOptions['class'] :
76 2
            $this->theme . ' social-likes';
77 3
    }
78
79
    /**
80
     * Run widget
81
     * @return string|void
82
     */
83 1
    public function run()
84
    {
85 1
        if (!empty($this->items)) {
86 1
            $this->registerAssets();
87 1
            echo Html::beginTag('div', $this->containerOptions) . PHP_EOL;
88 1
            $this->renderItems($this->items);
89 1
            echo Html::endTag('div') . PHP_EOL;
90
        }
91 1
    }
92
93
    /**
94
     * Render items
95
     * @param array $items
96
     */
97 1
    public function renderItems($items = [])
98
    {
99 1
        foreach ($items as $key => $options) {
100 1
            if (isset($options['serviceOptions']))
101 1
                echo Html::tag('div', $this->getContentTitle($options) ? $key : '', $options['serviceOptions']) . PHP_EOL;
102
        }
103 1
    }
104
105
    /**
106
     * @param array $options
107
     * @return bool|mixed
108
     */
109 1
    protected function getContentTitle($options = [])
110
    {
111 1
        return (isset($options['title'])) ? $options['title'] : $this->title;
112
    }
113
114
    /**
115
     * Register resources
116
     */
117 1
    protected function registerAssets()
118
    {
119 1
        $view = $this->getView();
120 1
        SocialLikesAsset::register($view);
121 1
        $this->registerClientCss($view);
122 1
        $this->registerClientButton($view);
123 1
        $this->registerClientOptions($view);
124 1
    }
125
126
    /**
127
     * @param \yii\web\View $view
128
     */
129 1
    protected function registerClientCss($view)
130
    {
131 1
        if (!empty($this->clientCss)) {
132 1
            $view->registerCss($this->clientCss);
133
        }
134 1
    }
135
136
    /**
137
     * @param \yii\web\View $view
138
     */
139 1
    protected function registerClientButton($view)
140
    {
141 1
        if (!empty($this->clientButtons)) {
142 1
            $clientButtons = Json::encode($this->clientButtons);
143 1
            $script = new JsExpression("
144 1
                var socialLikesButtons = {$clientButtons}
145
            ");
146 1
            $view->registerJs($script, View::POS_BEGIN);
147
        }
148 1
    }
149
150
    /**
151
     * @param \yii\web\View $view
152
     */
153 2
    public function registerClientOptions($view)
154
    {
155 2
        if (!empty($this->clientOptions)) {
156 1
            $options = Json::encode($this->clientOptions);
157 1
            $id = $this->containerOptions['id'];
158 1
            $script = new JsExpression("
159 1
                var container = document.getElementById('{$id}');
160
                var socialLikes = SocialLikesNext.default;
161 1
                socialLikes(container, {$options});
162
            ");
163 1
            $view->registerJs($script);
164
        }
165 2
    }
166
}
167