From 692a4a48d5f8f74a06d0b5890e31887a76a903f3 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Sat, 8 Jul 2017 13:58:36 -0400 Subject: Improve exception handling, introduce autoloading --- private/Controller/Controller.php | 44 +++++++++++++++++++++++++++++++++++---- private/Model/Connection.php | 3 ++- private/View/Main.php | 26 ++++++++++++++++------- private/View/Output.php | 34 ++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 private/View/Output.php (limited to 'private') diff --git a/private/Controller/Controller.php b/private/Controller/Controller.php index 188345a..586be15 100644 --- a/private/Controller/Controller.php +++ b/private/Controller/Controller.php @@ -11,6 +11,20 @@ use AugustOffensive\Model; */ class Controller { + /** + * Initiates connection with the database. + * + * While errors are ideally handled by the controller, this instantiation + * will likely throw a \PDOException which should be handled by the front- + * end due to this being a fatal error and generally unrecoverable. + * + * @return Model\Connection + */ + public static function initiateConnection (): Model\Connection + { + return new Model\Connection(); + } + /** * Creates and returns a Query object. * @@ -29,9 +43,9 @@ class Controller array $content ): Model\Query { try { - return new Model\Query ($path, $request, $content); + return new Model\Query($path, $request, $content); } catch (\Exception $err) { - return new Model\Query (array(), "", array("ERROR" => $err->getMessage())); + return new Model\Query(array(), "", array("ERROR" => $err->getMessage())); } } @@ -48,9 +62,31 @@ class Controller array $result ): Model\Result { try { - return new Model\Result ($resultType, $result); + return new Model\Result($resultType, $result); } catch (\Exception $err) { - return new Model\Result ("ERROR", array($err->getMessage())); + return new Model\Result("ERROR", array($err->getMessage())); + } + } + + /** + * Obtain the error result based on the exception that was thrown. + * + * @param \Exception $err the error that was thrown. + * + * @return Model\Result + */ + public static function errorResult (\Exception $err): Model\Result + { + $errorType = ""; + // Juggle error: objective is to sort error type + try { + throw $err; + } catch (\PDOException $e) { + $errorType = "DATABASE_ERROR"; + } catch (\Exception $e) { + $errorType = "ERROR"; } + + return new Model\Result($errorType, array("error" => $err->getMessage())); } } diff --git a/private/Model/Connection.php b/private/Model/Connection.php index 48cf4fb..4afa330 100644 --- a/private/Model/Connection.php +++ b/private/Model/Connection.php @@ -19,6 +19,7 @@ class Connection public function __construct () { // Establish connection to db + // breaks side-effect rule include 'creds.php'; try { @@ -34,7 +35,7 @@ class Connection } catch (\PDOException $err) { // we destroy $cred as quickly as possible $cred = null; - die(json_encode(array("Result-Type" => "ERROR", "Content" => array($err->getMessage())))); + throw $err; // throw for Controller to catch } return $this; } diff --git a/private/View/Main.php b/private/View/Main.php index 1dcd760..746c2bb 100644 --- a/private/View/Main.php +++ b/private/View/Main.php @@ -18,6 +18,9 @@ class Main /** @var Model\Result $result the Result object to be sent to the client. */ private $result; + /** @var string TYPE The type of output the client should receive. */ + private const TYPE = "json"; + /** * Prepares the output and environment for the front end of the service. * @@ -27,8 +30,6 @@ class Main */ public function __construct (Model\Connection $connection) { - header("Content-Type: application/json"); - $this->query = Controller\Controller::createQuery( explode('/', trim($_SERVER['PATH_INFO'] ?? '/api', '/')), $_SERVER['REQUEST_METHOD'], @@ -58,7 +59,7 @@ class Main } /** - * Communicates with the controller to generate the JSON result. + * Communicates with the controller to generate the output. * * @return string */ @@ -69,10 +70,19 @@ class Main array() ); - // generate result - return json_encode(array( - "Result-Type" => $this->result->getResultType(), - "Content" => $this->result->getResult() - )); + return self::generateOutput($this->result); + } + + /** + * Creates output of the result based on the defined constant TYPE. + * + * @param Model\Result $result The result to be sent to the client. + * + * @return string + */ + public static function generateOutput (Model\Result $result): string + { + $type = self::TYPE; + return Output::$type($result); } } diff --git a/private/View/Output.php b/private/View/Output.php new file mode 100644 index 0000000..fe42c15 --- /dev/null +++ b/private/View/Output.php @@ -0,0 +1,34 @@ + $result->getResultType(), + "Content" => $result->getResult() + )); + } +} -- cgit