microserviciile de register, login finalizate
This commit is contained in:
Executable
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace app\core;
|
||||
|
||||
use app\exceptions\InvalidTokenException;
|
||||
use app\exceptions\ServiceNotFoundException;
|
||||
use app\external_requests\CurlRequestBuilder;
|
||||
use Exception;
|
||||
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
private array $accessTokens = [];
|
||||
|
||||
private Request $request;
|
||||
private ?string $token;
|
||||
|
||||
|
||||
public function __construct(Request $request, ?string $token)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
public function set($path, $callback): void
|
||||
{
|
||||
$this->routes[$path] = $callback;
|
||||
}
|
||||
|
||||
public function getHeaders(string $path): array
|
||||
{
|
||||
$array = $_SESSION['headers_configuration'][$path];
|
||||
|
||||
if($path === '/authentication'){
|
||||
$array = $array[$this->request->getAuthRequest()];
|
||||
}
|
||||
|
||||
$array["Content-Length"] = empty($this->request->getBody()) ? 0 : strlen(json_encode($this->request->getBody()));
|
||||
|
||||
$toReturn = array();
|
||||
foreach ($array as $key => $value){
|
||||
$value = $key . ": " . $value;
|
||||
$toReturn[] = $value;
|
||||
}
|
||||
|
||||
return $toReturn;
|
||||
}
|
||||
|
||||
|
||||
public function resolve(): void
|
||||
{
|
||||
try {
|
||||
if (!$this->isTokenValid()) throw new InvalidTokenException();
|
||||
|
||||
$path = $this->request->getPath();
|
||||
$method = $this->request->method();
|
||||
$body = $this->request->getBody();
|
||||
$callback = $this->routes[$path] ?? false;
|
||||
|
||||
if (!$callback) throw new ServiceNotFoundException();
|
||||
|
||||
$headers = $this->getHeaders($path);
|
||||
|
||||
$requestBuilder = new CurlRequestBuilder();
|
||||
|
||||
$requestBuilder->setRequest($method);
|
||||
$requestBuilder->setUrl($callback);
|
||||
$requestBuilder->setHeaders($headers);
|
||||
$requestBuilder->setBody($body);
|
||||
$requestBuilder->setIsReturnable(true);
|
||||
$request = $requestBuilder->getCurlRequestObject();
|
||||
|
||||
$response = $request->makeRequest();
|
||||
$this->writeResponse(
|
||||
DVC::getHttpCodeWithCode(DVC::$successReturnCode),
|
||||
DVC::$successReturnCode, $response
|
||||
);
|
||||
} catch (Exception $e){
|
||||
$this->writeResponse(DVC::getHttpCodeWithCode($e->getCode()),
|
||||
$e->getCode(), $e->getMessage()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function isTokenValid(): bool
|
||||
{
|
||||
$validToken = hash('md5', 'api.knowyourfood');
|
||||
return $validToken == $this->token;
|
||||
}
|
||||
|
||||
private function writeResponse(int $httpCode, int $returnCode, string $returnMessage): void
|
||||
{
|
||||
header("Content-Disposition: ".DVC::$nameOfResponsePackage);
|
||||
header("Content-Type: application/json");
|
||||
http_response_code($httpCode);
|
||||
if($httpCode != 200) {
|
||||
echo json_encode([
|
||||
"code" => $returnCode,
|
||||
"message" => $returnMessage
|
||||
]);
|
||||
}else{
|
||||
echo $returnMessage;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user