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 "utils.php";
require "../src/react-display.php"; require "../src/react-display.php";
use App\Connexion;
use App\Controller\FrontController; use App\Controller\FrontController;
session_start();
$basePath = get_public_path(); $basePath = get_public_path();
$frontController = new FrontController($basePath); $frontController = new FrontController($basePath);
$frontController->run(); $frontController->run();

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

@ -3,17 +3,18 @@
namespace App\Controller; namespace App\Controller;
use AltoRouter; use AltoRouter;
use App\Gateway\FormResultGateway;
use App\Http\HttpCodes; use App\Http\HttpCodes;
use App\Http\HttpResponse; use App\Http\HttpResponse;
use App\Http\JsonHttpResponse; use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
use Exception; use Exception;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader; use Twig\Loader\FilesystemLoader;
class FrontController { class FrontController {
private AltoRouter $router; private AltoRouter $router;
public function __construct(string $basePath) { public function __construct(string $basePath) {
@ -27,16 +28,12 @@ class FrontController {
* @return void * @return void
*/ */
public function run(): void { public function run(): void {
$this->initializeRouterMap();
$match = $this->router->match(); $match = $this->router->match();
if ($match != null) { if ($match != null) {
$this->handleMatch($match); $this->handleMatch($match);
} else { } 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", "/", "UserController");
$this->router->map("GET", "/[a:action]?", "UserController"); $this->router->map("GET", "/[a:action]?", "UserController");
$this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "EditorController"); $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 * @param array<string, mixed> $match
* * @return void
* @return ViewHttpResponse
*/ */
private function handleMatch($match) { private function handleMatch(array $match): void {
$tag = $match['target']; $tag = $match['target'];
$action = $this->getAction($match); $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 : ) * @param string $controller
// } * @param string $action
* @param array<int, mixed> $params
private function tryToCall($controller, $action, array $params) { * @return HttpResponse
unset($params["action"]); */
$controller = $this->initControllerByRole($controller); private function tryToCall(string $controller, string $action, array $params): HttpResponse {
$controller = $this->getController($controller);
try { try {
if (is_callable(array($controller, $action))) { if (is_callable([$controller, $action])) {
return call_user_func_array(array($controller, $action), $params); return call_user_func_array([$controller, $action], $params);
} else { } else {
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); 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 * Get the right method to call to do an action
* *
* @param array $match * @param array<string, mixed> $match
* @return string * @return string
*/ */
private function getAction(array $match): string { private function getAction(array $match): string {
if (isset($match["params"]["action"])) { if (isset($match["params"]["action"])) {
return $match["params"]["action"]; return $match["params"]["action"];
} }
return "home"; return "default";
} }
/** /**
* Initialize the right controller by the user's role * Initialize the right controller by the user's role
* *
* @param string $controller * @param string $controller
* @return void * @return mixed
*/ */
private function initControllerByRole(string $controller) { private function getController(string $controller) {
$index = $controller;
$namespace = "\\App\\Controller\\"; $namespace = "\\App\\Controller\\";
$controller = $namespace . $controller; $controller = $namespace . $controller;
return new $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;
} }
/** /**
* Redirect the return by the response's type * Redirect the return by the response's type
* *
* @param array $match * @param HttpResponse $response
* @return void * @return void
*/ */
private function handleResponseByType(HttpResponse $response): void { private function handleResponseByType(HttpResponse $response): void {
// $response = call_user_func_array($match['target'], $match['params']);
http_response_code($response->getCode()); http_response_code($response->getCode());
if ($response instanceof ViewHttpResponse) { if ($response instanceof ViewHttpResponse) {
$this->displayViewByKind($response);
$this->diplayViewByKind($response); } elseif ($response instanceof JsonHttpResponse) {
} else if ($response instanceof JsonHttpResponse) {
header('Content-type: application/json'); header('Content-type: application/json');
echo $response->getJson(); echo $response->getJson();
} }
@ -166,7 +138,7 @@ class FrontController {
* @param ViewHttpResponse $response * @param ViewHttpResponse $response
* @return void * @return void
*/ */
private function diplayViewByKind(ViewHttpResponse $response): void { private function displayViewByKind(ViewHttpResponse $response): void {
$file = $response->getFile(); $file = $response->getFile();
$args = $response->getArguments(); $args = $response->getArguments();
@ -177,9 +149,9 @@ class FrontController {
case ViewHttpResponse::TWIG_VIEW: case ViewHttpResponse::TWIG_VIEW:
try { try {
$loader = new FilesystemLoader('../src/Views/'); $loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader); $twig = new Environment($loader);
$twig->display($file, $args); $twig->display($file, $args);
} catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) { } catch (RuntimeError|SyntaxError|LoaderError $e) {
http_response_code(500); 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"); 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; throw $e;

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

Loading…
Cancel
Save