collection-crud/src/Controller/CameraController.php

147 lines
4.3 KiB
PHP

<?php declare(strict_types=1);
namespace App\Controller;
use App\Entity\Camera;
use App\Form\CameraType;
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;
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/new.html.twig', 'camera', 'camera_show');
}
/**
* 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/show.html.twig', '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/edit.html.twig', 'camera', 'camera_show');
}
/**
* Deletes a camera entity.
*
* @throws LogicException
*/
#[Route(path: '/{id}', name: 'camera_delete', methods: ['DELETE'])]
public function deleteAction(Request $request, Camera $camera): RedirectResponse
{
return $this->itemDelete($request, $camera, 'camera_index');
}
/**
* 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 delete a camera entity.
*
* @param Camera $camera The camera entity
*
* @return FormInterface The form
*/
private function createDeleteForm(Camera $camera): FormInterface
{
return $this->buildForm($camera, 'camera_delete', 'DELETE');
}
/**
* 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);
}
}