collection-crud/src/Controller/CpuController.php

95 lines
2.7 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\
{RedirectResponse, Request, Response};
use Symfony\Component\Form\FormInterface;
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);
}
/**
* Moves a cpu to the previouslyOwned table
*/
#[Route(path: '/{id}/deacquire', name: 'cpu_deacquire', methods: ['POST'])]
public function reacquireAction(Request $request, Cpu $cpu): RedirectResponse
{
return $this->itemDeacquire($request, $cpu, 'previously_owned_cpu_index');
}
/**
* Creates a form to move
*
* @param cpu $cpu The cpu entity
*/
private function createDeacquireForm(cpu $cpu): FormInterface
{
return $this->buildForm($cpu, 'cpu_deacquire');
}
}