Return types

This commit is contained in:
Kilian Hofmann 2024-07-22 02:36:28 +02:00
parent af588ab174
commit 2168e37413
12 changed files with 35 additions and 46 deletions

View File

@ -10,7 +10,7 @@ use Khofmann\Models\User\User;
class Login extends Api class Login extends Api
{ {
public function post() public function post(): void
{ {
$email = Input::post("email"); $email = Input::post("email");
if (empty($email)) throw new Exception("Missing email", 400); if (empty($email)) throw new Exception("Missing email", 400);
@ -18,7 +18,7 @@ class Login extends Api
if (empty($password)) throw new Exception("Missing password", 400); if (empty($password)) throw new Exception("Missing password", 400);
try { try {
return Response::json(User::logIn($email, $password)); Response::json(User::logIn($email, $password));
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "Failed": case "Failed":

View File

@ -9,9 +9,10 @@ use Khofmann\Response\Response;
class Logout extends Api class Logout extends Api
{ {
public function post() public function post(): void
{ {
$token = Request::header("token"); $token = Request::header("token");
return Response::json(User::getByToken($token)->logOut());
Response::json(User::getByToken($token)->logOut());
} }
} }

View File

@ -10,7 +10,7 @@ use Khofmann\Models\User\User;
class Register extends Api class Register extends Api
{ {
public function post() public function post(): void
{ {
$username = Input::post("username"); $username = Input::post("username");
if (empty($username)) throw new Exception("Missing username", 400); if (empty($username)) throw new Exception("Missing username", 400);
@ -20,7 +20,7 @@ class Register extends Api
if (empty($password)) throw new Exception("Missing password", 400); if (empty($password)) throw new Exception("Missing password", 400);
try { try {
return Response::json(User::create($username, $email, $password)); Response::json(User::create($username, $email, $password));
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "Duplicate": case "Duplicate":

View File

@ -11,10 +11,10 @@ use Khofmann\Request\Request;
class User extends Api class User extends Api
{ {
public function get($id) public function get($id): void
{ {
try { try {
return Response::json(MUser::getByID($id)); Response::json(MUser::getByID($id));
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "NotFound": case "NotFound":
@ -25,14 +25,14 @@ class User extends Api
} }
} }
public function patch($id) public function patch($id): void
{ {
$username = Input::patch("username"); $username = Input::patch("username");
$password = Input::patch("password"); $password = Input::patch("password");
$image = Input::file("image"); $image = Input::file("image");
try { try {
return Response::json(MUser::getByID($id)->update($username, $password, $image)); Response::json(MUser::getByID($id)->update($username, $password, $image));
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "NotFound": case "NotFound":
@ -49,7 +49,7 @@ class User extends Api
} }
} }
public function patchSelf() public function patchSelf(): void
{ {
$token = Request::header("token"); $token = Request::header("token");
$username = Input::patch("username"); $username = Input::patch("username");
@ -57,7 +57,7 @@ class User extends Api
$image = Input::file("image"); $image = Input::file("image");
try { try {
return Response::json(MUser::getByToken($token)->update($username, $password, $image)); Response::json(MUser::getByToken($token)->update($username, $password, $image));
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "NotFound": case "NotFound":
@ -74,10 +74,10 @@ class User extends Api
} }
} }
public function delete($id) public function delete($id): void
{ {
try { try {
return Response::json(MUser::getByID($id)->delete()); Response::json(MUser::getByID($id)->delete());
} catch (Exception $err) { } catch (Exception $err) {
switch ($err->getMessage()) { switch ($err->getMessage()) {
case "NotFound": case "NotFound":

View File

@ -8,7 +8,7 @@ class Database extends PDO
{ {
private static array $instances = []; private static array $instances = [];
protected function __construct($dsn, $username = null, $password = null, array $options = null) protected function __construct(string $dsn, string $username = null, string $password = null, array $options = null)
{ {
parent::__construct($dsn, $username, $password, $options); parent::__construct($dsn, $username, $password, $options);
} }

View File

@ -4,7 +4,7 @@ namespace Khofmann\GUID;
class GUID class GUID
{ {
public static function v4($data = null) public static function v4($data = null): string
{ {
// Generate 16 bytes (128 bits) of random data or use the data passed into the function. // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
$data = $data ?? random_bytes(16); $data = $data ?? random_bytes(16);

View File

@ -6,24 +6,22 @@ use Khofmann\Request\Request;
class Input class Input
{ {
public static function post(string $index, $defaultValue = null)
public static function post($index, $defaultValue = null)
{ {
return Request::request()->getInputHandler()->post($index, $defaultValue); return Request::request()->getInputHandler()->post($index, $defaultValue);
} }
public static function patch($index, $defaultValue = null) public static function patch(string $index, $defaultValue = null)
{ {
return Request::request()->getInputHandler()->post($index, $defaultValue); return Request::request()->getInputHandler()->post($index, $defaultValue);
} }
public static function get($index, $defaultValue = null) public static function get(string $index, $defaultValue = null)
{ {
return Request::request()->getInputHandler()->get($index, $defaultValue); return Request::request()->getInputHandler()->get($index, $defaultValue);
} }
public static function file($index, $defaultValue = null) public static function file(string $index, $defaultValue = null)
{ {
return Request::request()->getInputHandler()->file($index, $defaultValue); return Request::request()->getInputHandler()->file($index, $defaultValue);
} }

View File

@ -155,7 +155,7 @@ class User implements JsonSerializable
} }
} }
public static function create(string $username, string $email, string $password) public static function create(string $username, string $email, string $password): bool
{ {
$db = Database::getInstance(); $db = Database::getInstance();
$guid = GUID::v4(); $guid = GUID::v4();
@ -186,6 +186,10 @@ class User implements JsonSerializable
throw $err; throw $err;
} }
} }
public static function confirm(string $confirmCode): bool
{
}
/* /*
* Members * Members
*/ */
@ -198,7 +202,7 @@ class User implements JsonSerializable
return $stmt->execute(); return $stmt->execute();
} }
public function update(?string $username, ?string $password, $image = null) public function update(?string $username, ?string $password, $image = null): bool
{ {
$db = Database::getInstance(); $db = Database::getInstance();
@ -233,7 +237,7 @@ class User implements JsonSerializable
return true; return true;
} }
public function delete() public function delete(): bool
{ {
$db = Database::getInstance(); $db = Database::getInstance();
$stmt = $db->prepare("DELETE FROM egb_benutzer WHERE id = :ID"); $stmt = $db->prepare("DELETE FROM egb_benutzer WHERE id = :ID");

View File

@ -12,7 +12,7 @@ class Request
return SimpleRouter::request(); return SimpleRouter::request();
} }
public static function header($name, $defaultValue = null, $tryParse = true) public static function header(string $name, $defaultValue = null, bool $tryParse = true): ?string
{ {
return request()->getHeader($name, $defaultValue, $tryParse); return request()->getHeader($name, $defaultValue, $tryParse);
} }

View File

@ -12,13 +12,13 @@ class Response
return SimpleRouter::response(); return SimpleRouter::response();
} }
public static function json($value, int $options = 0, int $dept = 512) public static function json($value, int $options = 0, int $dept = 512): void
{ {
if (is_bool($value)) { if (is_bool($value)) {
Response::response()->header('Content-Type: application/json; charset=utf-8'); Response::response()->header('Content-Type: application/json; charset=utf-8');
echo json_encode($value, $options, $dept); echo json_encode($value, $options, $dept);
exit(0); exit(0);
} }
return SimpleRouter::response()->json($value, $options, $dept); SimpleRouter::response()->json($value, $options, $dept);
} }
} }

View File

@ -34,22 +34,22 @@ class Config
return self::$instances[$cls]; return self::$instances[$cls];
} }
public static function getBasePath() public static function getBasePath(): string
{ {
return Config::getInstance()->app["basePath"]; return Config::getInstance()->app["basePath"];
} }
public static function getBaseFSPath() public static function getBaseFSPath(): string
{ {
return Config::getInstance()->app["baseFSPath"]; return Config::getInstance()->app["baseFSPath"];
} }
public static function getStoragePath() public static function getStoragePath(): string
{ {
return Config::getInstance()->app["storagePath"]; return Config::getInstance()->app["storagePath"];
} }
public static function getDatabase() public static function getDatabase(): array
{ {
return Config::getInstance()->database; return Config::getInstance()->database;
} }

View File

@ -38,17 +38,3 @@ function redirect(string $url, ?int $code = null): void
response()->redirect($url); response()->redirect($url);
} }
/**
* Get current csrf-token
* @return string|null
*/
function csrf_token(): ?string
{
$baseVerifier = Router::router()->getCsrfVerifier();
if ($baseVerifier !== null) {
return $baseVerifier->getTokenProvider()->getToken();
}
return null;
}