/**
  * @inheritdoc
  */
 public function setFlagsForUser(\Conjoon\Mail\Client\Message\Flag\FolderFlagCollection $folderFlagCollection, \Conjoon\User\User $user)
 {
     /**
      * @refactor
      */
     /**
      * @see Conjoon_Modules_Groupware_Email_Item_Model_Flag
      */
     require_once 'Conjoon/Modules/Groupware/Email/Item/Model/Flag.php';
     $flagModel = new \Conjoon_Modules_Groupware_Email_Item_Model_Flag();
     $userId = $user->getId();
     $flags = $folderFlagCollection->getFlagCollection()->getFlags();
     for ($i = 0, $len = count($flags); $i < $len; $i++) {
         $uId = $flags[$i]->getUId();
         $clear = $flags[$i]->isClear();
         switch (true) {
             case $flags[$i]->__toString() === '\\Seen':
                 try {
                     $isRead = !$clear;
                     $flagModel->flagItemAsRead($uId, $userId, $isRead);
                 } catch (\Exception $e) {
                     throw new MailRepositoryException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
                 }
                 break;
             case $flags[$i]->__toString() === '$Junk':
                 try {
                     $isSpam = !$clear;
                     $flagModel->flagItemAsSpam($uId, $userId, $isSpam);
                 } catch (\Exception $e) {
                     throw new MailRepositoryException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
                 }
                 break;
             case $flags[$i]->__toString() === '$NotJunk':
                 try {
                     $flagModel->flagItemAsSpam($uId, $userId, false);
                 } catch (\Exception $e) {
                     throw new MailRepositoryException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
                 }
                 break;
             default:
                 throw new MailRepositoryException("Unknown flag \"" . $flags[$i]->__toString() . "\"");
                 break;
         }
     }
 }
 /**
  * @inheritdoc
  */
 public function setFlags(\Conjoon\Mail\Client\Message\Flag\FolderFlagCollection $flagCollection, \Conjoon\User\User $user)
 {
     $folder = $flagCollection->getFolder();
     try {
         $mayWrite = $this->mayUserWriteFolder($folder, $user);
     } catch (\Conjoon\Mail\Client\Security\SecurityServiceException $e) {
         throw new \Conjoon\Mail\Server\Protocol\ProtocolException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
     }
     if (!$mayWrite) {
         throw new \Conjoon\Mail\Server\Protocol\ProtocolException("User must not access the folder \"" . $folder->getNodeId() . "\"");
     }
     try {
         $isRemoteMailbox = $this->isFolderRepresentingRemoteMailbox($folder, $user);
     } catch (\Conjoon\Mail\Client\Folder\FolderServiceException $e) {
         throw new \Conjoon\Mail\Server\Protocol\ProtocolException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
     }
     if ($isRemoteMailbox) {
         try {
             $account = $this->getAccountServiceForUser($user)->getMailAccountToAccessRemoteFolder($folder);
             if ($account) {
                 $imapMessageFlagRepository = $this->defaultClassNames['imapMessageFlagRepository'];
                 $imapRepository = new $imapMessageFlagRepository($account);
                 $imapRepository->setFlagsForUser($flagCollection, $user);
             }
         } catch (\Exception $e) {
             throw new \Conjoon\Mail\Server\Protocol\ProtocolException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
         }
         if (!$account) {
             throw new \Conjoon\Mail\Server\Protocol\ProtocolException("No mail account found for folder");
         }
     } else {
         try {
             $this->applyFlagCollectionForUser($flagCollection, $user);
         } catch (\Conjoon\Data\Repository\Mail\MailRepositoryException $e) {
             throw new \Conjoon\Mail\Server\Protocol\ProtocolException("Exception thrown by previous exception: " . $e->getMessage(), 0, $e);
         }
     }
     /**
      * @see \Conjoon\Mail\Server\Protocol\DefaultResult\SetFlagsResult
      */
     require_once 'Conjoon/Mail/Server/Protocol/DefaultResult/SetFlagsResult.php';
     return new \Conjoon\Mail\Server\Protocol\DefaultResult\SetFlagsResult();
 }