/**
  * @param Envelope $envelope
  * @param Queue $queue
  * @return DeliveryResult
  */
 public function __invoke(Envelope $envelope, Queue $queue) : DeliveryResult
 {
     $data = json_decode($envelope->getBody(), true);
     if (!isset($data['created_at'])) {
         return DeliveryResult::MSG_REJECT();
     }
     $data['created_at'] = DateTimeImmutable::createFromFormat('Y-m-d\\TH:i:s.u', $data['created_at'], new DateTimeZone('UTC'));
     if (false === $data['created_at']) {
         return DeliveryResult::MSG_REJECT();
     }
     try {
         $command = $this->messageFactory->createMessageFromArray($envelope->getType(), $data);
         $this->commandBus->dispatch($command);
     } catch (\Throwable $e) {
         while ($e = $e->getPrevious()) {
             if ($e instanceof ConcurrencyException) {
                 return DeliveryResult::MSG_REJECT_REQUEUE();
             }
         }
         return DeliveryResult::MSG_REJECT();
     }
     return DeliveryResult::MSG_ACK();
 }
Exemplo n.º 2
0
 /**
  * @param Envelope $envelope
  * @return array
  */
 protected function extractMessageInformation(Envelope $envelope) : array
 {
     return ['body' => $envelope->getBody(), 'routing_key' => $envelope->getRoutingKey(), 'type' => $envelope->getType()];
 }
Exemplo n.º 3
0
 /**
  * @param Envelope $envelope
  * @return Request
  * @throws Exception\InvalidJsonRpcVersion
  * @throws Exception\JsonParseError
  */
 protected function requestFromEnvelope(Envelope $envelope) : Request
 {
     if ($envelope->getHeader('jsonrpc') !== JsonRpcRequest::JSONRPC_VERSION) {
         throw new Exception\InvalidJsonRpcVersion();
     }
     if ($envelope->getContentEncoding() !== 'UTF-8' || $envelope->getContentType() !== 'application/json') {
         throw new Exception\InvalidJsonRpcRequest();
     }
     $payload = json_decode($envelope->getBody(), true);
     if (0 != json_last_error()) {
         throw new Exception\JsonParseError();
     }
     return new JsonRpcRequest($envelope->getExchangeName(), $envelope->getType(), $payload, $envelope->getCorrelationId(), $envelope->getRoutingKey(), (int) $envelope->getExpiration(), $envelope->getTimestamp());
 }