public function performCallback(EntityResponse $response) { if ($this->hasIdCallback($response->getId()) === false) { throw new Exception\InvalidArgument('There is no callback for id `' . $response->getId() . '`', 1); } $callable = $this->ids[$response->getId()]; call_user_func($callable, $response); }
public function testCollection() { $collection = new Response(); $entity = new EntityResponse(); $entity->setId(1); $collection->offsetSet('test', $entity); $this->assertInstanceOf('\\Jdolieslager\\JsonRpc\\Collection\\Response', $collection); $this->assertInstanceOf('Jdolieslager\\JsonRpc\\Entity\\Response', $collection->offsetGet('test')); $copy = $collection->getArrayCopy(); $this->assertInternalType('array', $copy); $this->assertArrayHasKey('test', $copy); $this->assertArrayHasKey('id', $copy['test']); $this->assertEquals(1, $copy['test']['id']); }
/** * Parse the result given by the JSON RPC Server * * @param Collection\Request $request * @param mixed $result * @param mixed $info * @return Collection\Response */ protected function parseResponse(Collection\Request $request, $result, $info) { // Rewind to collection to start from the beginning $request->rewind(); if (empty($result) && $request->isBatch() === false && $request->current()->getId() !== null) { throw new Exception\InvalidResponse('Client parse error', static::CLIENT_PARSE_ERROR); } // First go through the layer stack $result = $this->protocolLayerStack->handleResponse($result); $json = json_decode($result, true); if (is_array($json) === false) { throw new Exception\InvalidResponse('Client parse error', static::CLIENT_PARSE_ERROR); } // Check varaibles $hasIdKey = array_key_exists('id', $json); $isBatch = $request->isBatch(); // Check if we have a batch result if ($isBatch === true && $hasIdKey === true) { throw new Exception\InvalidResponse('Client expected batch result', static::CLIENT_EXPECTED_BATCH); // check if we have single result } else { if ($isBatch === false && $hasIdKey === false) { throw new Exception\InvalidResponse('Client expected single result', static::CLIENT_EXPECTED_SINGLE); } } // No batch, so we make it one if ($isBatch === false) { $json = array($json); } // Create collection response $responses = new Collection\Response(); // Loop through all the responses foreach ($json as $rawResponse) { $response = new Entity\Response(); // Fetch the items from the response array $id = $this->getArrayItem('id', $rawResponse); $jsonrpc = $this->getArrayItem('jsonrpc', $rawResponse); $result = $this->getArrayItem('result', $rawResponse, null); $error = $this->getArrayItem('error', $rawResponse, null); // Set general data $response->setId($id)->setJsonrpc($jsonrpc); // Determine which item to set if ($error === null) { $response->setResult($result); } else { // hydrate error information $errorObject = new Entity\Error(); $errorObject->setCode($this->getArrayItem('code', $error)); $errorObject->setData($this->getArrayItem('data', $error)); $errorObject->setMessage($this->getArrayItem('message', $error)); $response->setError($errorObject); } // When callback has been registered use this if ($request->hasIdCallback($response->getId())) { $request->performCallback($response); } // append the response to the collection $responses->append($response); } // return the collection return $responses; }
/** * General response creator for exceptions * * @param Entity\Request $request * @param \Exception $e * @return Entity\Response */ public function createResponseFromException(Entity\Request $request, \Exception $e) { if ($e instanceof Exception\ExceptionInterface === false) { $e = new Exception\InvalidRequest('Internal error', static::INTERNAL_ERROR, $e); } // Create required objects $response = new Entity\Response(); $error = new Entity\Error(); // Attach error the response object $response->setError($error); $response->setJsonrpc($request->getJsonrpc()); // Set the error data $error->setCode($e->getCode()); $error->setMessage($e->getMessage()); // The error code $code = $e->getCode(); // Set the request ID based on error code if ($code !== static::PARSE_ERROR && $code !== static::INVALID_REQUEST) { $response->setId($request->getId()); } $data = array(); if ($response->getId() !== null && $code !== static::METHOD_NOT_FOUND) { $methodName = strtolower($request->getMethod()); $namespace = 'global'; if (strpos($methodName, '.') !== false) { list($namespace, $methodName) = explode('.', $methodName, 2); } if ($this->handlers->offsetExists($namespace)) { $methods = $this->getHandlerReflectionMaker()->reflect($this->handlers->offsetGet($namespace)); if ($methods->offsetExists($methodName)) { $method = $methods->offsetGet($methodName); $data['parameters'] = $method->getParameters()->getArrayCopy(); } } } // In debug mode we print more data if ($this->debugMode === true) { $data['exceptions'] = array(); $data['backtrace'] = $e->getTrace(); while (null !== ($e = $e->getPrevious())) { $data['exceptions'][] = array($e->getCode() => $e->getMessage()); } } // Set all the data $error->setData($data); return $response; }
public function testPerformCallbackFailure() { $this->setExpectedException('\\Jdolieslager\\JsonRpc\\Exception\\InvalidArgument'); $collection = new Request(); $response = new Response(); $response->setId(1); $collection->performCallback($response); }