private function doHandle(CommandMessageInterface $command)
 {
     $violations = $this->validator->validate($command->getPayload());
     if (0 !== $violations->count()) {
         throw new ValidatorException("One or more constraints were violated.", $violations);
     }
     return $command;
 }
예제 #2
0
 public function dispatch(CommandMessageInterface $command, CommandCallbackInterface $callback = null)
 {
     $this->dispatchedCommands[] = $command;
     try {
         if (null !== $callback) {
             $callback->onSuccess($this->callbackBehavior->handle($command->getPayload(), $command->getMetaData()));
         }
     } catch (\Exception $ex) {
         if (null !== $callback) {
             $callback->onFailure($ex);
         }
     }
 }
예제 #3
0
 /**
  * @param CommandMessageInterface $commandMessage
  * @param SerializerInterface $serializer
  * @param bool $expectReply
  */
 public function __construct(CommandMessageInterface $commandMessage, SerializerInterface $serializer, $expectReply)
 {
     $this->commandMessage = $commandMessage;
     $this->commandIdentifier = $commandMessage->getIdentifier();
     $this->expectReply = $expectReply;
     $messageSerializer = new MessageSerializer($serializer);
     $payload = $messageSerializer->serializePayload($commandMessage);
     $metaData = $messageSerializer->serializeMetaData($commandMessage);
     $this->payloadType = $payload->getType()->getName();
     $this->payloadRevision = $payload->getType()->getRevision();
     $this->serializedPayload = $payload->getData();
     $this->serializedMetaData = $metaData->getData();
     $this->commandName = $commandMessage->getCommandName();
 }
 private function getAnnotatedTargetValue($annotationName, CommandMessageInterface $command, AnnotationReader $reader, \ReflectionClass $reflClass)
 {
     foreach (ReflectionUtils::getProperties($reflClass) as $property) {
         if (null !== ($annot = $reader->getPropertyAnnotation($property, $annotationName))) {
             $property->setAccessible(true);
             return $property->getValue($command->getPayload());
         }
     }
     foreach (ReflectionUtils::getMethods($reflClass) as $method) {
         if (null !== ($annot = $reader->getMethodAnnotation($method, $annotationName))) {
             $method->setAccessible(true);
             return $method->invoke($command->getPayload());
         }
     }
     return null;
 }
 public function provideAuditDataFor(CommandMessageInterface $command)
 {
     return $command->getMetaData()->all();
 }
 /**
  * @param CommandMessageInterface $command
  * @param CommandHandlerInterface $handler
  * @return mixed
  * @throws \Exception
  */
 protected function doDispatch(CommandMessageInterface $command, CommandHandlerInterface $handler)
 {
     $this->logger->debug("Dispatching command [{name}]", array('name' => $command->getCommandName()));
     $unitOfWork = $this->unitOfWorkFactory->createUnitOfWork();
     $unitOfWork->setLogger($this->logger);
     $chain = new DefaultInterceptorChain($command, $unitOfWork, $handler, $this->handlerInterceptors);
     try {
         $return = $chain->proceed();
     } catch (\Exception $ex) {
         $unitOfWork->rollback($ex);
         throw $ex;
     }
     $unitOfWork->commit();
     return $return;
 }
 private function findSuitableNode(CommandMessageInterface $command)
 {
     $nodes = $this->template->getSubscriptions($command->getCommandName());
     if (empty($nodes)) {
         throw new NoHandlerForCommandException(sprintf("No handler in cluster was subscribed for command [%s]", $command->getCommandName()));
     }
     return $nodes[0];
     // TODO temporary something more elaborate :)
 }
 /**
  * @param CommandMessageInterface $command
  * @return null|string
  */
 protected function doResolveRoutingKey(CommandMessageInterface $command)
 {
     $value = $command->getMetaData()->get($this->metaDataKey);
     return isset($value) ? (string) $value : null;
 }
 /**
  * {@inheritdoc}
  */
 protected function doResolveRoutingKey(CommandMessageInterface $command)
 {
     $reflectionClass = new \ReflectionClass($command->getPayload());
     return $this->findIdentifier($command, $reflectionClass);
 }
 public function handle(CommandMessageInterface $commandMessage, UnitOfWorkInterface $unitOfWork)
 {
     $command = $commandMessage->getPayload();
     switch ($commandMessage->getPayloadType()) {
         case 'CommandHandlerExample\\CreateUserCommand':
             $aggregate = new User($command->getIdentifier(), $command->getEmail());
             $this->repository->add($aggregate);
             break;
         case 'CommandHandlerExample\\ChangeUserEmailCommand':
             $aggregate = $this->repository->load($command->getIdentifier());
             $aggregate->changeEmail($command->getEmail());
             break;
     }
 }
 public function provideAuditDataFor(CommandMessageInterface $command)
 {
     return array($this->correlationIdKey => $command->getIdentifier());
 }
예제 #12
0
 /**
  * Finds and returns the suitable CommandHandlerInterface for the command message
  * or throws a NoHandlerForCommandException if none exist.
  *
  * @param CommandMessageInterface $message
  * @return CommandHandlerInterface
  * @throws NoHandlerForCommandException
  */
 public function findCommandHandlerFor(CommandMessageInterface $message)
 {
     if (!isset($this->subscriptions[$message->getCommandName()])) {
         throw new NoHandlerForCommandException(sprintf("No handler was subscribed for command [%s]", $message->getCommandName()));
     }
     return $this->subscriptions[$message->getCommandName()];
 }