/**
  * Check the given folders and if needed correct them
  *
  * @param EmailFolder[] $folders
  * @param EmailOrigin $origin
  */
 protected function ensureFoldersInitialized(array &$folders, EmailOrigin $origin)
 {
     if (!empty($folders) && count($folders) >= 2) {
         return;
     }
     $this->log->notice('Retrieving folders from an email server ...');
     $srcFolders = $this->manager->getFolders(null, true);
     $this->log->notice(sprintf('Retrieved %d folder(s).', count($srcFolders)));
     foreach ($srcFolders as $srcFolder) {
         $type = null;
         if ($srcFolder->hasFlag(Folder::FLAG_INBOX)) {
             $type = EmailFolder::INBOX;
         } elseif ($srcFolder->hasFlag(Folder::FLAG_SENT)) {
             $type = EmailFolder::SENT;
         }
         if ($type !== null) {
             $globalName = $srcFolder->getGlobalName();
             if ($this->isFolderExist($folders, $type, $globalName)) {
                 continue;
             }
             $this->log->notice(sprintf('Persisting "%s" folder ...', $globalName));
             $folder = new EmailFolder();
             $folder->setFullName($globalName)->setName($srcFolder->getLocalName())->setType($type);
             $origin->addFolder($folder);
             $this->em->persist($origin);
             $this->em->persist($folder);
             $folders[] = $folder;
             $this->log->notice(sprintf('The "%s" folder was persisted.', $globalName));
         }
     }
     $this->em->flush();
 }
 /**
  * Gets all folders from IMAP server
  *
  * @return Folder[]
  */
 protected function getFolders()
 {
     $this->logger->notice('Retrieving folders from an email server ...');
     $srcFolders = $this->manager->getFolders(null, true);
     $folders = [];
     foreach ($srcFolders as $srcFolder) {
         if (!$srcFolder->isSelectable()) {
             continue;
         }
         if ($srcFolder->hasFlag([Folder::FLAG_DRAFTS, Folder::FLAG_SPAM, Folder::FLAG_TRASH, Folder::FLAG_ALL])) {
             continue;
         }
         $folders[] = $srcFolder;
     }
     $this->logger->notice(sprintf('Retrieved %d folder(s).', count($folders)));
     return $folders;
 }