mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-04 12:29:43 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -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');
|
||||
|
Reference in New Issue
Block a user