User   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 11
c 7
b 1
f 2
lcom 2
cbo 4
dl 0
loc 115
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 4 1
A getRid() 0 4 1
A setRid() 0 4 1
A getChat() 0 4 1
A __construct() 0 6 1
B init() 0 24 5
A setChat() 0 5 1
1
<?php
2
namespace jones\wschat\components;
3
4
use Yii;
5
use yii\base\InvalidParamException;
6
7
/**
8
 * Class User
9
 * @package \jones\wschat\components
10
 *
11
 * @property mixed $id
12
 * @property string $username
13
 * @property string $avatar_16 url to avatar 16x16 image
14
 * @property string $avatar_32 url to avatar 32x32 image
15
 */
16
class User
17
{
18
    public $id;
19
    public $username;
20
    public $avatar_16;
21
    public $avatar_32;
22
    private $rid;
23
    /** @var \jones\wschat\components\ChatRoom $chat */
24
    private $chat;
25
    /** @var string */
26
    private $modelClassName = null;
27
28
    /**
29
     * @param $id
30
     * @param string $modelClassName default null
31
     * @param array $props array of properties for non auth chat users
32
     */
33
    public function __construct($id = null, $modelClassName = null, array $props = [])
34
    {
35
        $this->id = $id;
36
        $this->modelClassName = $modelClassName;
37
        $this->init($props);
38
}
39
40
    /**
41
     * Restore user attributes from cache or load it from
42
     * repository
43
     *
44
     * @access private
45
     * @param array $props
46
     * @return void
47
     */
48
    private function init(array $props = [])
49
    {
50
        $cache = Yii::$app->cache;
51
        $cache->keyPrefix = 'user';
52
        if ($cache->exists($this->id)) {
53
            $attrs = $cache->get($this->id);
54
        } else {
55
            if ($this->modelClassName) {
56
                if (!in_array('findOne', (array)get_class_methods($this->modelClassName))) {
57
                    throw new InvalidParamException(Yii::t('app', 'Model class should implements `findOne()` method'));
58
                }
59
                /** @var \yii\db\BaseActiveRecord $model */
60
                $model = call_user_func_array([$this->modelClassName, 'findOne'], ['id' => $this->id]);
61
                if (!$model) {
62
                    throw new InvalidParamException(Yii::t('app', 'User entity not found.'));
63
                }
64
                $attrs = $model->attributes;
65
            } else {
66
                $attrs = $props;
67
            }
68
            $cache->set($this->id, $attrs);
69
        }
70
        Yii::configure($this, $attrs);
71
    }
72
73
    /**
74
     * Get user id
75
     *
76
     * @param string
77
     * @return string
78
     */
79
    public function getId()
80
    {
81
        return $this->id;
82
    }
83
84
    /**
85
     * Get user resource id
86
     *
87
     * @access public
88
     * @return string
89
     */
90
    public function getRid()
91
    {
92
        return $this->rid;
93
    }
94
95
    /**
96
     * Set user resource id
97
     *
98
     * @access public
99
     * @param $rid
100
     * @return void
101
     */
102
    public function setRid($rid)
103
    {
104
        $this->rid = $rid;
105
    }
106
107
    /**
108
     * Get user chat room
109
     *
110
     * @access public
111
     * @return \jones\wschat\components\ChatRoom
112
     */
113
    public function getChat()
114
    {
115
        return $this->chat;
116
    }
117
118
    /**
119
     * Set chat room for user
120
     *
121
     * @access public
122
     * @param \jones\wschat\components\ChatRoom $chat
123
     * @return void
124
     */
125
    public function setChat(ChatRoom $chat)
126
    {
127
        $this->chat = $chat;
128
        $this->chat->addUser($this);
129
    }
130
}
131
132