You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Web/Sources/src/data/model/manager/UserManager.php

82 lines
2.1 KiB

<?php
namespace Manager;
use Model\Athlete;
use Model\Coach;
use Model\User;
use Network\IAuthService;
use Shared\Validation;
// c'est le modéle
// should be here try catch ??
class UserManager
{
private IAuthService $authService;
public User $currentUser;
public function __construct(IAuthService $authService)
{
$this->authService = $authService;
}
public function login($loginUser, $passwordUser): bool
{
if (!Validation::val_string($passwordUser) || !Validation::val_string($loginUser))
throw new \Exception(" some wrong with cred !!!!!");
$user = $this->authService->login($loginUser, $passwordUser);
if ($user) {
$this->currentUser = $user;
return true;
}
return false;
}
public function register($loginUser, $passwordUser,$data): bool
{
// foreach ($data as $entry) {
// $isValid = Validation::val_string($entry);
// if (!$isValid) {
// throw new \Exception( "Entry '$entry' is not a valid string.");
// }
// }
$fieldsToValidate = [
'nom' => 'Nom non valide',
'prenom' => 'Prénom non valide',
'taille' => 'Taille non valide',
'poids' => 'Poids non valide',
];
foreach ($fieldsToValidate as $fieldName => $errorMessage) {
if (!Validation::val_string($data[$fieldName])) {
throw new \Exception($errorMessage);
}
}
$roleName = $data['roleName'];
if ($roleName !== "Athlete" && $roleName !== "Coach") {
throw new \Exception("Rôle non valide");
}
if ($data['sexe'] !== "M" && $data['sexe'] !== "H") {
throw new \Exception("Sexe non valide");
}
if ($this->authService->register($loginUser, $passwordUser, $data)) {
return true;
}
return false;
}
public function deconnecter(): bool
{
if($this->authService->logoutUser()){
return true;
}
return false;
}
}
?>