fix errors, format, phpstan
continuous-integration/drone/push Build is passing Details

pull/17/head
Override-6 1 year ago
parent 35d0370564
commit 0a4ef19436
Signed by untrusted user who does not match committer: maxime.batista
GPG Key ID: 8002CC4B4DD9ECA5

@ -6,15 +6,8 @@ require "../sql/database.php";
require "utils.php";
require "../src/react-display.php";
use App\Connexion;
use App\Controller\FrontController;
session_start();
$basePath = get_public_path();
$frontController = new FrontController($basePath);
$frontController->run();

@ -6,7 +6,6 @@ use App\Connexion;
use App\Data\TacticInfo;
use App\Gateway\TacticInfoGateway;
use App\Http\HttpCodes;
use App\Http\HttpRequest;
use App\Http\HttpResponse;
use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse;
@ -15,9 +14,6 @@ use App\Model\TacticModel;
class EditorController {
private TacticModel $model;
/**
* @param TacticModel $model
*/
public function __construct() {
$this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
}

@ -3,17 +3,18 @@
namespace App\Controller;
use AltoRouter;
use App\Gateway\FormResultGateway;
use App\Http\HttpCodes;
use App\Http\HttpResponse;
use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse;
use Exception;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader;
class FrontController {
private AltoRouter $router;
public function __construct(string $basePath) {
@ -27,16 +28,12 @@ class FrontController {
* @return void
*/
public function run(): void {
$this->initializeRouterMap();
$match = $this->router->match();
if ($match != null) {
$this->handleMatch($match);
} else {
$this->diplayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND));
$this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND));
}
// $this->handleMatch($match["target"]);
// $this->handleByResponseType($this->matchRoute());
}
/**
@ -60,34 +57,32 @@ class FrontController {
$this->router->map("GET", "/", "UserController");
$this->router->map("GET", "/[a:action]?", "UserController");
$this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "EditorController");
// $this->router->map("GET", "/tactic/[i:id]/edit", "EditorController");
// $this->router->map("GET", "/", fn() => (new UserController())->home());
// $this->router->map("GET", "/tactic/new", fn() => (new EditorController(new TacticModel(new TacticInfoGateway($this->con))))->makeNew());
// $this->router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => (new EditorController(new TacticModel(new TacticInfoGateway($this->con))))->openEditorFor($id));
}
/**
* Call
*
* @return ViewHttpResponse
* @param array<string, mixed> $match
* @return void
*/
private function handleMatch($match) {
private function handleMatch(array $match): void {
$tag = $match['target'];
$action = $this->getAction($match);
$this->handleResponseByType($this->tryToCall($tag, $action, $match["params"]));
$params = $match["params"];
unset($params['action']);
$this->handleResponseByType($this->tryToCall($tag, $action, array_values($params)));
}
// private function sanitizeParam($params) {
// foreach ($key, $value : )
// }
private function tryToCall($controller, $action, array $params) {
unset($params["action"]);
$controller = $this->initControllerByRole($controller);
/**
* @param string $controller
* @param string $action
* @param array<int, mixed> $params
* @return HttpResponse
*/
private function tryToCall(string $controller, string $action, array $params): HttpResponse {
$controller = $this->getController($controller);
try {
if (is_callable(array($controller, $action))) {
return call_user_func_array(array($controller, $action), $params);
if (is_callable([$controller, $action])) {
return call_user_func_array([$controller, $action], $params);
} else {
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
}
@ -99,62 +94,39 @@ class FrontController {
/**
* Get the right method to call to do an action
*
* @param array $match
* @param array<string, mixed> $match
* @return string
*/
private function getAction(array $match): string {
if (isset($match["params"]["action"])) {
return $match["params"]["action"];
}
return "home";
return "default";
}
/**
* Initialize the right controller by the user's role
*
* @param string $controller
* @return void
* @return mixed
*/
private function initControllerByRole(string $controller) {
$index = $controller;
private function getController(string $controller) {
$namespace = "\\App\\Controller\\";
$controller = $namespace . $controller;
if (isset($_SESSION['role'])) {
if ($_SESSION['role'] == $this->dictControllerRole[$index]) {
$controller = new $controller();
return $controller;
}
}
// A décommenter/remplacer quand méthode de connexion disponible
// $connected = (new UserController())->login($this->dictControllerRole[$controller]);
// if (!$connected){
// return "null";
// }
$_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion
$controller = new $controller();
return $controller;
return new $controller();
}
/**
* Redirect the return by the response's type
*
* @param array $match
* @param HttpResponse $response
* @return void
*/
private function handleResponseByType(HttpResponse $response): void {
// $response = call_user_func_array($match['target'], $match['params']);
http_response_code($response->getCode());
if ($response instanceof ViewHttpResponse) {
$this->diplayViewByKind($response);
} else if ($response instanceof JsonHttpResponse) {
$this->displayViewByKind($response);
} elseif ($response instanceof JsonHttpResponse) {
header('Content-type: application/json');
echo $response->getJson();
}
@ -166,7 +138,7 @@ class FrontController {
* @param ViewHttpResponse $response
* @return void
*/
private function diplayViewByKind(ViewHttpResponse $response): void {
private function displayViewByKind(ViewHttpResponse $response): void {
$file = $response->getFile();
$args = $response->getArguments();
@ -177,9 +149,9 @@ class FrontController {
case ViewHttpResponse::TWIG_VIEW:
try {
$loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader);
$twig = new Environment($loader);
$twig->display($file, $args);
} catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) {
} catch (RuntimeError|SyntaxError|LoaderError $e) {
http_response_code(500);
echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s");
throw $e;
@ -187,4 +159,4 @@ class FrontController {
break;
}
}
}
}

@ -6,8 +6,11 @@ use App\Http\HttpResponse;
use App\Http\ViewHttpResponse;
class UserController {
public function home() : HttpResponse {
public function home(): HttpResponse {
return ViewHttpResponse::twig("home.twig", []);
}
public function default(): HttpResponse {
return self::home();
}
}

Loading…
Cancel
Save