diff --git a/src/controller/AuthorController.php b/src/controller/AuthorController.php index 20b848d..194bcb4 100644 --- a/src/controller/AuthorController.php +++ b/src/controller/AuthorController.php @@ -15,11 +15,11 @@ public function __construct($database) public function getAuthor() { - if (Req::checkIdParam()) { - $author = $this->database->getUser($_GET['id']); - if (!is_null($author) && $author !== false) { - return new Author($author); - } + Req::checkIdParam(); + + $author = $this->database->getUser(Req::id()); + if (!is_null($author) && $author !== false) { + return new Author($author); } return NULL; @@ -27,11 +27,11 @@ public function getAuthor() public function findAuthor() { - if (Req::checkNameParam()) { - $author = $this->database->findUser($_GET['name']); - if (!is_null($author) && $author !== false) { - return new Author($author); - } + Req::checkNameParam(); + + $author = $this->database->findUser(Req::name()); + if (!is_null($author) && $author !== false) { + return new Author($author); } return NULL; diff --git a/src/controller/ResourceController.php b/src/controller/ResourceController.php index 5a30d2e..100e5c8 100644 --- a/src/controller/ResourceController.php +++ b/src/controller/ResourceController.php @@ -32,12 +32,11 @@ public function listResources() public function getResource() { - if (Req::checkIdParam()) { - $resource = $this->database->getResource(Req::id()); + Req::checkIdParam(); - if (!is_null($resource) && $resource !== false) { - return new Resource($resource); - } + $resource = $this->database->getResource(Req::id()); + if (!is_null($resource) && $resource !== false) { + return new Resource($resource); } return NULL; @@ -45,18 +44,16 @@ public function getResource() public function getResourcesByAuthor() { - $out = []; - - if (Req::checkIdParam()) { - $resources = $this->database->getResourcesByUser(Req::id(), Req::page()); + Req::checkIdParam(); - if (is_null($resources)) { - return NULL; - } + $resources = $this->database->getResourcesByUser(Req::id(), Req::page()); + if (is_null($resources)) { + return NULL; + } - foreach ($resources as $resource) { - $out[] = new Resource($resource); - } + $out = []; + foreach ($resources as $resource) { + $out[] = new Resource($resource); } return $out; diff --git a/src/controller/ResourceUpdateController.php b/src/controller/ResourceUpdateController.php index 5286623..ac9ddfe 100644 --- a/src/controller/ResourceUpdateController.php +++ b/src/controller/ResourceUpdateController.php @@ -15,11 +15,11 @@ public function __construct($database) public function getResourceUpdate() { - if (Req::checkIdParam()) { - $update = $this->database->getResourceUpdate($_GET['id']); - if (!is_null($update) && $update !== false) { - return new ResourceUpdate($update); - } + Req::checkIdParam(); + + $update = $this->database->getResourceUpdate(Req::id()); + if (!is_null($update) && $update !== false) { + return new ResourceUpdate($update); } return NULL; @@ -27,15 +27,16 @@ public function getResourceUpdate() public function getResourceUpdates() { - $out = []; + Req::checkIdParam(); - if (Req::checkIdParam()) { - $updates = $this->database->getResourceUpdates($_GET['id'], Req::page()); - if (is_null($updates)) return NULL; + $updates = $this->database->getResourceUpdates(Req::id(), Req::page()); + if (is_null($updates)) { + return NULL; + } - foreach ($updates as $update) { - $out[] = new ResourceUpdate($update); - } + $out = []; + foreach ($updates as $update) { + $out[] = new ResourceUpdate($update); } return $out; diff --git a/src/util/RequestUtil.php b/src/util/RequestUtil.php index 735133e..f750dcd 100644 --- a/src/util/RequestUtil.php +++ b/src/util/RequestUtil.php @@ -29,6 +29,12 @@ public static function id() return $_GET['id'] ?? null; } + /** + * Checks if the 'id' parameter is present and valid. + * Exits with an error message if not. + * + * @return void + */ public static function checkIdParam() { $id = self::id(); @@ -42,8 +48,6 @@ public static function checkIdParam() echo new Error(400, "Invalid ID. ID must be numeric."); exit(); } - - return true; } public static function name() @@ -51,6 +55,12 @@ public static function name() return $_GET['name'] ?? null; } + /** + * Checks if the 'name' parameter is present and valid. + * Exits with an error message if not. + * + * @return void + */ public static function checkNameParam() { $name = self::name(); @@ -66,8 +76,6 @@ public static function checkNameParam() echo new Error(400, "Invalid name. Name must be at 3-24 characters in length and consist of letters, numbers, and/or a limited set of special characters (_, -, ., and/or one or more spaces)."); exit(); } - - return true; } public static function page()