2022-10-27 11:55:16 -04:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Entity\Cpu;
|
2024-05-28 18:44:29 -04:00
|
|
|
use App\Enum\CpuVendorString;
|
2022-10-27 11:55:16 -04:00
|
|
|
use App\Form\CpuType;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2024-03-27 16:23:08 -04:00
|
|
|
use Symfony\Component\HttpFoundation\
|
|
|
|
{RedirectResponse, Request, Response};
|
|
|
|
use Symfony\Component\Form\FormInterface;
|
2022-10-27 11:55:16 -04:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
|
|
|
#[Route('/cpu')]
|
|
|
|
class CpuController extends AbstractController {
|
2023-07-21 10:23:16 -04:00
|
|
|
use FormControllerBase;
|
2022-10-27 11:55:16 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/', name: 'cpu_index', methods: ['GET'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function index(): Response
|
|
|
|
{
|
2022-11-03 10:46:44 -04:00
|
|
|
$template = self::TEMPLATE_PATH . 'index.html.twig';
|
|
|
|
|
|
|
|
$items = $this->entityManager->getRepository(self::ENTITY)->findBy([], [
|
|
|
|
'productLine' => 'ASC',
|
|
|
|
'model' => 'ASC',
|
|
|
|
]);
|
|
|
|
|
2024-05-28 18:44:29 -04:00
|
|
|
$compare = static fn (Cpu $cpu1, Cpu $cpu2) => $cpu1->getId() <=> $cpu2->getId();
|
|
|
|
$filterByVendor = static function(array $items, CpuVendorString $vendorString): array
|
|
|
|
{
|
|
|
|
return array_filter($items, static fn (Cpu $cpu) => $cpu->getVendorString() === $vendorString);
|
|
|
|
};
|
|
|
|
|
2023-07-21 10:35:15 -04:00
|
|
|
$notReceived = array_filter($items, static fn (CPU $cpu) => $cpu->isReceived() === FALSE);
|
2024-05-28 18:44:29 -04:00
|
|
|
$items = array_udiff($items, $notReceived, $compare);
|
|
|
|
$noVendor = array_filter($items, static fn (Cpu $cpu) => $cpu->getVendorString() === null || $cpu->getVendorString() === '');
|
|
|
|
$items = array_udiff($items, $noVendor, $compare);
|
|
|
|
$amd = $filterByVendor($items, CpuVendorString::AMD);
|
|
|
|
$intel = $filterByVendor($items, CpuVendorString::INTEL);
|
|
|
|
$centaur = $filterByVendor($items, CpuVendorString::CENTAUR);
|
|
|
|
$cyrix = $filterByVendor($items, CpuVendorString::CYRIX);
|
|
|
|
$others = array_udiff($items, $amd, $intel, $centaur, $cyrix, $compare);
|
|
|
|
|
|
|
|
$sort = static function (Cpu $cpu1, Cpu $cpu2) {
|
|
|
|
$brandSort = $cpu1->getBrand()->getName() <=> $cpu2->getBrand()->getName();
|
|
|
|
$modelSort = $cpu1->getProductLine() <=> $cpu2->getProductLine();
|
|
|
|
|
|
|
|
return ($brandSort !== 0) ? $brandSort : $modelSort;
|
|
|
|
};
|
|
|
|
usort($notReceived, $sort);
|
|
|
|
usort($noVendor, $sort);
|
|
|
|
usort($amd, $sort);
|
|
|
|
usort($intel, $sort);
|
|
|
|
usort($centaur, $sort);
|
|
|
|
usort($cyrix, $sort);
|
|
|
|
usort($others, $sort);
|
2022-11-03 10:46:44 -04:00
|
|
|
|
|
|
|
return $this->render($template, [
|
|
|
|
'all' => [
|
|
|
|
'not_acquired' => $notReceived,
|
|
|
|
'amd' => $amd,
|
|
|
|
'intel' => $intel,
|
2024-05-28 18:44:29 -04:00
|
|
|
'centaur' => $centaur,
|
|
|
|
'cyrix' => $cyrix,
|
|
|
|
'no_vendor' => $noVendor,
|
2022-11-03 10:46:44 -04:00
|
|
|
'others' => $others,
|
|
|
|
],
|
|
|
|
]);
|
2022-10-27 11:55:16 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/new', name: 'cpu_new', methods: ['GET', 'POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function new(Request $request): Response
|
|
|
|
{
|
|
|
|
return $this->itemCreate($request, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}', name: 'cpu_show', methods: ['GET'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function show(Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->itemView($cpu, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}/edit', name: 'cpu_edit', methods: ['GET', 'POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function edit(Request $request, Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->itemUpdate($request, $cpu, 'cpu');
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:46:35 -04:00
|
|
|
#[Route('/{id}', name: 'cpu_delete', methods: ['POST'])]
|
2022-10-27 11:55:16 -04:00
|
|
|
public function delete(Request $request, Cpu $cpu): Response
|
|
|
|
{
|
|
|
|
return $this->deleteCSRF($request, $cpu);
|
|
|
|
}
|
2024-03-27 16:23:08 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
2022-10-27 11:55:16 -04:00
|
|
|
}
|