collection-crud/src/Controller/CameraController.php

138 lines
4.0 KiB
PHP

<?php declare(strict_types=1);
namespace App\Controller;
use App\Entity\Camera;
use App\Form\CameraType;
use App\Traits\DeleteFormTrait;
use App\Traits\FormControllerTrait;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMInvalidArgumentException;
use LogicException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\{RedirectResponse, Request, Response};
use Symfony\Component\Routing\Annotation\Route;
/**
* Camera controller.
*/
#[Route(path: 'camera')]
class CameraController extends AbstractController
{
use FormControllerTrait;
use DeleteFormTrait;
protected const ENTITY = Camera::class;
protected const TEMPLATE_PATH = 'camera/';
protected const ROUTE_PREFIX = 'camera_';
protected const FORM = CameraType::class;
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
/**
* Lists all camera entities.
*/
#[Route(path: '/', name: 'camera_index', methods: ['GET'])]
public function indexAction(): Response
{
$receivedItems = $this->entityManager->getRepository(self::ENTITY)->findBy([
'received' => TRUE,
], [
'isWorking' => 'ASC',
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
$newItems = $this->entityManager->getRepository(self::ENTITY)->findBy([
'received' => FALSE,
], [
'brand' => 'ASC',
'mount' => 'ASC',
'model' => 'ASC',
]);
$working = array_filter($receivedItems, [$this, 'isWorking']);
$notWorking = array_filter($receivedItems, [$this, 'isNotWorking']);
return $this->render('camera/index.html.twig', [
'not_received' => $newItems,
'not_working' => $notWorking,
'working' => $working,
]);
}
/**
* Creates a new camera entity.
*/
#[Route(path: '/new', name: 'camera_new', methods: ['GET', 'POST'])]
public function newAction(Request $request): RedirectResponse|Response
{
return $this->itemCreate($request,'camera');
}
/**
* Finds and displays a camera entity.
*/
#[Route(path: '/{id}', name: 'camera_show', methods: ['GET'])]
public function showAction(Camera $camera): Response
{
return $this->itemView($camera,'camera');
}
/**
* Displays a form to edit an existing camera entity.
*
* @throws LogicException
*/
#[Route(path: '/{id}/edit', name: 'camera_edit', methods: ['GET', 'POST'])]
public function editAction(Request $request, Camera $camera): RedirectResponse|Response
{
return $this->itemUpdate($request, $camera,'camera');
}
/**
* Deletes a camera entity.
*
* @throws LogicException
*/
#[Route(path: '/{id}', name: 'camera_delete', methods: ['POST','DELETE'])]
public function deleteAction(Request $request, Camera $camera): RedirectResponse
{
return $this->itemDelete($request, $camera);
}
/**
* Moves a camera to the previouslyOwned table
*
* @throws LogicException
* @throws ORMInvalidArgumentException
*/
#[Route(path: '/{id}/deacquire', name: 'camera_deacquire', methods: ['POST'])]
public function deacquireAction(Request $request, Camera $camera): RedirectResponse
{
return $this->itemDeacquire($request, $camera, 'previously-owned-camera_index');
}
/**
* Creates a form to move
*
* @param Camera $camera The camera entity
*/
private function createDeacquireForm(Camera $camera): FormInterface
{
return $this->buildForm($camera, 'camera_deacquire');
}
private function isWorking(Camera $camera): bool
{
return $camera->getIsWorking();
}
private function isNotWorking(Camera $camera): bool
{
return ! $this->isWorking($camera);
}
}