From c1fec9243b0057d90547e0874116230a809fe14f Mon Sep 17 00:00:00 2001 From: samuel Date: Tue, 21 Nov 2023 11:12:11 +0100 Subject: [PATCH] format and fix phpstan --- front/views/Visualizer.tsx | 7 +++-- src/Controller/AuthController.php | 43 +++++++++++++++++++------------ src/Gateway/AuthGateway.php | 22 +++++++++------- src/Model/AuthModel.php | 39 +++++++++++++++++++--------- 4 files changed, 70 insertions(+), 41 deletions(-) diff --git a/front/views/Visualizer.tsx b/front/views/Visualizer.tsx index ddf7fe2..541da09 100644 --- a/front/views/Visualizer.tsx +++ b/front/views/Visualizer.tsx @@ -2,9 +2,8 @@ import React, { CSSProperties, useState } from "react" import "../style/visualizer.css" import Court from "../assets/basketball_court.svg" - -export default function Visualizer({id, name}: { id: number; name: string }) { - const [style, setStyle] = useState({}); +export default function Visualizer({ id, name }: { id: number; name: string }) { + const [style, setStyle] = useState({}) return (
@@ -20,5 +19,5 @@ export default function Visualizer({id, name}: { id: number; name: string }) { />
- ); + ) } diff --git a/src/Controller/AuthController.php b/src/Controller/AuthController.php index b4ef8cb..e42c27d 100644 --- a/src/Controller/AuthController.php +++ b/src/Controller/AuthController.php @@ -8,10 +8,10 @@ use App\Http\HttpResponse; use App\Http\ViewHttpResponse; use App\Model\AuthModel; use App\Validation\FieldValidationFail; +use App\Validation\ValidationFail; use App\Validation\Validators; use Twig\Environment; - class AuthController { private AuthModel $model; @@ -26,7 +26,12 @@ class AuthController { return ViewHttpResponse::twig("display_register.html.twig", []); } - private function displayBadFields(string $viewName, array $fails): HttpResponse{ + /** + * @param string $viewName + * @param ValidationFail[] $fails + * @return HttpResponse + */ + private function displayBadFields(string $viewName, array $fails): HttpResponse { $bad_fields = []; foreach ($fails as $err) { if ($err instanceof FieldValidationFail) { @@ -36,48 +41,54 @@ class AuthController { return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]); } - + /** + * @param mixed[] $request + * @return HttpResponse + */ public function confirmRegister(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "username" => [Validators::name(), Validators::lenBetween(2, 32)], "password" => [Validators::lenBetween(6, 256)], "confirmpassword" => [Validators::lenBetween(6, 256)], - "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], ]); if (!empty($fails)) { - return $this->displayBadFields("display_register.html.twig",$fails); + return $this->displayBadFields("display_register.html.twig", $fails); } $fails = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email']); if (empty($fails)) { $results = $this->model->getUserFields($request['email']); return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayBadFields("display_register.html.twig",$fails); + return $this->displayBadFields("display_register.html.twig", $fails); } - public function displayLogin():HttpResponse{ + public function displayLogin(): HttpResponse { return ViewHttpResponse::twig("display_login.html.twig", []); } - - public function confirmLogin(array $request):HttpResponse{ + /** + * @param mixed[] $request + * @return HttpResponse + */ + public function confirmLogin(array $request): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ "password" => [Validators::lenBetween(6, 256)], - "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)] + "email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)], ]); if (!empty($fails)) { - return $this->displayBadFields("display_login.html.twig",$fails); + return $this->displayBadFields("display_login.html.twig", $fails); } - $fails = $this->model->login($request['email'],$request['password']); - if (empty($fails)){ + $fails = $this->model->login($request['email'], $request['password']); + if (empty($fails)) { $results = $this->model->getUserFields($request['email']); - return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]); + return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]); } - return $this->displayBadFields("display_login.html.twig",$fails); + return $this->displayBadFields("display_login.html.twig", $fails); } -} \ No newline at end of file +} diff --git a/src/Gateway/AuthGateway.php b/src/Gateway/AuthGateway.php index e6c7199..5acc01c 100644 --- a/src/Gateway/AuthGateway.php +++ b/src/Gateway/AuthGateway.php @@ -3,7 +3,7 @@ namespace App\Gateway; use App\Connexion; -use \PDO; +use PDO; class AuthGateway { private Connexion $con; @@ -16,23 +16,27 @@ class AuthGateway { } - public function mailExist(string $email):bool{ + public function mailExist(string $email): bool { return $this->getUserFields($email) != null; } - public function insertAccount(string $username, string $hash, string $email):void{ - $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash'=> [$hash, PDO::PARAM_STR],':email'=>[$email, PDO::PARAM_STR]]); + public function insertAccount(string $username, string $hash, string $email): void { + $this->con->exec("INSERT INTO AccountUser VALUES (:username,:hash,:email)", [':username' => [$username, PDO::PARAM_STR],':hash' => [$hash, PDO::PARAM_STR],':email' => [$email, PDO::PARAM_STR]]); } - public function getUserHash(string $email):string{ - $results = $this->con->fetch ("SELECT hash FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + public function getUserHash(string $email): string { + $results = $this->con->fetch("SELECT hash FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); return $results[0]['hash']; } - public function getUserFields (string $email): ?array { - $results = $this->con->fetch ("SELECT username,email FROM AccountUser WHERE email = :email",[':email'=>[$email, PDO::PARAM_STR]]); + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { + $results = $this->con->fetch("SELECT username,email FROM AccountUser WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]); $firstRow = $results[0] ?? null; return $firstRow; } @@ -40,4 +44,4 @@ class AuthGateway { -} \ No newline at end of file +} diff --git a/src/Model/AuthModel.php b/src/Model/AuthModel.php index fd00bb8..45b63e4 100644 --- a/src/Model/AuthModel.php +++ b/src/Model/AuthModel.php @@ -5,10 +5,9 @@ namespace App\Model; use App\Controller\AuthController; use App\Gateway\AuthGateway; use App\Validation\FieldValidationFail; +use App\Validation\ValidationFail; class AuthModel { - - private AuthGateway $gateway; /** * @param AuthGateway $gateway @@ -18,31 +17,47 @@ class AuthModel { } - public function register(string $username, string $password, string $confirmPassword,string $email): array { + /** + * @param string $username + * @param string $password + * @param string $confirmPassword + * @param string $email + * @return ValidationFail[] + */ + public function register(string $username, string $password, string $confirmPassword, string $email): array { $errors = []; if ($password != $confirmPassword) { - $errors[] = new FieldValidationFail("confirmpassword","password and password confirmation are not equals"); + $errors[] = new FieldValidationFail("confirmpassword", "password and password confirmation are not equals"); } - if ($this->gateway->mailExist($email)){ - $errors[] = new FieldValidationFail("email","email already exist"); + if ($this->gateway->mailExist($email)) { + $errors[] = new FieldValidationFail("email", "email already exist"); } - if(empty($errors)){ - $hash = password_hash($password,PASSWORD_DEFAULT); - $this->gateway->insertAccount($username,$hash,$email); + if(empty($errors)) { + $hash = password_hash($password, PASSWORD_DEFAULT); + $this->gateway->insertAccount($username, $hash, $email); } return $errors; } - public function getUserFields(string $email):array{ + /** + * @param string $email + * @return array|null + */ + public function getUserFields(string $email): ?array { return $this->gateway->getUserFields($email); } - public function login(string $email,string $password): array{ + /** + * @param string $email + * @param string $password + * @return ValidationFail[] $errors + */ + public function login(string $email, string $password): array { $errors = []; if (!$this->gateway->mailExist($email)) { @@ -62,4 +77,4 @@ class AuthModel { -} \ No newline at end of file +}