public function testEnqueueFail()
 {
     $user = $this->getMockUser(TimEnum::ANY, SexEnum::ANONYM);
     $user->expects($this->any())->method('getChannelId')->willReturn(1);
     $this->assertEmpty($this->duals->getUserPosition($user), 'getUserPosition check for anonymous failed');
     $this->assertFalse($this->duals->matchDual($user), 'matchDual must return false for anonymous');
     $this->assertEmpty($this->duals->getUserPosition($user), 'getUserPosition check for anonymous failed');
 }
 public static function notifyOnPendingDuals(User $user)
 {
     if (!empty(PendingDuals::get()->getUsersByDual($user))) {
         $response = (new MessageResponse())->setMsg(MsgToken::create('DualIsWanted', $user->getProperties()->getTim()->getShortName()))->setTime(null)->setChannelId($user->getChannelId());
         (new UserCollection())->attach($user)->setResponse($response)->notify(false);
     }
 }
 /**
  * @param User $user
  * @param UserCollection $userCollection
  */
 public function notifyChat(User $user, UserCollection $userCollection)
 {
     $channelId = $user->getChannelId();
     DI::get()->getLogger()->info("Total user count {$userCollection->getTotalCount()}", [__CLASS__]);
     if ($user->isInPrivateChat()) {
         $dualUsers = new UserCollection();
         $dualUsers->attach($user);
         $response = (new MessageResponse())->setTime(null)->setGuests($userCollection->getUsersByChatId($channelId))->setChannelId($channelId);
         if ($userCollection->getClientsCount($channelId) > 1) {
             $dualUsers = $userCollection;
             $response->setMsg(MsgToken::create('PartnerIsOnline'))->setDualChat('match');
         } elseif ($num = PendingDuals::get()->getUserPosition($user)) {
             $response->setMsg(MsgToken::create('StillInDualSearch', $num))->setDualChat('init');
         } else {
             $response->setMsg(MsgToken::create('YouAreAlone'))->setDualChat('match');
         }
         if ($user->getLastMsgId()) {
             $response->setMsg(Msg::create(null));
         }
         $dualUsers->setResponse($response)->notify(false);
     } else {
         ChannelNotifier::welcome($user, $userCollection);
         ChannelNotifier::indentifyChat($user, $userCollection, true);
     }
 }
 public static function run(User $user, UserCollection $users)
 {
     $duals = PendingDuals::get();
     if ($duals->deleteByUser($user)) {
         self::informOnPendingExit($user);
         $userList = $duals->getUsersByTim($user);
         self::sendRenewPositions($userList, $users);
     }
 }
 private static function dualGuestsList(User $user)
 {
     $dualUsers = DI::get()->getUsers()->getUsersByChatId($user->getChannelId());
     $dual = TimEnum::create(PendingDuals::get()->getDualTim($user->getProperties()->getTim()));
     foreach ($dualUsers as $n => $partner) {
         $props = $partner->getProperties();
         if ($props->getTim()->getId() != $dual->getId() && $props->getTim()->getId() != TimEnum::ANY) {
             unset($dualUsers[$n]);
         }
         if ($props->getSex()->getId() == $user->getProperties()->getSex()->getId()) {
             unset($dualUsers[$n]);
         }
     }
     if (empty($dualUsers)) {
         return;
     }
     $collection = new UserCollection();
     foreach ($dualUsers as $partner) {
         $collection->attach($partner);
     }
     $response = (new MessageResponse())->setTime(null)->setChannelId($user->getChannelId())->setMsg(MsgToken::create('DualIsWanted', $dual->getShortName()));
     $collection->setResponse($response)->notify(false);
 }
 protected function processSubmit(ChainContainer $chain)
 {
     $request = $chain->getRequest();
     $user = $chain->getFrom();
     $lang = $user->getLang();
     $onlineLimitRule = function ($val) {
         $val = (int) $val;
         return $val >= 0 && $val <= 50;
     };
     $aboutRule = function ($val) {
         $len = mb_strlen($val);
         return $len >= 0 && $len <= 1024;
     };
     try {
         $form = (new Form())->import($request)->addRule(PropertiesDAO::NAME, Rules::namePattern(), $lang->getPhrase('InvalidNameFormat'))->addRule(PropertiesDAO::ABOUT, $aboutRule, $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::TIM, Rules::timPattern(), $lang->getPhrase('InvalidTIMFormat'))->addRule(PropertiesDAO::SEX, Rules::sexPattern(), $lang->getPhrase('InvalidSexFormat'))->addRule(PropertiesDAO::CITY, Rules::cityPattern(), $lang->getPhrase('InvalidCityFormat'))->addRule(PropertiesDAO::BIRTH, Rules::birthYears(), $lang->getPhrase('InvalidYearFormat'))->addRule(PropertiesDAO::CENSOR, Rules::notNull(), $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::NOTIFY_VISUAL, Rules::notNull(), $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::NOTIFY_SOUND, Rules::notNull(), $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::LINE_BREAK_TYPE, Rules::notNull(), $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::ONLINE_NOTIFICATION, $onlineLimitRule, $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::IS_SUBSCRIBED, Rules::notNull(), $lang->getPhrase('InvalidField'))->addRule(PropertiesDAO::MESSAGE_ANIMATION_TYPE, Rules::msgAnimationType(), $lang->getPhrase('InvalidField'));
     } catch (WrongRuleNameException $e) {
         RespondError::make($user, ['property' => $lang->getPhrase('InvalidProperty') . ' ' . $e->getMessage()]);
         return;
     }
     if (!$form->validate()) {
         RespondError::make($user, $form->getErrors());
         return;
     }
     $userName = $request[PropertiesDAO::NAME] = strip_tags(trim($request[PropertiesDAO::NAME]));
     if (!$this->checkIfAlreadyRegisteredName(CharTranslator::toEnglish($userName), $user)) {
         return;
     }
     if (!$this->checkIfAlreadyRegisteredName(CharTranslator::toRussian($userName), $user)) {
         return;
     }
     if ($user->isInPrivateChat() || PendingDuals::get()->getUserPosition($user)) {
         $this->forbiddenChangeInDualization($user);
         $this->propertiesResponse($user);
         return;
     }
     $oldName = $user->getProperties()->getName();
     $this->importProperties($user, $request);
     $this->guestsUpdateResponse($user, $oldName);
     $this->propertiesResponse($user);
     ChannelNotifier::notifyOnPendingDuals($user);
 }
 private function cleanPendingQueue(User $user)
 {
     $duals = PendingDuals::get();
     $duals->deleteByUser($user);
 }