ChatManager::isUserExistsInChat()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 5
eloc 8
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 13
rs 8.8571
1
<?php
2
namespace jones\wschat\components;
3
4
use Yii;
5
use jones\wschat\collections\History;
6
7
/**
8
 * Class ChatManager
9
 * @package \jones\wschat\components
10
 */
11
class ChatManager
12
{
13
    /** @var \jones\wschat\components\User[] */
14
    private $users = [];
15
    /** @var string a namespace of class to get user instance */
16
    public $userClassName = null;
17
18
    /**
19
     * Check if user exists in list
20
     * return resource id if user in current chat - else null
21
     *
22
     * @access private
23
     * @param $id
24
     * @param $chatId
25
     * @return null|int
26
     */
27
    public function isUserExistsInChat($id, $chatId)
28
    {
29
        foreach ($this->users as $rid => $user) {
30
            $chat = $user->getChat();
31
            if (!$chat) {
32
                continue;
33
            }
34
            if ($user->id == $id && $chat->getUid() == $chatId) {
35
                return $rid;
36
            }
37
        }
38
        return null;
39
    }
40
41
    /**
42
     * Add new user to manager
43
     *
44
     * @access public
45
     * @param integer $rid
46
     * @param mixed $id
47
     * @param array $props
48
     * @return void
49
     */
50
    public function addUser($rid, $id, array $props = [])
51
    {
52
        $user = new User($id, $this->userClassName, $props);
53
        $user->setRid($rid);
54
        $this->users[$rid] = $user;
55
    }
56
57
    /**
58
     * Return if exists user chat room
59
     *
60
     * @access public
61
     * @param $rid
62
     * @return \jones\wschat\components\ChatRoom|null
63
     */
64
    public function getUserChat($rid)
65
    {
66
        $user = $this->getUserByRid($rid);
67
        return $user ? $user->getChat() : null;
68
    }
69
70
    /**
71
     * Find chat room by id, if not exists create new chat room
72
     * and assign to user by resource id
73
     *
74
     * @access public
75
     * @param $chatId
76
     * @param $rid
77
     * @return \jones\wschat\components\ChatRoom|null
78
     */
79
    public function findChat($chatId, $rid)
80
    {
81
        $chat = null;
82
        $storedUser = $this->getUserByRid($rid);
83
        foreach ($this->users as $user) {
84
            $userChat = $user->getChat();
85
            if (!$userChat) {
86
                continue;
87
            }
88
            if ($userChat->getUid() == $chatId) {
89
                $chat = $userChat;
90
                Yii::info('User('.$user->id.') will be joined to: '.$chatId, 'chat');
91
                break;
92
            }
93
        }
94
        if (!$chat) {
95
            Yii::info('New chat room: '.$chatId.' for user: '.$storedUser->id, 'chat');
96
            $chat = new ChatRoom();
97
            $chat->setUid($chatId);
98
        }
99
        $storedUser->setChat($chat);
100
        return $chat;
101
    }
102
103
    /**
104
     * Get user by resource id
105
     *
106
     * @access public
107
     * @param $rid
108
     * @return User
109
     */
110
    public function getUserByRid($rid)
111
    {
112
        return !empty($this->users[$rid]) ? $this->users[$rid] : null;
113
    }
114
115
    /**
116
     * Find user by resource id and remove it from chat
117
     *
118
     * @access public
119
     * @param $rid
120
     * @return void
121
     */
122
    public function removeUserFromChat($rid)
123
    {
124
        $user = $this->getUserByRid($rid);
125
        if (!$user) {
126
            return;
127
        }
128
        $chat = $user->getChat();
129
        if ($chat) {
130
            $chat->removeUser($user);
131
        }
132
        unset($this->users[$rid]);
133
    }
134
135
    /**
136
     * Store chat message
137
     *
138
     * @access public
139
     * @param \jones\wschat\components\User $user
140
     * @param \jones\wschat\components\ChatRoom $chat
141
     * @param string $message
142
     */
143
    public function storeMessage(User $user, ChatRoom $chat, $message)
144
    {
145
        $params = [
146
            'chat_id' => $chat->getUid(),
147
            'chat_title' => $chat->title,
148
            'user_id' => $user->getId(),
149
            'username' => $user->username,
150
            'avatar_16' => $user->avatar_16,
151
            'avatar_32' => $user->avatar_32,
152
            'message' => $message['message'],
153
            'timestamp' => $message['timestamp']
154
        ];
155
        AbstractStorage::factory()->storeMessage($params);
156
    }
157
158
    /**
159
     * Load user chat history
160
     *
161
     * @access public
162
     * @param mixed $chatId
163
     * @param integer $limit
164
     * @return array
165
     */
166
    public function getHistory($chatId, $limit = 10)
167
    {
168
        $data = AbstractStorage::factory()->getHistory($chatId, $limit);
169
        return array_reverse($data);
170
    }
171
}
172