/** * @param SearchQuery|null $query * @return ImapMessageIterator */ public function findItems($query = null) { $this->ensureConnected(); $searchString = ''; if ($query !== null) { $searchString = $query->convertToSearchString(); } if (empty($searchString)) { $result = new ImapMessageIterator($this->imap); } else { $ids = $this->imap->search(array($searchString)); $result = new ImapMessageIterator($this->imap, $ids); } return $result; }
public function testComplexQuery() { $simpleSubQuery = $this->createSearchQuery(); $simpleSubQuery->value('val1'); $complexSubQuery = $this->createSearchQuery(); $complexSubQuery->value('val2'); $complexSubQuery->orOperator(); $complexSubQuery->value('val3'); $this->query->item('subject', $simpleSubQuery); $this->query->item('subject', $complexSubQuery); $this->query->orOperator(); $this->query->openParenthesis(); $this->query->item('subject', 'product3'); $this->query->notOperator(); $this->query->item('subject', 'product4'); $this->query->closeParenthesis(); $this->assertEquals('"subject:val1 subject:(val2 OR val3) OR (subject:product3 - subject:product4)"', $this->query->convertToSearchString()); }
public function testComplexQuery() { $simpleSubQuery = $this->createSearchQuery(); $simpleSubQuery->value('val1'); $complexSubQuery = $this->createSearchQuery(); $complexSubQuery->value('val2'); $complexSubQuery->orOperator(); $complexSubQuery->value('val3'); $this->query->item('subject', $simpleSubQuery); $this->query->andOperator(); $this->query->item('subject', $complexSubQuery); $this->query->orOperator(); $this->query->openParenthesis(); $this->query->item('subject', 'product3'); $this->query->notOperator(); $this->query->item('subject', 'product4'); $this->query->closeParenthesis(); $this->assertEquals('SUBJECT val1 OR (OR SUBJECT val2 SUBJECT val3) (SUBJECT product3 NOT SUBJECT product4)', $this->query->convertToSearchString()); }
/** * Performs synchronization of emails retrieved by the given search query in the given folder * * @param ImapEmailFolder $imapFolder * @param SearchQuery $searchQuery * * @return \DateTime The max sent date */ protected function syncEmails(ImapEmailFolder $imapFolder, SearchQuery $searchQuery) { $folder = $imapFolder->getFolder(); $folderType = $folder->getType(); $lastSynchronizedAt = $folder->getSynchronizedAt(); $this->logger->notice(sprintf('Loading emails from "%s" folder ...', $folder->getFullName())); $this->logger->notice(sprintf('Query: "%s".', $searchQuery->convertToSearchString())); $count = $processed = $invalid = $totalInvalid = 0; $emails = $this->manager->getEmails($searchQuery); $emails->setBatchSize(self::READ_BATCH_SIZE); $emails->setBatchCallback(function ($batch) { $this->registerEmailsInKnownEmailAddressChecker($batch); }); $emails->setConvertErrorCallback(function (\Exception $e) use(&$invalid) { $invalid++; $this->logger->error(sprintf('Error occurred while trying to process email: %s', $e->getMessage()), ['exception' => $e]); }); $this->logger->notice(sprintf('Found %d email(s).', $emails->count())); $batch = []; /** @var Email $email */ foreach ($emails as $email) { $processed++; if ($processed % self::READ_HINT_COUNT === 0) { $this->logger->notice(sprintf('Processed %d of %d emails.%s', $processed, $emails->count(), $invalid === 0 ? '' : sprintf(' Detected %d invalid email(s).', $invalid))); $totalInvalid += $invalid; $invalid = 0; } if (!$this->isApplicableEmail($email, $folderType, (int) $this->currentUser->getId(), $this->currentOrganization)) { continue; } if ($email->getSentAt() > $lastSynchronizedAt) { $lastSynchronizedAt = $email->getSentAt(); } $count++; $batch[] = $email; if ($count === self::DB_BATCH_SIZE) { $this->saveEmails($batch, $imapFolder, $this->currentUser, $this->currentOrganization); $count = 0; $batch = []; } } if ($count > 0) { $this->saveEmails($batch, $imapFolder, $this->currentUser, $this->currentOrganization); } $totalInvalid += $invalid; if ($totalInvalid > 0) { $this->logger->warning(sprintf('Detected %d invalid email(s) in "%s" folder.', $totalInvalid, $folder->getFullName())); } return $lastSynchronizedAt; }
/** * @param SearchQuery $query * @param $expectedResult * * @dataProvider isComplexProvider */ public function testIsComplex($query, $expectedResult) { $this->assertEquals($expectedResult, $query->isComplex()); }
/** * Loads emails from an email server and save them into the database * * @param EmailFolder $folder * @param SearchQuery $searchQuery */ protected function loadEmails(EmailFolder $folder, SearchQuery $searchQuery) { $this->log->notice(sprintf('Query: "%s".', $searchQuery->convertToSearchString())); $folder->setSynchronizedAt(new \DateTime('now', new \DateTimeZone('UTC'))); $emails = $this->manager->getEmails($searchQuery); $needFolderFlush = true; $count = 0; $batch = array(); foreach ($emails as $email) { $count++; $batch[] = $email; if ($count === self::DB_BATCH_SIZE) { $this->saveEmails($batch, $folder); $needFolderFlush = false; $count = 0; $batch = array(); } } if ($count > 0) { $this->saveEmails($batch, $folder); $needFolderFlush = false; } if ($needFolderFlush) { $this->em->flush(); } }
/** * Performs synchronization of emails retrieved by the given search query in the given folder * * @param ImapEmailFolder $imapFolder * @param SearchQuery $searchQuery * * @return \DateTime The max sent date */ protected function syncEmails(ImapEmailFolder $imapFolder, SearchQuery $searchQuery) { $folder = $imapFolder->getFolder(); $folderType = $folder->getType(); $lastSynchronizedAt = $folder->getSynchronizedAt(); $this->log->notice(sprintf('Loading emails from "%s" folder ...', $folder->getFullName())); $this->log->notice(sprintf('Query: "%s".', $searchQuery->convertToSearchString())); $emails = $this->manager->getEmails($searchQuery); $emails->setBatchSize(self::READ_BATCH_SIZE); $emails->setBatchCallback(function ($batch) use($folderType) { $this->registerEmailsInKnownEmailAddressChecker($batch, $folderType); }); $this->log->notice(sprintf('Found %d email(s).', $emails->count())); $count = 0; $processed = 0; $batch = []; /** @var Email $email */ foreach ($emails as $email) { $processed++; if ($processed % self::READ_HINT_COUNT === 0) { $this->log->notice(sprintf('Processed %d of %d emails ...', $processed, $emails->count())); } if (!$this->isApplicableEmail($email, $folderType)) { continue; } if ($email->getSentAt() > $lastSynchronizedAt) { $lastSynchronizedAt = $email->getSentAt(); } $count++; $batch[] = $email; if ($count === self::DB_BATCH_SIZE) { $this->saveEmails($batch, $imapFolder); $count = 0; $batch = array(); } } if ($count > 0) { $this->saveEmails($batch, $imapFolder); } return $lastSynchronizedAt; }