ChatWidget   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 0 Features 4
Metric Value
wmc 3
c 5
b 0
f 4
lcom 1
cbo 4
dl 0
loc 53
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 1
A registerJsOptions() 0 10 2
1
<?php
2
namespace jones\wschat;
3
4
use Yii;
5
use yii\base\Widget;
6
use yii\web\View;
7
use yii\helpers\Json;
8
9
/**
10
 * Class ChatWidget
11
 * @package jones\wschat
12
 */
13
class ChatWidget extends Widget
14
{
15
    /**
16
     * @var boolean set to true if widget will be run for auth users
17
     */
18
    public $auth = false;
19
    public $user_id = null;
20
    public $view = 'index';
21
    /** @var integer $port web socket port */
22
    public $port = 8080;
23
    /** @var array $chatList list of preloaded chats */
24
    public $chatList = [
25
        'id' => 1,
26
        'title' => 'All'
27
    ];
28
    /** @var string path to avatars folder */
29
    public $imgPath = '@vendor/joni-jones/yii2-wschat/assets/img';
30
31
    /**
32
     * @var boolean is user available to add nwe rooms
33
     */
34
    public $add_room = true;
35
36
    /**
37
     * @override
38
     */
39
    public function run()
40
    {
41
        $this->registerJsOptions();
42
        Yii::$app->assetManager->publish($this->imgPath);
43
        return $this->render($this->view, [
44
            'auth' => $this->auth,
45
            'add_room' => $this->add_room
46
        ]);
47
    }
48
49
    /**
50
     * Register js variables
51
     *
52
     * @access protected
53
     * @return void
54
     */
55
    protected function registerJsOptions()
56
    {
57
        $opts = [
58
            'var currentUserId = '.($this->user_id ?: 0).';',
59
            'var port = '.$this->port.';',
60
            'var chatList = '.Json::encode($this->chatList).';',
61
            'var imgPath = "'.Yii::$app->assetManager->getPublishedUrl($this->imgPath).'";',
62
        ];
63
        $this->getView()->registerJs(implode(' ', $opts), View::POS_BEGIN);
64
    }
65
}
66
 
67