/**
  * @param Message $message
  * @return array
  */
 private function arrayFromMessage(Message $message)
 {
     $messageData = $this->messageConverter->convertToArray($message);
     MessageDataAssertion::assert($messageData);
     $messageData['created_at'] = $message->createdAt()->format('Y-m-d\\TH:i:s.u');
     return $messageData;
 }
 /**
  * @interitdoc
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     $payload = null;
     $messageName = 'UNKNOWN';
     try {
         $payload = $request->getParsedBody();
         if (is_array($payload) && isset($payload['message_name'])) {
             $messageName = $payload['message_name'];
         }
         MessageDataAssertion::assert($payload);
         $message = $this->messageFactory->createMessageFromArray($payload['message_name'], $payload);
         switch ($message->messageType()) {
             case Message::TYPE_COMMAND:
                 $this->commandBus->dispatch($message);
                 return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
             case Message::TYPE_EVENT:
                 $this->eventBus->dispatch($message);
                 return $response->withStatus(Middleware::STATUS_CODE_ACCEPTED);
             case Message::TYPE_QUERY:
                 return $this->responseStrategy->fromPromise($this->queryBus->dispatch($message));
             default:
                 return $next($request, $response, new RuntimeException(sprintf('Invalid message type "%s" for message "%s".', $message->messageType(), $messageName), Middleware::STATUS_CODE_BAD_REQUEST));
         }
     } catch (\Assert\InvalidArgumentException $e) {
         return $next($request, $response, new RuntimeException($e->getMessage(), Middleware::STATUS_CODE_BAD_REQUEST, $e));
     } catch (\Exception $e) {
         return $next($request, $response, new RuntimeException(sprintf('An error occurred during dispatching of message "%s"', $messageName), Middleware::STATUS_CODE_INTERNAL_SERVER_ERROR, $e));
     }
 }
 /**
  * @param  Envelope $envelope
  * @throws InvalidArgumentException
  * @return string
  */
 public function serialize(Envelope $envelope)
 {
     $message = $envelope->getMessage();
     if (!$message instanceof BernardMessage) {
         throw new InvalidArgumentException(sprintf("Serialize message %s failed due to wrong message type", $message->getName()));
     }
     $messageData = $this->messageConverter->convertToArray($message->getProophMessage());
     MessageDataAssertion::assert($messageData);
     $messageData['created_at'] = $message->getProophMessage()->createdAt()->format('Y-m-d\\TH:i:s.u');
     return json_encode(['message' => $messageData, 'timestamp' => $envelope->getTimestamp()]);
 }
Beispiel #4
0
 /**
  * Creates a new domain message from given array
  *
  * @param array $messageData
  * @return static
  */
 public static function fromArray(array $messageData)
 {
     MessageDataAssertion::assert($messageData);
     $messageRef = new \ReflectionClass(get_called_class());
     /** @var $message DomainMessage */
     $message = $messageRef->newInstanceWithoutConstructor();
     $message->uuid = Uuid::fromString($messageData['uuid']);
     $message->messageName = $messageData['message_name'];
     $message->version = $messageData['version'];
     $message->setPayload($messageData['payload']);
     $message->metadata = $messageData['metadata'];
     $message->createdAt = $messageData['created_at'];
     return $message;
 }
 /**
  * @inheritdoc
  */
 public function __invoke(Message $message, Deferred $deferred = null)
 {
     $messageData = $this->messageConverter->convertToArray($message);
     MessageDataAssertion::assert($messageData);
     $messageData['created_at'] = $message->createdAt()->format('Y-m-d\\TH:i:s.u');
     $promise = $this->guzzleClient->postAsync($this->uri, ['json' => $messageData]);
     if ($deferred) {
         if ($this->async) {
             $this->resolveOrRejectDeferredAsync($deferred, $promise);
         } else {
             $this->resolveOrRejectDeferredSync($deferred, $promise);
         }
     }
 }
 /**
  * @param Message $event
  *
  * @return Document
  */
 private function convertEventToDocument(Message $event)
 {
     $eventArr = $this->messageConverter->convertToArray($event);
     MessageDataAssertion::assert($eventArr);
     $data = ['event_id' => $eventArr['uuid'], 'version' => $eventArr['version'], 'event_name' => $eventArr['message_name'], 'payload' => $eventArr['payload'], 'metadata' => $eventArr['metadata'], 'created_at' => $eventArr['created_at']->format('Y-m-d\\TH:i:s.u')];
     $document = new Document($data);
     $document->setId($data['event_id']);
     return $document;
 }
 /**
  * @test
  * @dataProvider
  * @dataProvider provideMessageDataWithMissingKey
  */
 public function it_throws_exception_if_message_data_is_invalid($messageData, $errorMessage)
 {
     $this->setExpectedException('\\InvalidArgumentException', $errorMessage);
     MessageDataAssertion::assert($messageData);
 }
 /**
  * Insert an event
  *
  * @param StreamName $streamName
  * @param Message $e
  * @return void
  */
 private function insertEvent(StreamName $streamName, Message $e)
 {
     $eventArr = $this->messageConverter->convertToArray($e);
     MessageDataAssertion::assert($eventArr);
     $eventData = ['event_id' => $eventArr['uuid'], 'version' => $eventArr['version'], 'event_name' => $eventArr['message_name'], 'payload' => $this->payloadSerializer->serializePayload($eventArr['payload']), 'created_at' => $eventArr['created_at']->format('Y-m-d\\TH:i:s.u')];
     foreach ($eventArr['metadata'] as $key => $value) {
         $eventData[$key] = (string) $value;
     }
     $this->connection->insert($this->getTable($streamName), $eventData);
 }