/**
  * @param IncomingLogicalMessageContext $context
  * @param callable                      $next
  */
 public function invoke($context, callable $next)
 {
     $messageTypes = $context->getMessage()->getMessageTypes();
     $handlerIdsToInvoke = $this->messageHandlerRegistry->getHandlerIdsFor($messageTypes);
     if (!$context->isMessageHandled() && !count($handlerIdsToInvoke)) {
         $messageTypes = implode(',', $messageTypes);
         throw new UnexpectedValueException("No message handlers could be found for message types '{$messageTypes}'.");
     }
     foreach ($handlerIdsToInvoke as $handlerId) {
         /** @var MessageHandlerInterface $handler */
         $handler = $context->getBuilder()->build($handlerId);
         $handlingContext = $this->incomingContextFactory->createInvokeHandlerContext($handler, $context);
         $next($handlingContext);
         if ($handlingContext->isHandlerInvocationAborted()) {
             //if the chain was aborted skip the other handlers
             break;
         }
     }
     $context->markMessageAsHandled();
 }
 /**
  * @param IncomingLogicalMessageContext $context
  * @param callable                      $next
  */
 public function invoke($context, callable $next)
 {
     $mutatorIds = $this->mutatorRegistry->getIncomingLogicalMessageMutatorIds();
     if (empty($mutatorIds)) {
         $next();
         return;
     }
     $logicalMessage = $context->getMessage();
     $messageInstance = $logicalMessage->getMessageInstance();
     $mutatorContext = new IncomingLogicalMessageMutationContext($messageInstance, $context->getHeaders());
     foreach ($mutatorIds as $mutatorId) {
         /** @var IncomingLogicalMessageMutatorInterface $mutator */
         $mutator = $context->getBuilder()->build($mutatorId);
         $mutator->mutateIncoming($mutatorContext);
     }
     if ($mutatorContext->hasMessageChanged()) {
         $logicalMessage->updateInstance($mutatorContext->getMessage(), $this->messageFactory);
     }
     $context->replaceHeaders($mutatorContext->getHeaders());
     $next();
 }