mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -23,8 +23,6 @@ use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use function array_merge;
|
||||
|
||||
abstract class AbstractCRUDController extends AbstractController
|
||||
{
|
||||
/**
|
||||
@@ -49,14 +47,12 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
return $this->crudConfig['base_role'];
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('the config does not have any role for the ' .
|
||||
'method %s nor a global role for the whole action. Add those to your ' .
|
||||
'configuration or override the required method', $request->getMethod()));
|
||||
throw new \RuntimeException(sprintf('the config does not have any role for the method %s nor a global role for the whole action. Add those to your configuration or override the required method', $request->getMethod()));
|
||||
}
|
||||
|
||||
public static function getSubscribedServices(): array
|
||||
{
|
||||
return array_merge(
|
||||
return \array_merge(
|
||||
parent::getSubscribedServices(),
|
||||
[
|
||||
'chill_main.paginator_factory' => PaginatorFactory::class,
|
||||
@@ -153,7 +149,7 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The crud name.
|
||||
* @return string the crud name
|
||||
*/
|
||||
protected function getCrudName(): string
|
||||
{
|
||||
|
@@ -13,9 +13,6 @@ namespace Chill\MainBundle\CRUD\Controller;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Exception;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
@@ -24,15 +21,11 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
|
||||
use function array_merge;
|
||||
use function ucfirst;
|
||||
|
||||
class ApiController extends AbstractCRUDController
|
||||
{
|
||||
/**
|
||||
* Base method for handling api action.
|
||||
*
|
||||
* @param string $_format
|
||||
* @return void
|
||||
*/
|
||||
public function entityApi(Request $request, mixed $id, ?string $_format = 'json'): Response
|
||||
@@ -51,8 +44,7 @@ class ApiController extends AbstractCRUDController
|
||||
$entity = $this->getEntity($action, $id, $request);
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s '
|
||||
. 'is not found', $this->getCrudName(), $id));
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $request, $_format, $entity);
|
||||
@@ -119,8 +111,7 @@ class ApiController extends AbstractCRUDController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s '
|
||||
. 'is not found', $this->getCrudName(), $id));
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $request, $_format, $entity);
|
||||
@@ -214,10 +205,11 @@ class ApiController extends AbstractCRUDController
|
||||
*
|
||||
* @param string action
|
||||
* @param mixed id
|
||||
* @param string $property the name of the property. This will be used to make a `add+$property` and `remove+$property` method
|
||||
* @param string $postedDataType the type of the posted data (the content)
|
||||
* @param string $property the name of the property. This will be used to make a `add+$property` and `remove+$property` method
|
||||
* @param string $postedDataType the type of the posted data (the content)
|
||||
* @param string $postedDataContext a context to deserialize posted data (the content)
|
||||
* @param bool $forcePersist force to persist the created element (only for POST request)
|
||||
* @param bool $forcePersist force to persist the created element (only for POST request)
|
||||
*
|
||||
* @throw BadRequestException if unable to deserialize the posted data
|
||||
* @throw BadRequestException if the method is not POST or DELETE
|
||||
*/
|
||||
@@ -252,15 +244,14 @@ class ApiController extends AbstractCRUDController
|
||||
try {
|
||||
$postedData = $this->getSerializer()->deserialize($request->getContent(), $postedDataType, $_format, $postedDataContext);
|
||||
} catch (\Symfony\Component\Serializer\Exception\UnexpectedValueException $e) {
|
||||
throw new BadRequestHttpException(sprintf('Unable to deserialize posted ' .
|
||||
'data: %s', $e->getMessage()), $e, 0);
|
||||
throw new BadRequestHttpException(sprintf('Unable to deserialize posted data: %s', $e->getMessage()), $e, 0);
|
||||
}
|
||||
|
||||
match ($request->getMethod()) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_DELETE => $entity->{'remove' . ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_POST => $entity->{'add' . ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_DELETE => $entity->{'remove'.\ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_POST => $entity->{'add'.\ucfirst($property)}($postedData),
|
||||
default => throw new BadRequestHttpException('this method is not supported'),
|
||||
};
|
||||
|
||||
@@ -277,7 +268,7 @@ class ApiController extends AbstractCRUDController
|
||||
return $this->json($errors, 422);
|
||||
}
|
||||
|
||||
if ($forcePersist && $request->getMethod() === Request::METHOD_POST) {
|
||||
if ($forcePersist && Request::METHOD_POST === $request->getMethod()) {
|
||||
$this->getDoctrine()->getManager()->persist($postedData);
|
||||
}
|
||||
|
||||
@@ -288,6 +279,7 @@ class ApiController extends AbstractCRUDController
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_DELETE => $this->json('', Response::HTTP_OK),
|
||||
Request::METHOD_POST => $this->json(
|
||||
@@ -296,7 +288,7 @@ class ApiController extends AbstractCRUDController
|
||||
[],
|
||||
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity, [$postedData])
|
||||
),
|
||||
default => throw new Exception('Unable to handle such request method.'),
|
||||
default => throw new \Exception('Unable to handle such request method.'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -313,7 +305,7 @@ class ApiController extends AbstractCRUDController
|
||||
$default[AbstractNormalizer::OBJECT_TO_POPULATE] = $entity;
|
||||
}
|
||||
|
||||
$context = array_merge(
|
||||
$context = \array_merge(
|
||||
$default,
|
||||
$this->getContextForSerialization($action, $request, $_format, $entity)
|
||||
);
|
||||
@@ -438,7 +430,7 @@ class ApiController extends AbstractCRUDController
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_GET => ['groups' => ['read']],
|
||||
Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_POST => ['groups' => ['write']],
|
||||
default => throw new LogicException('get context for serialization is not implemented for this method'),
|
||||
default => throw new \LogicException('get context for serialization is not implemented for this method'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -453,7 +445,6 @@ class ApiController extends AbstractCRUDController
|
||||
return ['groups' => ['read']];
|
||||
}
|
||||
|
||||
|
||||
protected function getSerializer(): SerializerInterface
|
||||
{
|
||||
return $this->get('serializer');
|
||||
|
@@ -19,7 +19,6 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
@@ -30,9 +29,6 @@ use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
|
||||
class CRUDController extends AbstractController
|
||||
{
|
||||
/**
|
||||
@@ -47,9 +43,6 @@ class CRUDController extends AbstractController
|
||||
return new Response($parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*/
|
||||
public function delete(Request $request, $id): Response
|
||||
{
|
||||
return $this->deleteAction('delete', $request, $id);
|
||||
@@ -75,7 +68,7 @@ class CRUDController extends AbstractController
|
||||
|
||||
public static function getSubscribedServices(): array
|
||||
{
|
||||
return array_merge(
|
||||
return \array_merge(
|
||||
parent::getSubscribedServices(),
|
||||
[
|
||||
'chill_main.paginator_factory' => PaginatorFactory::class,
|
||||
@@ -178,7 +171,6 @@ class CRUDController extends AbstractController
|
||||
* Throw an \Symfony\Component\Security\Core\Exception\AccessDeniedHttpException
|
||||
* if not accessible.
|
||||
*
|
||||
*
|
||||
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedHttpException
|
||||
*/
|
||||
protected function checkACL(string $action, mixed $entity)
|
||||
@@ -189,7 +181,7 @@ class CRUDController extends AbstractController
|
||||
/**
|
||||
* Count the number of entities.
|
||||
*/
|
||||
protected function countEntities(string $action, Request $request, ?FilterOrderHelper $filterOrder = null): int
|
||||
protected function countEntities(string $action, Request $request, FilterOrderHelper $filterOrder = null): int
|
||||
{
|
||||
return $this->buildQueryEntities($action, $request)
|
||||
->select('COUNT(e)')
|
||||
@@ -217,10 +209,8 @@ class CRUDController extends AbstractController
|
||||
*
|
||||
* It is preferable to override customizeForm instead of overriding
|
||||
* this method.
|
||||
*
|
||||
* @param string $formClass
|
||||
*/
|
||||
protected function createFormFor(string $action, mixed $entity, ?string $formClass = null, array $formOptions = []): FormInterface
|
||||
protected function createFormFor(string $action, mixed $entity, string $formClass = null, array $formOptions = []): FormInterface
|
||||
{
|
||||
$formClass ??= $this->getFormClassFor($action);
|
||||
|
||||
@@ -239,7 +229,6 @@ class CRUDController extends AbstractController
|
||||
protected function customizeQuery(string $action, Request $request, $query): void {}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $formClass
|
||||
*/
|
||||
protected function deleteAction(string $action, Request $request, $id, $formClass = null): Response
|
||||
@@ -255,13 +244,7 @@ class CRUDController extends AbstractController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
@@ -300,7 +283,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', ['id' => $entity->getId()]);
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -321,8 +304,6 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Duplicate an entity.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function duplicateEntity(string $action, Request $request)
|
||||
{
|
||||
@@ -419,7 +400,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', ['id' => $entity->getId()]);
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -475,20 +456,15 @@ class CRUDController extends AbstractController
|
||||
* The parameters may be personnalized using `generateTemplateParameter`.
|
||||
*
|
||||
* @param class-string $formClass
|
||||
*
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
*/
|
||||
protected function formEditAction(string $action, Request $request, mixed $id, ?string $formClass = null, array $formOptions = []): Response
|
||||
protected function formEditAction(string $action, Request $request, mixed $id, string $formClass = null, array $formOptions = []): Response
|
||||
{
|
||||
$entity = $this->getEntity($action, $id, $request);
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
@@ -523,7 +499,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_index');
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index');
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -574,7 +550,6 @@ class CRUDController extends AbstractController
|
||||
/**
|
||||
* Customize template parameters.
|
||||
*
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function generateTemplateParameter(
|
||||
@@ -588,8 +563,6 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Include services.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getActionConfig(string $action)
|
||||
{
|
||||
@@ -623,8 +596,6 @@ class CRUDController extends AbstractController
|
||||
* get the instance of the entity with the given id.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function getEntity(mixed $action, $id, Request $request): ?object
|
||||
{
|
||||
@@ -679,15 +650,13 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Get the result of the query.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getQueryResult(
|
||||
string $action,
|
||||
Request $request,
|
||||
int $totalItems,
|
||||
PaginatorInterface $paginator,
|
||||
?FilterOrderHelper $filterOrder = null
|
||||
FilterOrderHelper $filterOrder = null
|
||||
) {
|
||||
$query = $this->queryEntities($action, $request, $paginator, $filterOrder);
|
||||
|
||||
@@ -697,7 +666,7 @@ class CRUDController extends AbstractController
|
||||
/**
|
||||
* @return \Chill\MainBundle\Entity\Center[]
|
||||
*/
|
||||
protected function getReachableCenters(string $role, ?Scope $scope = null)
|
||||
protected function getReachableCenters(string $role, Scope $scope = null)
|
||||
{
|
||||
return $this->getAuthorizationHelper()
|
||||
->getReachableCenters($this->getUser(), $role, $scope);
|
||||
@@ -712,7 +681,7 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function getRoleFor($action)
|
||||
{
|
||||
if (array_key_exists('role', $this->getActionConfig($action))) {
|
||||
if (\array_key_exists('role', $this->getActionConfig($action))) {
|
||||
return $this->getActionConfig($action)['role'];
|
||||
}
|
||||
|
||||
@@ -727,11 +696,11 @@ class CRUDController extends AbstractController
|
||||
* and view.
|
||||
*
|
||||
* @param string $action
|
||||
* @param mixed $entity the entity for the current request, or an array of entities
|
||||
*
|
||||
* @throws LogicException if no template are available
|
||||
* @param mixed $entity the entity for the current request, or an array of entities
|
||||
*
|
||||
* @return string the path to the template
|
||||
*
|
||||
* @throws \LogicException if no template are available
|
||||
*/
|
||||
protected function getTemplateFor($action, mixed $entity, Request $request)
|
||||
{
|
||||
@@ -745,9 +714,7 @@ class CRUDController extends AbstractController
|
||||
'index' => '@ChillMain/CRUD/index.html.twig',
|
||||
'view' => '@ChillMain/CRUD/view.html.twig',
|
||||
'delete' => '@ChillMain/CRUD/delete.html.twig',
|
||||
default => throw new LogicException("the view for action {$action} is not "
|
||||
. 'defined. You should override ' . __METHOD__ . ' to add this '
|
||||
. 'action'),
|
||||
default => throw new \LogicException("the view for action {$action} is not ".'defined. You should override '.__METHOD__.' to add this action'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -756,10 +723,6 @@ class CRUDController extends AbstractController
|
||||
return $this->container->get('translator');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function hasCustomTemplate($action, $entity, Request $request): bool
|
||||
{
|
||||
return !empty($this->getActionConfig($action)['template']);
|
||||
@@ -869,9 +832,9 @@ class CRUDController extends AbstractController
|
||||
$next = $request->request->get('submit', 'save-and-close');
|
||||
|
||||
return match ($next) {
|
||||
'save-and-close' => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_index'),
|
||||
'save-and-new' => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_new', $request->query->all()),
|
||||
default => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', [
|
||||
'save-and-close' => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index'),
|
||||
'save-and-new' => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_new', $request->query->all()),
|
||||
default => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', [
|
||||
'id' => $entity->getId(),
|
||||
]),
|
||||
};
|
||||
@@ -879,27 +842,16 @@ class CRUDController extends AbstractController
|
||||
|
||||
protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostCheckACL($action, Request $request, $entity): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostFetchEntity($action, Request $request, $entity): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
@@ -912,21 +864,12 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function onPostIndexFetchQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $entities) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
protected function onPreDelete(string $action, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
protected function onPreIndex(string $action, Request $request) {}
|
||||
@@ -936,14 +879,8 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function onPreIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
@@ -969,7 +906,7 @@ class CRUDController extends AbstractController
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
protected function queryEntities(string $action, Request $request, PaginatorInterface $paginator, ?FilterOrderHelper $filterOrder = null)
|
||||
protected function queryEntities(string $action, Request $request, PaginatorInterface $paginator, FilterOrderHelper $filterOrder = null)
|
||||
{
|
||||
$query = $this->buildQueryEntities($action, $request)
|
||||
->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())
|
||||
@@ -979,9 +916,6 @@ class CRUDController extends AbstractController
|
||||
return $this->orderQuery($action, $query, $request, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
$this->getDoctrine()
|
||||
@@ -1021,13 +955,7 @@ class CRUDController extends AbstractController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
|
Reference in New Issue
Block a user