/** * Try to connect to a helpDesk after a client joined * Online available helpdesks will be assigned to new client. * New client will be pushed to the pending client queue for waiting if no available helpdesks * @param array $client the client information, containing openId, nick, avatar * @param MongoId $accountId the account UUID * @return array ['status': 'fail'] or ['status': 'ok', 'helpDesk':'...', 'channel':'....', 'coversationId':'...'] */ public static function connect($client, $accountId) { if ($targetDeskId = self::hasVIPDesk($client, $accountId)) { return self::createConnection($client, $targetDeskId, $accountId); } else { if ($targetDeskId = self::getLastestDesk($client, $accountId)) { return self::createConnection($client, $targetDeskId, $accountId); } else { $conversations = Yii::$app->cache->get('conversations' . $accountId); if ($conversations) { $minClientsCount = HelpDeskSetting::getMaxClientCount($accountId); $targetDeskId = null; foreach ($conversations as $deskId => &$clientList) { $currentCount = count($clientList); if ($currentCount < $minClientsCount) { $minClientsCount = $currentCount; $targetDeskId = $deskId; } } if (!empty($targetDeskId)) { return self::createConnection($client, $targetDeskId, $accountId); } else { //push the client into pending clients's queue PendingClient::enQueue($client); self::sendSystemReplyByType($client, $accountId, HelpDeskSetting::REPLY_WAITTING); return ['status' => 'failed', 'channel' => ChatConversation::CHANNEL_GLOBAL . $accountId, 'client' => $client]; } } else { //push the client into pending clients's queue PendingClient::enQueue($client); self::sendSystemReplyByType($client, $accountId, HelpDeskSetting::REPLY_WAITTING); } } } }