payload = $payload; return $this; } /** * Exception classes that should not be relayed to the client * * @access public * @param mixed $exception * @return $this */ public function withLocalException($exception) { if (is_array($exception)) { $this->localExceptions = array_merge($this->localExceptions, $exception); } else { $this->localExceptions[] = $exception; } return $this; } /** * Set procedure handler * * @access public * @param ProcedureHandler $procedureHandler * @return $this */ public function withProcedureHandler(ProcedureHandler $procedureHandler) { $this->procedureHandler = $procedureHandler; return $this; } /** * Set middleware handler * * @access public * @param MiddlewareHandler $middlewareHandler * @return $this */ public function withMiddlewareHandler(MiddlewareHandler $middlewareHandler) { $this->middlewareHandler = $middlewareHandler; return $this; } /** * Parse incoming request * * @access public * @return string * @throws AccessDeniedException * @throws AuthenticationFailureException */ public function parse() { try { JsonFormatValidator::validate($this->payload); RpcFormatValidator::validate($this->payload); $this->middlewareHandler ->withProcedure($this->payload['method']) ->execute(); $result = $this->procedureHandler->executeProcedure( $this->payload['method'], empty($this->payload['params']) ? array() : $this->payload['params'] ); if (! $this->isNotification()) { return ResponseBuilder::create() ->withId($this->payload['id']) ->withResult($result) ->build(); } } catch (Exception $e) { return $this->handleExceptions($e); } return ''; } /** * Handle exceptions * * @access protected * @param Exception $e * @return string * @throws Exception */ protected function handleExceptions(Exception $e) { foreach ($this->localExceptions as $exception) { if ($e instanceof $exception) { throw $e; } } if ($e instanceof InvalidJsonRpcFormatException || ! $this->isNotification()) { return ResponseBuilder::create() ->withId(isset($this->payload['id']) ? $this->payload['id'] : null) ->withException($e) ->build(); } return ''; } /** * Return true if the message is a notification * * @access protected * @return bool */ protected function isNotification() { return is_array($this->payload) && !isset($this->payload['id']); } }