public function listen(array $commandHandlers)
 {
     $this->init();
     $callback = function ($req) use($commandHandlers) {
         $command = $this->serializer->deserialize(json_decode($req->body, true));
         foreach ($commandHandlers as $commandHandler) {
             if ($this->hasHandleMethodFor($commandHandler, $command)) {
                 try {
                     $commandHandler->handle($command);
                     $message = json_encode(['status' => 'succes']);
                 } catch (\Exception $e) {
                     $message = json_encode(['status' => 'error', 'message' => $e->getMessage()]);
                 }
                 $msg = new AMQPMessage((string) $message, array('correlation_id' => $req->get('correlation_id')));
                 $req->delivery_info['channel']->basic_publish($msg, '', $req->get('reply_to'));
             }
         }
         $req->delivery_info['channel']->basic_ack($req->delivery_info['delivery_tag']);
     };
     $this->channel->basic_consume($this->queueName, '', false, false, false, false, $callback);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
     $this->channel->close();
     $this->connection->close();
 }
 /**
  * @return ReadModelInterface[]
  */
 public function findAll()
 {
     $statement = $this->connection->prepare(sprintf('SELECT serialized FROM %s WHERE class = :class', static::TABLE));
     $statement->execute(['class' => $this->class]);
     return array_map(function ($row) {
         return $this->serializer->deserialize(json_decode($row['serialized'], true));
     }, $statement->fetchAll(\PDO::FETCH_ASSOC));
 }
 /**
  * {@inheritDoc}
  */
 public function replay($events, $queueName)
 {
     $adapter = $this->queueFactory->create($queueName);
     foreach ($events as $domainMessage) {
         $msg = json_encode($this->serializer->serialize($domainMessage));
         $adapter->publish($msg);
     }
     $adapter->publish('finished');
 }
 /**
  * @test
  */
 public function it_should_deserialize_to_a_DomainMessage()
 {
     $metadata = new Metadata();
     $payload = [];
     $time = DateTime::fromString('2015-01-01');
     $event = new DomainMessage('a', 0, $metadata, $payload, $time);
     $serializer = new ThirdPartyPayloadAndMetadataDomainMessageSerializer($this->payloadSerializer, $this->metadataSerializer);
     $this->metadataSerializer->shouldReceive('deserialize')->with(['metadata'])->andReturn($metadata);
     $this->payloadSerializer->shouldReceive('deserialize')->with(['payload'])->andReturn($payload);
     $serialized = ['id' => 'a', 'playhead' => 0, 'metadata' => ['metadata'], 'payload' => ['payload'], 'recordedOn' => (new \DateTime('2015-01-01'))->format(DateTime::FORMAT_STRING)];
     $deserialized = $serializer->deserialize($serialized);
     $this->assertEquals($event, $deserialized);
 }
 /**
  * {@inheritDoc}
  */
 public function dispatch($command)
 {
     $this->init();
     if (!is_object($command)) {
         throw new CommandNotAnObjectException();
     }
     $serializedCommand = json_encode($this->serializer->serialize($command));
     $this->response = null;
     $this->corr_id = uniqid();
     $msg = new AMQPMessage((string) $serializedCommand, array('correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue));
     $this->channel->basic_publish($msg, self::EXCHANGE_NAME, 'command');
     while (!$this->response) {
         $this->channel->wait(null, false, 10);
     }
     $this->channel->close();
     return $this->response;
 }
 /**
  * @param string $message
  *
  * @return DomainMessage
  */
 private function deserializeDomainMessage($message)
 {
     return $this->serializer->deserialize(json_decode($message, true));
 }
 /**
  * serialize some property to json
  */
 private function serialize($property)
 {
     return json_encode($this->serializer->serialize($property));
 }
 /**
  * @param $result
  *
  * @return array
  */
 private function deserialize($result)
 {
     return $this->serializer->deserialize($result);
 }
Пример #9
0
 /**
  * @param $row
  * @return DomainMessage
  */
 protected function deserializeEvent($row)
 {
     $metadata = json_decode($row['metadata'], true);
     $payload = json_decode($row['payload'], true);
     return new DomainMessage($row['uuid'], $row['playhead'], $this->metadataSerializer->deserialize($metadata, $metadata['payload']), $this->payloadSerializer->deserialize($payload, $payload['payload']), DateTime::fromString($row['recorded_on']));
 }
 /**
  * Turns a DomainMessage into array using broadway's serializers.
  *
  * @param DomainMessage $message The domain message.
  * @throws SerializationException When the serializers cannot perform.
  * @return array                  An array representation of the message.
  */
 private function convertDomainMessage(DomainMessage $message)
 {
     $metadata = $this->metadataSerializer->serialize($message->getMetadata());
     $payload = $this->payloadSerializer->serialize($message->getPayload());
     return ['uuid' => (string) $message->getId(), 'playhead' => $message->getPlayhead(), 'metadata' => json_encode($metadata), 'payload' => json_encode($payload), 'recorded_on' => $message->getRecordedOn()->toString(), 'type' => $message->getType()];
 }
 /**
  * @param array $serializedObject
  *
  * @return DomainMessage
  */
 public function deserialize(array $serializedObject)
 {
     return new DomainMessage($serializedObject['id'], $serializedObject['playhead'], $this->metadataSerializer->deserialize($serializedObject['metadata']), $this->payloadSerializer->deserialize($serializedObject['payload']), DateTime::fromString($serializedObject['recordedOn']));
 }