private function getMailBoxMessageProvider()
 {
     if (is_null($this->mailboxMessageProvider)) {
         $this->mailboxMessageProvider = $this->mailboxMessageProviderFactory->create(array('host' => $this->settings->getServerAddress(), 'user' => $this->settings->getUsername(), 'password' => $this->settings->getPassword(), 'ssl' => $this->settings->getSslEnabled()));
     }
     return $this->mailboxMessageProvider;
 }
 /**
  * Handle mail process
  * @param MessageProvider $provider
  * @return void
  */
 public function handle(MessageProvider $provider)
 {
     $processedMessages = [];
     $strategies = $this->strategyHolder->getStrategies();
     foreach ($provider->fetchMessagesToProcess() as $message) {
         foreach ($strategies as $strategy) {
             $this->processingContext->setStrategy($strategy);
             try {
                 if (!$message->isFailed() && !$message->isSystem()) {
                     $this->processingContext->execute($message);
                 }
                 if (false === isset($processedMessages[$message->getUniqueId()])) {
                     $processedMessages[$message->getUniqueId()] = $message;
                 }
             } catch (\Exception $e) {
                 // TODO
                 $this->logger->error(sprintf('Error processing message: %s', $e->getMessage()));
             }
         }
     }
     if ($this->settings->getDeleteProcessedMessages()) {
         $provider->deleteProcessedMessages($processedMessages);
     } else {
         $provider->markMessagesAsProcessed($processedMessages);
     }
 }
 /**
  * @test
  */
 public function testIsDefault()
 {
     $branches = array(new Branch(8, 'DUMMY_NAME', "DUMMY_DESC"), new Branch(9, 'DUMMY_NAME', "DUMMY_DESC"));
     $this->systemSettings->expects($this->any())->method('getDefaultBranchId')->will($this->returnValue(self::DEFAULT_BRANCH_ID));
     $result = $this->placeholder->isDefault($branches[0]);
     $this->assertTrue($result);
     $result = $this->placeholder->isDefault($branches[1]);
     $this->assertFalse($result);
 }
 /**
  * @param Branch $branch
  * @return bool
  */
 public function isDefault($branch)
 {
     if (!$branch instanceof Branch) {
         return false;
     }
     $defaultBranchId = $this->systemSettings->getDefaultBranchId();
     if (!$defaultBranchId || is_null($branch->getId())) {
         return false;
     }
     return $defaultBranchId == $branch->getId();
 }
 /**
  * @test
  */
 public function thatHandlesAndDeletesMessages()
 {
     $messages = $this->getMessages();
     $strategies = array($this->strategy);
     $this->settings->expects($this->any())->method('getDeleteProcessedMessages')->will($this->returnValue(true));
     $this->provider->expects($this->once())->method('fetchMessagesToProcess')->will($this->returnValue($messages));
     $this->strategyHolder->expects($this->once())->method('getStrategies')->will($this->returnValue($strategies));
     $this->context->expects($this->exactly(count($strategies)))->method('setStrategy')->with($this->isInstanceOf('\\Diamante\\EmailProcessingBundle\\Model\\Processing\\Strategy'));
     $this->context->expects($this->exactly(count($messages) * count($strategies)))->method('execute')->with($this->isInstanceOf('Diamante\\EmailProcessingBundle\\Model\\Message'));
     $this->provider->expects($this->once())->method('deleteProcessedMessages');
     $this->manager->handle($this->provider);
 }
 /**
  * @param $from
  * @param $to
  * @return int
  */
 private function getAppropriateBranch($from, $to)
 {
     $branchId = null;
     preg_match('/@(.*)/', $from, $output);
     if (isset($output[1])) {
         $customerDomain = $output[1];
         $branchId = $this->branchEmailConfigurationService->getConfigurationBySupportAddressAndCustomerDomain($to, $customerDomain);
     }
     if (!$branchId) {
         $branchId = $this->emailProcessingSettings->getDefaultBranchId();
     }
     return $branchId;
 }
 public function testProcessWhenMessageWithoutReferenceWithDefaultBranch()
 {
     $message = new Message(self::DUMMY_UNIQUE_ID, self::DUMMY_MESSAGE_ID, self::DUMMY_SUBJECT, self::DUMMY_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO);
     $assigneeId = 1;
     $diamanteUser = $this->getReporter(1);
     $this->userService->expects($this->once())->method('getUserByEmail')->with($this->equalTo(self::DUMMY_MESSAGE_FROM))->will($this->returnValue($diamanteUser));
     $reporter = $this->getReporter($diamanteUser->getId());
     preg_match('/@(.*)/', self::DUMMY_MESSAGE_FROM, $output);
     $customerDomain = $output[1];
     $this->branchEmailConfigurationService->expects($this->once())->method('getConfigurationBySupportAddressAndCustomerDomain')->with($this->equalTo(self::DUMMY_MESSAGE_TO), $this->equalTo($customerDomain))->will($this->returnValue(null));
     $this->emailProcessingSettings->expects($this->once())->method('getDefaultBranchId')->will($this->returnValue(self::DEFAULT_BRANCH_ID));
     $this->messageReferenceService->expects($this->once())->method('createTicket')->with($this->equalTo($message->getMessageId()), self::DEFAULT_BRANCH_ID, $message->getSubject(), $message->getContent(), $reporter, $assigneeId);
     $this->branchEmailConfigurationService->expects($this->once())->method('getBranchDefaultAssignee')->with($this->equalTo($assigneeId))->will($this->returnValue(1));
     $this->ticketStrategy->process($message);
 }