diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 0600a01..7d61b28 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -14,10 +14,12 @@ use Twig\Error\RuntimeError; use Twig\Error\SyntaxError; use Twig\Loader\FilesystemLoader; -class FrontController { +class FrontController +{ private AltoRouter $router; - public function __construct(string $basePath) { + public function __construct(string $basePath) + { $this->router = $this->createRouter($basePath); $this->initializeRouterMap(); } @@ -27,7 +29,8 @@ class FrontController { * * @return void */ - public function run(): void { + public function run(): void + { $match = $this->router->match(); if ($match != null) { $this->handleMatch($match); @@ -42,7 +45,8 @@ class FrontController { * @param string $basePath * @return AltoRouter */ - public function createRouter(string $basePath): AltoRouter { + public function createRouter(string $basePath): AltoRouter + { $router = new AltoRouter(); $router->setBasePath($basePath); return $router; @@ -53,17 +57,19 @@ class FrontController { * * @return void */ - private function initializeRouterMap(): void { + private function initializeRouterMap(): void + { $this->router->map("GET", "/", "UserController"); - $this->router->map("GET", "/[a:action]?", "UserController"); - $this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "UserController"); + $this->router->map("GET|POST", "/[a:action]?/[i:id]", "UserController"); + $this->router->map("GET|POST", "/tactic/[a:action]/[i:idTactic]?", "UserController"); } /** * @param array $match * @return void */ - private function handleMatch(array $match): void { + private function handleMatch(array $match): void + { $tag = $match['target']; $action = $this->getAction($match); @@ -78,15 +84,12 @@ class FrontController { * @param array $params * @return HttpResponse */ - private function tryToCall(string $controller, string $action, array $params): HttpResponse { + private function tryToCall(string $controller, string $action, array $params): HttpResponse + { $controller = $this->getController($controller); - try { - if (is_callable([$controller, $action])) { - return call_user_func_array([$controller, $action], $params); - } else { - return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); - } - } catch (Exception $e) { + if (is_callable([$controller, $action])) { + return call_user_func_array([$controller, $action], $params); + } else { return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); } } @@ -97,7 +100,8 @@ class FrontController { * @param array $match * @return string */ - private function getAction(array $match): string { + private function getAction(array $match): string + { if (isset($match["params"]["action"])) { return $match["params"]["action"]; } @@ -110,7 +114,8 @@ class FrontController { * @param string $controller * @return mixed */ - private function getController(string $controller) { + private function getController(string $controller) + { $namespace = "\\App\\Controller\\"; $controller = $namespace . $controller; return new $controller(); @@ -122,7 +127,8 @@ class FrontController { * @param HttpResponse $response * @return void */ - private function handleResponseByType(HttpResponse $response): void { + private function handleResponseByType(HttpResponse $response): void + { http_response_code($response->getCode()); if ($response instanceof ViewHttpResponse) { $this->displayViewByKind($response); @@ -138,7 +144,8 @@ class FrontController { * @param ViewHttpResponse $response * @return void */ - private function displayViewByKind(ViewHttpResponse $response): void { + private function displayViewByKind(ViewHttpResponse $response): void + { $file = $response->getFile(); $args = $response->getArguments(); @@ -151,7 +158,7 @@ class FrontController { $loader = new FilesystemLoader('../src/Views/'); $twig = new Environment($loader); $twig->display($file, $args); - } catch (RuntimeError|SyntaxError|LoaderError $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; diff --git a/src/Controller/Sub/EditorController.php b/src/Controller/Sub/EditorController.php index def6ac9..ce7a0a3 100644 --- a/src/Controller/Sub/EditorController.php +++ b/src/Controller/Sub/EditorController.php @@ -14,8 +14,8 @@ use App\Model\TacticModel; class EditorController { private TacticModel $model; - public function __construct() { - $this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database()))); + public function __construct(TacticModel $model) { + $this->model = $model; } private function openEditor(TacticInfo $tactic): HttpResponse { diff --git a/src/Controller/TeamController.php b/src/Controller/Sub/TeamController.php similarity index 98% rename from src/Controller/TeamController.php rename to src/Controller/Sub/TeamController.php index 08e22cc..c3991a3 100644 --- a/src/Controller/TeamController.php +++ b/src/Controller/Sub/TeamController.php @@ -1,6 +1,6 @@ createNew(); } + + public function createTeam(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displaySubmitTeam(); + } + return $ctrl->submitTeam($_POST); + } + + public function listTeams(): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + if ($_SERVER['REQUEST_METHOD'] === 'GET') { + return $ctrl->displayListTeamByName(); + } + return $ctrl->listTeamByName($_POST); + } + + public function getTeam(int $id): HttpResponse { + $model = new TeamModel(new TeamGateway(new Connexion(get_database()))); + $ctrl = new Sub\TeamController($model); + return $ctrl->getTeam($id); + } } diff --git a/src/Views/display_teams.html.twig b/src/Views/display_teams.html.twig index c0ac185..47b3cbc 100644 --- a/src/Views/display_teams.html.twig +++ b/src/Views/display_teams.html.twig @@ -10,7 +10,7 @@

Aucune équipe n'a été trouvée

Chercher une équipe

-
+
@@ -22,7 +22,7 @@
{% else %} {% for t in teams %} -
+

Nom de l'équipe : {{ t.name }}

logo de l'équipe
diff --git a/src/Views/insert_team.html.twig b/src/Views/insert_team.html.twig index 0cc85dd..fbe6917 100644 --- a/src/Views/insert_team.html.twig +++ b/src/Views/insert_team.html.twig @@ -64,7 +64,7 @@

Créer une équipe

- +
diff --git a/src/Views/list_team_by_name.html.twig b/src/Views/list_team_by_name.html.twig index 1d9ddf3..1165da3 100644 --- a/src/Views/list_team_by_name.html.twig +++ b/src/Views/list_team_by_name.html.twig @@ -62,7 +62,7 @@

Chercher une équipe

- +