Skip to content

Commit

Permalink
fist step aggregator
Browse files Browse the repository at this point in the history
  • Loading branch information
secure73 committed Jul 1, 2024
1 parent 2564061 commit 3567e89
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 7 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
],
"minimum-stability": "stable",
"require-dev": {
"phpstan/phpstan": "^1.11",
"pestphp/pest": "^2.34"
},
"config": {
Expand Down
71 changes: 71 additions & 0 deletions src/core/Aggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Gemvc\Core;

use Gemvc\Http\Request;
use Gemvc\Http\JsonResponse;
use Gemvc\Http\Response;

class Aggregator
{
private Request $request;
private string $requested_service;
private string $requested_method;

public function __construct(Request $request)
{
$this->request = $request;
$this->setRequestedService();
$this->runApp();
}

private function runApp(): void
{
if (!file_exists('./app/service/'.$this->requested_service.'.php')) {
$this->showNotFound("the service path for so called $this->requested_service does not exists , check your service name if properly typed");
die;
}
$serviceInstance = false;
try {
$service = 'App\\Service\\' . $this->requested_service;
$serviceInstance = new $service($this->request);
} catch (\Throwable $e) {
$this->showNotFound($e->getMessage());
die;
}
if (!method_exists($serviceInstance, $this->requested_method)) {
$this->showNotFound("requested method $this->requested_method does not exist in service, check if you type it correctly");
die;
}
$method = $this->requested_method;
$response = $serviceInstance->$method();
if(!$response instanceof JsonResponse)
{
Response::internalError("method $method dose not provide JsonResponse as return value")->show();
die;
}
$response->show();
die;
}


private function setRequestedService(): void
{
$method = "index";

$segments = explode('/', $this->request->requestedUrl);
$service = $segments[$_ENV["SERVICE_IN_URL_SECTION"]] ? ucfirst($segments[$_ENV["SERVICE_IN_URL_SECTION"]]) : "Index";
if (isset($segments[$_ENV["METHOD_IN_URL_SECTION"]]) && $segments[$_ENV["METHOD_IN_URL_SECTION"]]) {
$method = $segments[$_ENV["METHOD_IN_URL_SECTION"]];
}
$this->requested_service = $service;
$this->requested_method = $method;
}

private function showNotFound(string $message): void
{
$jsonResponse = new JsonResponse();
$jsonResponse->notFound($message);
$jsonResponse->show();
}
}
12 changes: 6 additions & 6 deletions src/core/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ class Auth
public ?string $error;
public function __construct(Request $request)
{
$this->request = $request;
$this->isAuthenticated = false;
$this->user_id = 0;
$this->request = $request;
$this->token = null;
$this->error = null;
$this->authenticate($request);
$this->authenticate();
}

/**
Expand Down Expand Up @@ -58,12 +59,12 @@ private function checkExistedProcessedRequest(): bool
return true;
}

private function authenticate(Request $request): bool
private function authenticate(): bool
{
$jwt = new JWTToken();
if (!$this->checkExistedProcessedRequest()) {

if(!$jwt->extractToken($request))
if(!$jwt->extractToken($this->request))
{
return false;
}
Expand All @@ -72,8 +73,7 @@ private function authenticate(Request $request): bool
}
$this->token = $jwt;
$this->isAuthenticated = true;
$request->__set('token', $jwt);
$this->request = $request;
$this->request->__set('token', $jwt);
$this->user_id = $jwt->user_id;
return true;
}
Expand All @@ -84,6 +84,6 @@ private function authenticate(Request $request): bool
$this->token = $this->request->token;
$this->isAuthenticated = true;
$this->user_id = $this->request->token->user_id;
return $this->isAuthenticated;
return true;
}
}

0 comments on commit 3567e89

Please sign in to comment.