/**
  * 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 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);
 }
 /**
  * Handle mail process
  * @param MessageProvider $provider
  * @return void
  */
 public function handle(MessageProvider $provider)
 {
     $messagesToMove = array();
     $strategies = $this->strategyHolder->getStrategies();
     foreach ($provider->fetchMessagesToProcess() as $message) {
         foreach ($strategies as $strategy) {
             $this->processingContext->setStrategy($strategy);
             try {
                 if (!$message->isFailed()) {
                     $this->processingContext->execute($message);
                 }
                 if (false === isset($messagesToMove[$message->getUniqueId()])) {
                     $messagesToMove[$message->getUniqueId()] = $message;
                 }
             } catch (\Exception $e) {
                 $this->logger->error(sprintf('Error processing message: %s', $e->getMessage()));
             }
         }
     }
     $provider->markMessagesAsProcessed($messagesToMove);
 }
 /**
  * @test
  */
 public function thatHandles()
 {
     $messages = array(new Message(self::DUMMY_MESSAGE_UNIQUE_ID, self::DUMMY_MESSAGE_ID, self::DUMMY_MESSAGE_SUBJECT, self::DUMMY_MESSAGE_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO, self::DUMMY_MESSAGE_REFERENCE));
     $strategies = array($this->strategy);
     $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('markMessagesAsProcessed')->with($this->logicalAnd($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_ARRAY), $this->countOf(count($messages)), $this->callback(function ($other) {
         $result = true;
         foreach ($other as $message) {
             $constraint = \PHPUnit_Framework_Assert::isInstanceOf('Diamante\\EmailProcessingBundle\\Model\\Message');
             try {
                 \PHPUnit_Framework_Assert::assertThat($message, $constraint);
             } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
                 $result = false;
             }
         }
         return $result;
     })));
     $this->manager->handle($this->provider);
 }