/**
  * {@inheritdoc}
  */
 public function select(EmailFolder $folder, OroEntityManager $em)
 {
     /** @var ImapEmailOrigin $origin */
     $origin = $folder->getOrigin();
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()));
     return new ImapEmailFlagManager($this->connectorFactory->createImapConnector($config), $em);
 }
 /**
  * {@inheritdoc}
  */
 public function select(EmailFolder $folder, OroEntityManager $em)
 {
     /** @var UserEmailOrigin $origin */
     $origin = $folder->getOrigin();
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin));
     return new ImapEmailFlagManager($this->connectorFactory->createImapConnector($config), $em);
 }
 /**
  * {@inheritdoc}
  */
 public function loadEmailBody(EmailFolder $folder, Email $email, EntityManager $em)
 {
     /** @var UserEmailOrigin $origin */
     $origin = $folder->getOrigin();
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin));
     $manager = new ImapEmailManager($this->connectorFactory->createImapConnector($config));
     $manager->selectFolder($folder->getFullName());
     $repo = $em->getRepository('OroImapBundle:ImapEmail');
     $query = $repo->createQueryBuilder('e')->select('e.uid')->innerJoin('e.imapFolder', 'if')->where('e.email = ?1 AND if.folder = ?2')->setParameter(1, $email)->setParameter(2, $folder)->getQuery();
     $loadedEmail = $manager->findEmail($query->getSingleScalarResult());
     if (null === $loadedEmail) {
         throw new EmailBodyNotFoundException($email);
     }
     $builder = new EmailBodyBuilder();
     $builder->setEmailBody($loadedEmail->getBody()->getContent(), $loadedEmail->getBody()->getBodyIsText());
     foreach ($loadedEmail->getAttachments() as $attachment) {
         $builder->addEmailAttachment($attachment->getFileName(), $attachment->getContent(), $attachment->getContentType(), $attachment->getContentTransferEncoding(), $attachment->getContentId());
     }
     return $builder->getEmailBody();
 }
 /**
  * @param $request
  * $param sting $formParentName
  * @return FormInterface
  */
 public function getCheckGmailConnectionForm($request, $formParentName)
 {
     $form = $this->formFactory->create('oro_imap_configuration_gmail', null, ['csrf_protection' => false]);
     $form->submit($request);
     if (!$form->isValid()) {
         throw new Exception("Incorrect setting for IMAP authentication");
     }
     /** @var UserEmailOrigin $origin */
     $origin = $form->getData();
     $password = $this->mcrypt->decryptData($origin->getPassword());
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $password, $origin->getAccessToken());
     $connector = $this->imapConnectorFactory->createImapConnector($config);
     /** @var EntityManager $entityManager */
     $entityManager = $this->doctrine->getManager();
     $manager = new ImapEmailFolderManager($connector, $entityManager, $origin);
     $emailFolders = $manager->getFolders();
     $origin->setFolders($emailFolders);
     $accountTypeModel = $this->createAccountModel(AccountTypeModel::ACCOUNT_TYPE_GMAIL, $origin);
     return $this->prepareForm($formParentName, $accountTypeModel);
 }
 /**
  * {@inheritdoc}
  */
 public function loadEmailBody(Email $email, EntityManager $em)
 {
     /** @var ImapEmailOrigin $origin */
     $origin = $email->getFolder()->getOrigin();
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()));
     $manager = new ImapEmailManager($this->connectorFactory->createImapConnector($config));
     $manager->selectFolder($email->getFolder()->getFullName());
     $repo = $em->getRepository('OroImapBundle:ImapEmail');
     $query = $repo->createQueryBuilder('e')->select('e.uid')->where('e.email = ?1')->setParameter(1, $email)->getQuery();
     /** @var ImapEmail $imapEmail */
     $imapEmail = $query->getSingleResult();
     $loadedEmail = $manager->findEmail($imapEmail['uid']);
     if ($loadedEmail === null) {
         throw new \RuntimeException(sprintf('Cannot find a body for "%s" email.', $email->getSubject()));
     }
     $builder = new EmailBodyBuilder();
     $builder->setEmailBody($loadedEmail->getBody()->getContent(), $loadedEmail->getBody()->getBodyIsText());
     foreach ($loadedEmail->getAttachments() as $attachment) {
         $builder->addEmailAttachment($attachment->getFileName(), $attachment->getContent(), $attachment->getContentType(), $attachment->getContentTransferEncoding());
     }
     return $builder->getEmailBody();
 }
 /**
  * Creates a processor is used to synchronize emails
  *
  * @param ImapEmailOrigin $origin
  * @return ImapEmailSynchronizationProcessor
  */
 protected function createSynchronizationProcessor($origin)
 {
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()));
     return $this->syncProcessorFactory->create(new ImapEmailManager($this->connectorFactory->createImapConnector($config)), $this->getKnownEmailAddressChecker());
 }
 /**
  * Creates a processor is used to synchronize emails
  *
  * @param ImapEmailOrigin $origin
  * @return ImapEmailSynchronizationProcessor
  */
 protected function createSynchronizationProcessor($origin)
 {
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()));
     return new ImapEmailSynchronizationProcessor($this->log, $this->getEntityManager(), $this->emailEntityBuilder, $this->emailAddressManager, $this->knownEmailAddressChecker, new ImapEmailManager($this->connectorFactory->createImapConnector($config)));
 }
 /**
  * Creates a processor is used to synchronize emails
  *
  * @param UserEmailOrigin $origin
  * @return ImapEmailSynchronizationProcessor
  */
 protected function createSynchronizationProcessor($origin)
 {
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->encryptor->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin));
     return $this->syncProcessorFactory->create(new ImapEmailManager($this->connectorFactory->createImapConnector($config)), $this->getKnownEmailAddressChecker());
 }
 /**
  * @param ImapEmailOrigin $origin
  *
  * @return ImapEmailFolderManager
  */
 protected function createManager(ImapEmailOrigin $origin)
 {
     $config = new ImapConfig($origin->getHost(), $origin->getPort(), $origin->getSsl(), $origin->getUser(), $this->mcrypt->decryptData($origin->getPassword()));
     $connector = $this->connectorFactory->createImapConnector($config);
     return new ImapEmailFolderManager($connector, $this->doctrine->getManager(), $origin);
 }
 /**
  * @param UserEmailOrigin $origin
  *
  * @return ImapEmailFolderManager
  */
 protected function createManager(UserEmailOrigin $origin)
 {
     $config = new ImapConfig($origin->getImapHost(), $origin->getImapPort(), $origin->getImapEncryption(), $origin->getUser(), $this->mcrypt->decryptData($origin->getPassword()), $this->imapEmailGoogleOauth2Manager->getAccessTokenWithCheckingExpiration($origin));
     $connector = $this->connectorFactory->createImapConnector($config);
     return new ImapEmailFolderManager($connector, $this->doctrine->getManager(), $origin);
 }