Example #1
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());
 }
Example #2
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;
 }