/**
  * @inheritdoc
  */
 public function getFolderEntity(Folder $folder)
 {
     $id = $folder->getNodeId() ? $folder->getNodeId() : $folder->getRootId();
     $rootId = $folder->getRootId();
     try {
         $orgEntity = $this->folderRepository->findById($id);
     } catch (InvalidArgumentException $e) {
         throw new FolderServiceException("Exception thrown by previous exception", 0, $e);
     }
     if (!$orgEntity) {
         throw new FolderDoesNotExistException("Folder {$folder} was not found");
     }
     // we might have found a folder.
     // but we have to make sure that the node id we are inspecting is actually
     // an id in the data storage, and not simply the name of a folder (e.g. "2")
     $entity = $orgEntity;
     while (true) {
         if ($entity && $entity->getParent()) {
             $entity = $entity->getParent();
         } else {
             break;
         }
     }
     if ($entity->getId() != $rootId) {
         throw new FolderDoesNotExistException("Folder {$folder} was not found");
     }
     return $orgEntity;
 }
 /**
  * Helper function for folder*Accessible checks.
  * @param \Conjoon\Mail\Client\Folder\Folder $folder
  * @param bool $considerChildFolders True to recurse into child folders
  *                                   and check whether these are accessible
  *                                   too
  *
  * @return bool true if folder(s) are accessible, otherwise false
  *
  * @throws FolderSecurityServiceException
  * @throws \Conjoon\Mail\Client\Folder\FolderDoesNotExistException
  */
 protected function isFolderAccessibleHelper(\Conjoon\Mail\Client\Folder\Folder $folder, $considerChildFolders = false)
 {
     $path = $folder->getPath();
     $nodeId = $folder->getNodeId();
     $rootId = $folder->getRootId();
     $checkNodeId = null;
     $checkForRoot = false;
     // the folder that gets transformed to its corresponding entity later on
     $transformToEntity = $folder;
     switch (true) {
         // only root id available, check only root
         case empty($path) && empty($nodeId):
             $checkNodeId = $rootId;
             $checkForRoot = true;
             break;
             // paths set, node id available.
         // paths set, node id available.
         case !empty($path) && !empty($nodeId):
             $doesMailFolderExist = $this->checkClientMailFolderExists($folder);
             // check if node id exists client side
             if ($doesMailFolderExist) {
                 // check if node is accessible
                 $checkNodeId = $nodeId;
             } else {
                 // check if root node is accessible
                 // if remote, check for rootID
                 try {
                     $repRemote = $this->folderCommons->isFolderRepresentingRemoteMailbox($folder);
                 } catch (\Conjoon\Mail\Client\Folder\FolderServiceException $e) {
                     throw new FolderSecurityServiceException("Exception trhown by previous exception: " . $e->getMessage(), 0, $e);
                 }
                 if (!$repRemote) {
                     throw new \Conjoon\Mail\Client\Folder\FolderDoesNotExistException("The folder {$folder} does not seem to exist");
                 }
                 $checkNodeId = $rootId;
                 $checkForRoot = true;
             }
             break;
         default:
             throw new FolderSecurityServiceException("Could not check whether folder \"" . $folder->__toString() . "\" is accessible ");
     }
     if ($checkForRoot) {
         // assemble new folder to check for availability of checkNodeId
         $folderCheck = new \Conjoon\Mail\Client\Folder\Folder(new \Conjoon\Mail\Client\Folder\DefaultFolderPath('["root", "' . $checkNodeId . '"]'));
         $doesMailFolderExist = $this->checkClientMailFolderExists($folderCheck);
         $transformToEntity = $folderCheck;
         if (!$doesMailFolderExist) {
             throw new \Conjoon\Mail\Client\Folder\FolderDoesNotExistException("The folder {$folder} does not seem to exist");
         }
     }
     return $this->checkFolderHierarchyAccessible($this->folderCommons->getFolderEntity($transformToEntity), $considerChildFolders === true);
 }