Skip to content

Commit

Permalink
Refactor all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
adammbalogh committed Jan 5, 2017
1 parent 645e2d0 commit f9b1bda
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 192 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -16,7 +16,6 @@
],
"require": {
"php": ">=5.4",
"league/booboo": "^1.0.0",
"justinrainbow/json-schema": "^4.0.0"
},
"require-dev": {
Expand Down
15 changes: 15 additions & 0 deletions src/Response/Contract/ErrorFormatter.php
@@ -0,0 +1,15 @@
<?php

namespace PhpJsonRpc\Server\Response\Contract;

use PhpJsonRpc\Server\Response\Error;

interface ErrorFormatter
{
/**
* @param Error $error
*
* @return \stdClass
*/
public function formatError(Error $error);
}
69 changes: 28 additions & 41 deletions src/Response/Error.php
Expand Up @@ -2,91 +2,78 @@

namespace PhpJsonRpc\Server\Response;

use PhpJsonRpc\Server\Response\Contract\ErrorFormatter;
use PhpJsonRpc\Server\Error\Exception\ExceptionWithData;

class Error implements \JsonSerializable
{
/**
* @var int
* @var \Exception
*/
protected $code;
protected $exception;

/**
* @var string
* @var ErrorFormatter
*/
protected $message;
protected $errorFormatter;

/**
* @var mixed|null
*/
protected $data;

/**
* @param int $code
* @param string $message
* @param mixed|null $data
* @param \Exception $exception
* @param ErrorFormatter $errorFormatter
*/
public function __construct($code, $message, $data = null)
public function __construct(\Exception $exception, ErrorFormatter $errorFormatter)
{
$this->code = $code;
$this->message = $message;
$this->data = $data;
$this->exception = $exception;
$this->errorFormatter = $errorFormatter;
}

/**
* @param \Exception $exception
*
* @return Error
* @return \Exception
*/
public static function create(\Exception $exception)
public function exception()
{
$data = null;

if ($exception instanceof ExceptionWithData) {
$data = $exception->getData();
}

return new self($exception->getCode(), $exception->getMessage(), $data);
return $this->exception;
}

/**
* @return int
* @return int|mixed
*/
public function code()
{
return $this->code;
return $this->exception()->getCode();
}

/**
* @return string
*/
public function message()
{
return $this->message;
return $this->exception()->getMessage();
}

/**
* @return bool
*/
public function hasData()
{
return ($this->exception instanceof ExceptionWithData && $this->exception->getData());
}

/**
* @return mixed|null
*/
public function data()
{
return $this->data;
if ($this->exception instanceof ExceptionWithData) {
return $this->exception->getData();
}
}

/**
* @return \stdClass
*/
public function jsonSerialize()
{
$jsonSerializable = new \stdClass();

$jsonSerializable->code = $this->code();
$jsonSerializable->message = $this->message();

if (!is_null($this->data())) {
$jsonSerializable->data = $this->data();
}

return $jsonSerializable;
return $this->errorFormatter->formatError($this);
}
}
71 changes: 71 additions & 0 deletions src/Response/ErrorFormatter/DefaultErrorFormatter.php
@@ -0,0 +1,71 @@
<?php

namespace PhpJsonRpc\Server\Response\ErrorFormatter;

use PhpJsonRpc\Server\Response\Error;
use PhpJsonRpc\Server\Response\Contract\ErrorFormatter;

class DefaultErrorFormatter implements ErrorFormatter
{
/**
* @var bool
*/
protected $isDebug;

/**
* @param bool $isDebug
*/
public function __construct($isDebug = true)
{
$this->isDebug = $isDebug;
}

/**
* @param Error $error
*
* @return \stdClass
*/
public function formatError(Error $error)
{
$jsonSerializable = new \stdClass();

$jsonSerializable->code = $error->code();
$jsonSerializable->message = $error->message();

if ($error->hasData()) {
$jsonSerializable->data = $error->data();
}

if ($this->isDebug) {
$jsonSerializable->debug = $this->buildDebugData($error->exception());
}

return $jsonSerializable;
}

/**
* @param \Exception $exception
*
* @return array
*/
protected function buildDebugData(\Exception $exception)
{
$debugData = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'type' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'trace' => $exception->getTrace()
];

if ($exception->getPrevious()) {
$debugData = [$debugData];
$newError = $this->buildDebugData($exception->getPrevious());

array_unshift($debugData, $newError);
}

return $debugData;
}
}
90 changes: 0 additions & 90 deletions src/Server/Booboo/JsonFormatter.php

This file was deleted.

24 changes: 1 addition & 23 deletions src/Server/Server.php
Expand Up @@ -2,8 +2,6 @@

namespace PhpJsonRpc\Server\Server;

use League\BooBoo\Runner as BooBooRunner;
use PhpJsonRpc\Server\Server\Booboo\JsonFormatter;
use PhpJsonRpc\Server\Service\Service as JsonRpcService;

/**
Expand All @@ -14,20 +12,7 @@ class Server
/**
* @var JsonRpcService[]
*/
private $services = [];

/**
* @var BooBooRunner
*/
private $boobooRunner;

/**
* @param BooBooRunner|null $boobooRunner
*/
public function __construct(BooBooRunner $boobooRunner = null)
{
$this->boobooRunner = $boobooRunner ? : new BooBooRunner([new JsonFormatter(true)]);
}
protected $services = [];

/**
* @param JsonRpcService $jsonRpcService
Expand All @@ -43,16 +28,9 @@ public function addService(JsonRpcService $jsonRpcService)
*/
public function handle($endpoint, $request)
{
$this->boobooRunner->treatErrorsAsExceptions(true);
$this->boobooRunner->register();

echo json_encode(
$this->findService($endpoint)->dispatch($request)
);

$this->boobooRunner->clearFormatters();
$this->boobooRunner->clearHandlers();
$this->boobooRunner->deregister();
}

/**
Expand Down

0 comments on commit f9b1bda

Please sign in to comment.