1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
171 | } |
||
172 |