private function sync() { if ($this->synchronized) { return; } /** @var ResponseInterface $clientResponse */ $clientResponse = $this->promise->wait(); if (200 !== $clientResponse->getStatusCode()) { throw new RemoteCallFailedException(); } $data = (string) $clientResponse->getBody(); // Null (empty response) is expected if only notifications were sent $rawResponses = []; if ('' !== $data) { $rawResponses = json_decode($data, false); if (json_last_error() !== JSON_ERROR_NONE) { throw ResponseParseException::notAJsonResponse(); } } if (!is_array($rawResponses) && $rawResponses instanceof \stdClass) { $rawResponses = [$rawResponses]; } $this->responses = []; foreach ($rawResponses as $rawResponse) { try { $response = new SyncResponse($rawResponse); } catch (ResponseParseException $exception) { //todo: logging??? (@scaytrase) continue; } $this->responses[$response->getId()] = $response; } $this->synchronized = true; }
/** * @param \stdClass $response * @throws ResponseParseException */ public function validate(\stdClass $response) { if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) { throw ResponseParseException::bothErrorAndResultPresent(); } if (!property_exists($response, JsonRpcResponseInterface::ERROR_FIELD) && !property_exists($response, JsonRpcResponseInterface::RESULT_FIELD)) { throw ResponseParseException::noErrorOrResultPresent(); } if (!property_exists($response, JsonRpcResponseInterface::ID_FIELD)) { throw ResponseParseException::noIdSpecified(); } if (!property_exists($response, JsonRpcResponseInterface::VERSION_FIELD)) { throw ResponseParseException::noVersionSpecified(); } if ($response->jsonrpc !== JsonRpcClient::VERSION) { throw ResponseParseException::inconsistentVersionReceived(); } if (property_exists($response, JsonRpcResponseInterface::ERROR_FIELD)) { if (!is_object($response->{JsonRpcResponseInterface::ERROR_FIELD})) { throw ResponseParseException::errorIsNotAnObject(); } if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_CODE_FIELD)) { throw ResponseParseException::noErrorCodePresent(); } if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_MESSAGE_FIELD)) { throw ResponseParseException::noErrorMessagePresent(); } } }