/**
  * @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();
 }
Ejemplo 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()];
 }
Ejemplo 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());
 }
Ejemplo n.º 4
0
 /**
  * @param Envelope $envelope
  * @return Response
  */
 private function responseFromEnvelope(Envelope $envelope) : Response
 {
     if ($envelope->getHeader('jsonrpc') !== JsonRpcResponse::JSONRPC_VERSION || $envelope->getContentEncoding() !== 'UTF-8' || $envelope->getContentType() !== 'application/json') {
         return JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32603, 'Invalid JSON-RPC response'));
     }
     $payload = json_decode($envelope->getBody(), true);
     if (!in_array($envelope->getCorrelationId(), $this->requestIds)) {
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32603, 'Mismatched JSON-RPC IDs'));
     } elseif (isset($payload['result'])) {
         $response = JsonRpcResponse::withResult($envelope->getCorrelationId(), $payload['result']);
     } elseif (!isset($payload['error']['code']) || !isset($payload['error']['message']) || !is_int($payload['error']['code']) || !is_string($payload['error']['message'])) {
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError(JsonRpcError::ERROR_CODE_32603, 'Invalid JSON-RPC response'));
     } else {
         $response = JsonRpcResponse::withError($envelope->getCorrelationId(), new JsonRpcError($payload['error']['code'], $payload['error']['message']), $payload['data'] ?? null);
     }
     return $response;
 }