Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
blacknell committed Jan 9, 2019
1 parent 1e416a3 commit a83b0d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions example/MyAPI.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ protected function daylight()

}
catch (OutOfBoundsException $e) {
$this->logger->error($e->getMessage(), $this->toString());
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());

return array("result" => ['error' => $e->getMessage(), 'code' => $e->getCode()], "code" => $e->getCode());
}
catch (Exception $e) {
$this->logger->error($e->getMessage(), $this->toString());
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());

return array("result" => ['error' => $e->getMessage(), 'code' => 400], "code" => 400);
Expand Down
12 changes: 7 additions & 5 deletions src/RestAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract class RestAPI
* API constructor.
*
* @param $request
* @param Logger|null $logger created via Monolog\Monolog
* @param \Monolog\Logger|null $logger created via Monolog\Monolog
*
* @throws RuntimeException
*/
Expand Down Expand Up @@ -103,16 +103,18 @@ public function __construct($request, \Monolog\Logger $logger = null)
$this->file = file_get_contents("php://input");
break;
default:
$this->logger->error("Method Not Allowed", $this->toString());
$this->logger->error("Method Not Allowed", $this->toObject());
throw new RuntimeException("Method Not Allowed", 405);
break;
}
}

/**
* Returns the request components as an object
*
* @return array
*/
protected function toString()
protected function toObject()
{
return [
'method'=> $this->method,
Expand All @@ -129,11 +131,11 @@ public function processAPI()
{
if ((int) method_exists($this, $this->endpoint) > 0) {
$result = $this->{$this->endpoint}($this->args);
$this->logger->debug("API processed", array($this->toString(), $result));
$this->logger->debug("API processed", array($this->toObject(), $result));

return $this->response($result['result'], $result['code']);
}
$this->logger->error("No endpoint: $this->endpoint", $this->toString());
$this->logger->error("No endpoint: $this->endpoint", $this->toObject());

return $this->response(['error' => "No endpoint", 'code' => 404], 404);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/StubAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ protected function stubGet()

}
catch (OutOfBoundsException $e) {
$this->logger->error($e->getMessage(), $this->toString());
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());

return array("result" => ['error' => $e->getMessage(), 'code' => $e->getCode()], "code" => $e->getCode());
}
catch (Exception $e) {
$this->logger->error($e->getMessage(), $this->toString());
$this->logger->error($e->getMessage(), $this->toObject());
$this->logger->error($e->getTraceAsString());

return array("result" => ['error' => $e->getMessage(), 'code' => 400], "code" => 400);
Expand Down

0 comments on commit a83b0d9

Please sign in to comment.