134 lines
3.6 KiB
PHP
134 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
final class ConsoleService
|
|
{
|
|
private JsonStore $store;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->store = new JsonStore(Config::get("dataPath") . "/consoles.json");
|
|
}
|
|
|
|
public function listerPourUtilisateur(string $utilisateurId): array
|
|
{
|
|
$consoles = $this->store->filtrer(
|
|
static fn (array $console): bool => ($console["utilisateurId"] ?? "") === $utilisateurId
|
|
);
|
|
|
|
usort(
|
|
$consoles,
|
|
static fn (array $a, array $b): int => strcmp($a["nom"] ?? "", $b["nom"] ?? "")
|
|
);
|
|
|
|
return $consoles;
|
|
}
|
|
|
|
public function trouverPourUtilisateur(string $id, string $utilisateurId): ?array
|
|
{
|
|
$console = $this->store->trouverParId($id);
|
|
|
|
if ($console === null || ($console["utilisateurId"] ?? "") !== $utilisateurId)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return $console;
|
|
}
|
|
|
|
public function creer(string $utilisateurId, array $donnees): array
|
|
{
|
|
$nom = trim((string) ($donnees["nom"] ?? ""));
|
|
$constructeur = trim((string) ($donnees["constructeur"] ?? ""));
|
|
|
|
if ($nom === "" || $constructeur === "")
|
|
{
|
|
throw new InvalidArgumentException("Le nom et le constructeur sont obligatoires.");
|
|
}
|
|
|
|
$annee = $this->normaliserAnnee($donnees["annee"] ?? null);
|
|
$maintenant = Helpers::maintenant();
|
|
|
|
$console = [
|
|
"id" => Helpers::genererId(),
|
|
"utilisateurId" => $utilisateurId,
|
|
"nom" => $nom,
|
|
"constructeur" => $constructeur,
|
|
"annee" => $annee,
|
|
"notes" => trim((string) ($donnees["notes"] ?? "")),
|
|
"createdAt" => $maintenant,
|
|
"updatedAt" => $maintenant
|
|
];
|
|
|
|
return $this->store->ajouter($console);
|
|
}
|
|
|
|
public function modifier(string $id, string $utilisateurId, array $donnees): array
|
|
{
|
|
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
|
|
|
if ($console === null)
|
|
{
|
|
throw new RuntimeException("Console introuvable.");
|
|
}
|
|
|
|
$nom = trim((string) ($donnees["nom"] ?? ""));
|
|
$constructeur = trim((string) ($donnees["constructeur"] ?? ""));
|
|
|
|
if ($nom === "" || $constructeur === "")
|
|
{
|
|
throw new InvalidArgumentException("Le nom et le constructeur sont obligatoires.");
|
|
}
|
|
|
|
$miseAJour = $this->store->mettreAJour($id, [
|
|
"nom" => $nom,
|
|
"constructeur" => $constructeur,
|
|
"annee" => $this->normaliserAnnee($donnees["annee"] ?? null),
|
|
"notes" => trim((string) ($donnees["notes"] ?? "")),
|
|
"updatedAt" => Helpers::maintenant()
|
|
]);
|
|
|
|
if ($miseAJour === null)
|
|
{
|
|
throw new RuntimeException("Échec de la mise à jour.");
|
|
}
|
|
|
|
return $miseAJour;
|
|
}
|
|
|
|
public function supprimer(string $id, string $utilisateurId): bool
|
|
{
|
|
$console = $this->trouverPourUtilisateur($id, $utilisateurId);
|
|
|
|
if ($console === null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return $this->store->supprimer($id);
|
|
}
|
|
|
|
public function compterPourUtilisateur(string $utilisateurId): int
|
|
{
|
|
return count($this->listerPourUtilisateur($utilisateurId));
|
|
}
|
|
|
|
private function normaliserAnnee(mixed $annee): ?int
|
|
{
|
|
if ($annee === null || $annee === "")
|
|
{
|
|
return null;
|
|
}
|
|
|
|
$valeur = (int) $annee;
|
|
|
|
if ($valeur < 1970 || $valeur > (int) date("Y") + 2)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return $valeur;
|
|
}
|
|
}
|