Skip to content

Commit

Permalink
Update Database & APIView
Browse files Browse the repository at this point in the history
- Add @throws to doc comments
- Database: Make execute throw `PDOException`
- APIView: Add cache max age
  • Loading branch information
felix-schindler committed Nov 2, 2022
1 parent af36210 commit f0dbcfd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
11 changes: 4 additions & 7 deletions Backend/Core/Data/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,13 @@ public function __construct()
*
* @param string $queryStr SQL query as string
* @param array<string,string> $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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Backend/Core/Functions/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions Backend/Core/System/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -43,6 +45,7 @@ public function initRoutes(): void {
* Executes the controller with given params
*
* @param array<string,string> $params Parameters
* @throws Exception If there's no request URI set
*/
public function runExecute(array $params): void {
if (($code = $this->checkAccess()) === 200) {
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions Backend/Views/Base/APIView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ class APIView extends View
{
/**
* @param array<mixed>|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);
}
Expand Down

0 comments on commit f0dbcfd

Please sign in to comment.