From f0dbcfd7c2f55caf50698bd33e42d388fde1833c Mon Sep 17 00:00:00 2001 From: Felix Date: Wed, 2 Nov 2022 14:37:42 +0100 Subject: [PATCH] Update Database & APIView - Add @throws to doc comments - Database: Make execute throw `PDOException` - APIView: Add cache max age --- Backend/Core/Data/Database.php | 11 ++++------- Backend/Core/Functions/Utils.php | 2 +- Backend/Core/System/Controller.php | 4 ++++ Backend/Views/Base/APIView.php | 8 +++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Backend/Core/Data/Database.php b/Backend/Core/Data/Database.php index d7768a5..4d36131 100644 --- a/Backend/Core/Data/Database.php +++ b/Backend/Core/Data/Database.php @@ -27,16 +27,13 @@ public function __construct() * * @param string $queryStr SQL query as string * @param array $values Escaped values - * @return bool Whether the query was executed successfully + * @throws PDOException When anything goes wrong + * @return bool Whether the query was executed successfully */ public function execute(string $queryStr, array $values): bool { - try { - $this->query = $this->prepare($queryStr); - return $this->query->execute($values); - } catch (PDOException) { - return false; - } + $this->query = $this->prepare($queryStr); + return $this->query->execute($values); } /** diff --git a/Backend/Core/Functions/Utils.php b/Backend/Core/Functions/Utils.php index 7cd7b2d..05dec73 100644 --- a/Backend/Core/Functions/Utils.php +++ b/Backend/Core/Functions/Utils.php @@ -12,7 +12,7 @@ class Utils * @param string $algo Hashing algorithmn - Standard: SHA256 * @return string Hashed data */ - public static function encrypt(string $data, string $algo = "sha256"): string { + public static function encrypt(string $data, string $algo = 'sha256'): string { return hash($algo, $data); } diff --git a/Backend/Core/System/Controller.php b/Backend/Core/System/Controller.php index 51e2b48..e7ca281 100644 --- a/Backend/Core/System/Controller.php +++ b/Backend/Core/System/Controller.php @@ -33,6 +33,8 @@ abstract class Controller /** * Adds the paths to the router + * + * @throws Exception When a path is already taken */ public function initRoutes(): void { foreach ($this->paths as $path) @@ -43,6 +45,7 @@ public function initRoutes(): void { * Executes the controller with given params * * @param array $params Parameters + * @throws Exception If there's no request URI set */ public function runExecute(array $params): void { if (($code = $this->checkAccess()) === 200) { @@ -84,6 +87,7 @@ protected function redirect(string $url): never { /** * Checks if the access to this controller is allowed * + * @throws Exception If there's no request method set * @return int HTTP status code (200 === OK!) */ private function checkAccess(): int { diff --git a/Backend/Views/Base/APIView.php b/Backend/Views/Base/APIView.php index 1a1ecaa..856b41e 100644 --- a/Backend/Views/Base/APIView.php +++ b/Backend/Views/Base/APIView.php @@ -4,16 +4,18 @@ class APIView extends View { /** * @param array|null $data JSON data to be displayed + * @param int $maxAge Cache max age (default: 5 minutes) */ public function __construct( - public ?array $data = null + public ?array $data = null, + private readonly int $maxAge = 300 ){} public function render(): void { header('Content-Type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: ' . DOMAIN); - // header('Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept'); - // header('Access-Control-Max-Age: 86400'); + header('Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept'); + header('Access-Control-Max-Age: ' . $this->maxAge); echo json_encode($this->data); }