collection-crud/src/Controller/CpuController.php

74 lines
2.2 KiB
PHP

<?php declare(strict_types=1);
namespace App\Controller;
use App\Entity\Cpu;
use App\Form\CpuType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
use Symfony\Component\Routing\Annotation\Route;
#[Route('/cpu')]
class CpuController extends AbstractController {
use FormControllerBase;
protected const ENTITY = Cpu::class;
protected const TEMPLATE_PATH = 'cpu/';
protected const ROUTE_PREFIX = 'cpu_';
protected const FORM = CpuType::class;
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
#[Route('/', name: 'cpu_index', methods: ['GET'])]
public function index(): Response
{
$template = self::TEMPLATE_PATH . 'index.html.twig';
$items = $this->entityManager->getRepository(self::ENTITY)->findBy([], [
'productLine' => 'ASC',
'model' => 'ASC',
]);
$amd = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'AMD' && $cpu->isReceived());
$intel = array_filter($items, static fn (Cpu $cpu) => $cpu->getBrand()->getName() === 'Intel' && $cpu->isReceived());
$others = array_filter($items, static fn (Cpu $cpu) => ( ! in_array($cpu->getBrand()->getName(), ['AMD', 'Intel'], TRUE)) && $cpu->isReceived());
$notReceived = array_filter($items, static fn (CPU $cpu) => $cpu->isReceived() === FALSE);
return $this->render($template, [
'all' => [
'not_acquired' => $notReceived,
'amd' => $amd,
'intel' => $intel,
'others' => $others,
],
]);
}
#[Route('/new', name: 'cpu_new', methods: ['GET', 'POST'])]
public function new(Request $request): Response
{
return $this->itemCreate($request, 'cpu');
}
#[Route('/{id}', name: 'cpu_show', methods: ['GET'])]
public function show(Cpu $cpu): Response
{
return $this->itemView($cpu, 'cpu');
}
#[Route('/{id}/edit', name: 'cpu_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Cpu $cpu): Response
{
return $this->itemUpdate($request, $cpu, 'cpu');
}
#[Route('/{id}', name: 'cpu_delete', methods: ['POST'])]
public function delete(Request $request, Cpu $cpu): Response
{
return $this->deleteCSRF($request, $cpu);
}
}