microserviciile de register, login finalizate
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
interface AuthInterface
|
||||
{
|
||||
public function executeReq(array $body): int|string;
|
||||
}
|
||||
Executable
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
namespace app\core;
|
||||
|
||||
use app\exceptions\InvalidAuthTokenException;
|
||||
use app\exceptions\InvalidBodyException;
|
||||
use app\exceptions\InvalidRequestException;
|
||||
use app\exceptions\InvalidAuthRequestException;
|
||||
|
||||
use app\models\LoginModel;
|
||||
use app\models\RegisterModel;
|
||||
use Exception;
|
||||
|
||||
class Controller
|
||||
{
|
||||
private string $method;
|
||||
private ?array $body;
|
||||
private ?string $token;
|
||||
private ?string $authRequest;
|
||||
|
||||
public function __construct(string $method, ?string $authRequest, ?array $body, ?string $token)
|
||||
{
|
||||
$this->method = $method;
|
||||
$this->body = $body;
|
||||
$this->authRequest = $authRequest;
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
if (!$this->isTokenValid()) throw new InvalidAuthTokenException();
|
||||
if (!$this->isAuthRequestValid()) throw new InvalidAuthRequestException();
|
||||
if (!$this->body) throw new InvalidBodyException();
|
||||
|
||||
$model = match ($this->method) {
|
||||
'GET' => new LoginModel(),
|
||||
'PUT' => new RegisterModel(),
|
||||
default => throw new InvalidRequestException(),
|
||||
};
|
||||
|
||||
$model->createConnection();
|
||||
$result = $model->executeReq($this->body);
|
||||
|
||||
$this->writeResponse(
|
||||
DVC::getHttpCodeWithCode(DVC::$successReturnCode),
|
||||
DVC::$successReturnCode, $result
|
||||
);
|
||||
|
||||
} catch (Exception $e){
|
||||
$this->writeResponse(DVC::getHttpCodeWithCode($e->getCode()),
|
||||
$e->getCode(), $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function isTokenValid(): bool
|
||||
{
|
||||
$validToken = hash('md5', 'service_authentication');
|
||||
return $validToken === $this->token;
|
||||
}
|
||||
|
||||
private function isAuthRequestValid(): bool
|
||||
{
|
||||
return match ($this->authRequest){
|
||||
"register", "login" => true,
|
||||
default => false
|
||||
};
|
||||
}
|
||||
|
||||
private function writeResponse(int $httpCode, int $returnCode, string|array $returnMessage): void
|
||||
{
|
||||
header("Content-Disposition: ".DVC::$nameOfResponsePackage);
|
||||
header("Content-Type: application/json");
|
||||
http_response_code($httpCode);
|
||||
echo json_encode([
|
||||
"code" => $returnCode,
|
||||
"message" => $returnMessage
|
||||
]);
|
||||
}
|
||||
}
|
||||
Executable
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
Class DVC
|
||||
{
|
||||
public static string $nameOfResponsePackage = "response_package";
|
||||
|
||||
public static string $successName = "Success";
|
||||
public static string $genericExceptionName = "GenericErrorException";
|
||||
public static string $invalidTokenName = "InvalidTokenException";
|
||||
public static string $invalidRequestName = "InvalidRequestException";
|
||||
public static string $invalidBodyName = "InvalidBodyException";
|
||||
public static string $alreadyExistsName = "AlreadyExistsException";
|
||||
public static string $userAlreadyExistsName = "userAlreadyExistsException";
|
||||
public static string $userNotExistingName = "userNotExistingException";
|
||||
public static string $fieldNotExistingName = "FieldNotExistingException";
|
||||
|
||||
public static string $returnCode = "returnCode";
|
||||
public static string $returnMessage = "returnMessage";
|
||||
|
||||
public static int $successReturnCode = 460;
|
||||
|
||||
public static function getArrayResponse(string $name): array
|
||||
{
|
||||
return match ($name){
|
||||
DVC::$successName => [
|
||||
DVC::$returnCode => 460,
|
||||
DVC::$returnMessage => "Operation done successfully"
|
||||
],
|
||||
DVC::$genericExceptionName => [
|
||||
DVC::$returnCode => 461,
|
||||
DVC::$returnMessage => "Internal Server Error"
|
||||
],
|
||||
DVC::$invalidTokenName => [
|
||||
DVC::$returnCode => 462,
|
||||
DVC::$returnMessage => "Unauthorized access"
|
||||
],
|
||||
DVC::$invalidRequestName => [
|
||||
DVC::$returnCode => 463,
|
||||
DVC::$returnMessage => "Service doesn't provide this type of request"
|
||||
],
|
||||
DVC::$invalidBodyName => [
|
||||
DVC::$returnCode => 464,
|
||||
DVC::$returnMessage => "Not valid parameters sent"
|
||||
],
|
||||
DVC::$alreadyExistsName => [
|
||||
|
||||
DVC::$returnCode => 465,
|
||||
DVC::$returnMessage => "Field already existing"
|
||||
],
|
||||
DVC::$fieldNotExistingName => [
|
||||
DVC::$returnCode => 466,
|
||||
DVC::$returnMessage => "Field not existing anymore"
|
||||
],
|
||||
DVC::$userAlreadyExistsName => [
|
||||
DVC::$returnCode => 467,
|
||||
DVC::$returnMessage => "User already exists with this email"
|
||||
],
|
||||
DVC::$userNotExistingName => [
|
||||
DVC::$returnCode => 468,
|
||||
DVC::$returnMessage => "User account doesn't exists"
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
public static function getHttpCodeWithCode(int $code): int
|
||||
{
|
||||
return match ($code){
|
||||
460 => 200,
|
||||
461 => 500,
|
||||
462 => 401,
|
||||
463 => 405,
|
||||
464 => 400,
|
||||
465, 466 => 204,
|
||||
467 => 409,
|
||||
468 => 404,
|
||||
default => 100
|
||||
};
|
||||
}
|
||||
}
|
||||
Executable
+37
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
class Request{
|
||||
public function method(): string
|
||||
{
|
||||
return $_SERVER['REQUEST_METHOD'];
|
||||
}
|
||||
|
||||
public function getAuthToken(): ?string
|
||||
{
|
||||
return array_key_exists("Authorization", apache_request_headers()) ?
|
||||
apache_request_headers()["Authorization"] : NULL;
|
||||
}
|
||||
|
||||
public function getAuthRequest(): ?string
|
||||
{
|
||||
return array_key_exists("Auth-Request", apache_request_headers()) ?
|
||||
apache_request_headers()["Auth-Request"] : NULL;
|
||||
}
|
||||
|
||||
public function getBody(): ?array
|
||||
{
|
||||
if(!array_key_exists("Content-Length", apache_request_headers())) return NULL;
|
||||
|
||||
$bytesToRead = intval(apache_request_headers()["Content-Length"]);
|
||||
if($bytesToRead == 0) return NULL;
|
||||
|
||||
$inputStream = fopen('php://input', 'r');
|
||||
$jsonData = fread($inputStream, $bytesToRead);
|
||||
|
||||
$jsonData = json_decode($jsonData, true);
|
||||
|
||||
fclose($inputStream);
|
||||
return empty($jsonData["values"]) ? [] : $jsonData["values"];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user