Refatoring : Working state

This commit is contained in:
julien
2026-03-16 14:11:49 +01:00
parent 073e23a9f8
commit d0761ff010
21 changed files with 1262 additions and 1301 deletions

View File

@@ -3,76 +3,10 @@ declare(strict_types=1);
namespace App\Category;
use App\Shared\Http\FlashServiceInterface;
use App\Shared\Pagination\PaginationPresenter;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
final class CategoryController
/**
* Pont de compatibilité : le contrôleur HTTP principal vit désormais dans
* App\Category\Http\CategoryController.
*/
final class CategoryController extends Http\CategoryController
{
private const PER_PAGE = 20;
public function __construct(
private readonly Twig $view,
private readonly CategoryServiceInterface $categoryService,
private readonly FlashServiceInterface $flash,
) {
}
public function index(Request $req, Response $res): Response
{
$page = PaginationPresenter::resolvePage($req->getQueryParams());
$paginated = $this->categoryService->findPaginated($page, self::PER_PAGE);
return $this->view->render($res, 'admin/categories/index.twig', [
'categories' => $paginated->getItems(),
'pagination' => PaginationPresenter::fromRequest($req, $paginated),
'error' => $this->flash->get('category_error'),
'success' => $this->flash->get('category_success'),
]);
}
public function create(Request $req, Response $res): Response
{
$data = (array) $req->getParsedBody();
$name = (string) ($data['name'] ?? '');
try {
$this->categoryService->create($name);
$trimmed = trim($name);
$this->flash->set('category_success', "La catégorie « {$trimmed} » a été créée avec succès");
} catch (\InvalidArgumentException $e) {
$this->flash->set('category_error', $e->getMessage());
} catch (\Throwable) {
$this->flash->set('category_error', "Une erreur inattendue s'est produite");
}
return $res->withHeader('Location', '/admin/categories')->withStatus(302);
}
/**
* @param array<string, mixed> $args
*/
public function delete(Request $req, Response $res, array $args): Response
{
$id = (int) ($args['id'] ?? 0);
$category = $this->categoryService->findById($id);
if ($category === null) {
$this->flash->set('category_error', 'Catégorie introuvable');
return $res->withHeader('Location', '/admin/categories')->withStatus(302);
}
try {
$this->categoryService->delete($category);
$this->flash->set('category_success', "La catégorie « {$category->getName()} » a été supprimée");
} catch (\InvalidArgumentException $e) {
$this->flash->set('category_error', $e->getMessage());
} catch (\Throwable) {
$this->flash->set('category_error', "Une erreur inattendue s'est produite");
}
return $res->withHeader('Location', '/admin/categories')->withStatus(302);
}
}