mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-04 12:29:43 +00:00
DX: apply rector rules up to php8.0
This commit is contained in:
@@ -32,31 +32,18 @@ class ApiController extends AbstractCRUDController
|
||||
/**
|
||||
* Base method for handling api action.
|
||||
*
|
||||
* @param mixed $id
|
||||
* @param string $_format
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function entityApi(Request $request, $id, ?string $_format = 'json'): Response
|
||||
public function entityApi(Request $request, mixed $id, ?string $_format = 'json'): Response
|
||||
{
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_GET:
|
||||
case Request::METHOD_HEAD:
|
||||
return $this->entityGet('_entity', $request, $id, $_format);
|
||||
|
||||
case Request::METHOD_PUT:
|
||||
case Request::METHOD_PATCH:
|
||||
return $this->entityPut('_entity', $request, $id, $_format);
|
||||
|
||||
case Request::METHOD_POST:
|
||||
return $this->entityPostAction('_entity', $request, $id);
|
||||
|
||||
case Request::METHOD_DELETE:
|
||||
return $this->entityDelete('_entity', $request, $id, $_format);
|
||||
|
||||
default:
|
||||
throw new BadRequestHttpException('This method is not implemented');
|
||||
}
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_GET, Request::METHOD_HEAD => $this->entityGet('_entity', $request, $id, $_format),
|
||||
Request::METHOD_PUT, Request::METHOD_PATCH => $this->entityPut('_entity', $request, $id, $_format),
|
||||
Request::METHOD_POST => $this->entityPostAction('_entity', $request, $id),
|
||||
Request::METHOD_DELETE => $this->entityDelete('_entity', $request, $id, $_format),
|
||||
default => throw new BadRequestHttpException('This method is not implemented'),
|
||||
};
|
||||
}
|
||||
|
||||
public function entityDelete($action, Request $request, $id, string $_format): Response
|
||||
@@ -115,13 +102,10 @@ class ApiController extends AbstractCRUDController
|
||||
|
||||
public function entityPost(Request $request, $_format): Response
|
||||
{
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_POST:
|
||||
return $this->entityPostAction('_entity', $request, $_format);
|
||||
|
||||
default:
|
||||
throw new BadRequestHttpException('This method is not implemented');
|
||||
}
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_POST => $this->entityPostAction('_entity', $request, $_format),
|
||||
default => throw new BadRequestHttpException('This method is not implemented'),
|
||||
};
|
||||
}
|
||||
|
||||
public function entityPut($action, Request $request, $id, string $_format): Response
|
||||
@@ -199,14 +183,10 @@ class ApiController extends AbstractCRUDController
|
||||
*/
|
||||
public function indexApi(Request $request, string $_format)
|
||||
{
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_GET:
|
||||
case REQUEST::METHOD_HEAD:
|
||||
return $this->indexApiAction('_index', $request, $_format);
|
||||
|
||||
default:
|
||||
throw $this->createNotFoundException('This method is not supported');
|
||||
}
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_GET, REQUEST::METHOD_HEAD => $this->indexApiAction('_index', $request, $_format),
|
||||
default => throw $this->createNotFoundException('This method is not supported'),
|
||||
};
|
||||
}
|
||||
|
||||
public function onBeforeSerialize(string $action, Request $request, $_format, $entity): ?Response
|
||||
@@ -242,7 +222,7 @@ class ApiController extends AbstractCRUDController
|
||||
* @throw BadRequestException if unable to deserialize the posted data
|
||||
* @throw BadRequestException if the method is not POST or DELETE
|
||||
*/
|
||||
protected function addRemoveSomething(string $action, $id, Request $request, string $_format, string $property, string $postedDataType, array $postedDataContext = [], bool $forcePersist = false): Response
|
||||
protected function addRemoveSomething(string $action, mixed $id, Request $request, string $_format, string $property, string $postedDataType, array $postedDataContext = [], bool $forcePersist = false): Response
|
||||
{
|
||||
$entity = $this->getEntity($action, $id, $request);
|
||||
|
||||
@@ -277,23 +257,11 @@ class ApiController extends AbstractCRUDController
|
||||
'data: %s', $e->getMessage()), $e, 0);
|
||||
}
|
||||
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_DELETE:
|
||||
// oups... how to use property accessor to remove element ?
|
||||
/* @phpstan-ignore-next-line as we do not find a simpler way to do this */
|
||||
$entity->{'remove' . ucfirst($property)}($postedData);
|
||||
|
||||
break;
|
||||
|
||||
case Request::METHOD_POST:
|
||||
/* @phpstan-ignore-next-line as we do not find a simpler way to do this */
|
||||
$entity->{'add' . ucfirst($property)}($postedData);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new BadRequestHttpException('this method is not supported');
|
||||
}
|
||||
match ($request->getMethod()) {
|
||||
Request::METHOD_DELETE => $entity->{'remove' . ucfirst($property)}($postedData),
|
||||
Request::METHOD_POST => $entity->{'add' . ucfirst($property)}($postedData),
|
||||
default => throw new BadRequestHttpException('this method is not supported'),
|
||||
};
|
||||
|
||||
$errors = $this->validate($action, $request, $_format, $entity, [$postedData]);
|
||||
|
||||
@@ -319,21 +287,16 @@ class ApiController extends AbstractCRUDController
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_DELETE:
|
||||
return $this->json('', Response::HTTP_OK);
|
||||
|
||||
case Request::METHOD_POST:
|
||||
return $this->json(
|
||||
$postedData,
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity, [$postedData])
|
||||
);
|
||||
}
|
||||
|
||||
throw new Exception('Unable to handle such request method.');
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_DELETE => $this->json('', Response::HTTP_OK),
|
||||
Request::METHOD_POST => $this->json(
|
||||
$postedData,
|
||||
Response::HTTP_OK,
|
||||
[],
|
||||
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity, [$postedData])
|
||||
),
|
||||
default => throw new Exception('Unable to handle such request method.'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -376,7 +339,7 @@ class ApiController extends AbstractCRUDController
|
||||
* @param mixed $id
|
||||
* @param mixed $_format
|
||||
*/
|
||||
protected function entityGet(string $action, Request $request, $id, $_format = 'html'): Response
|
||||
protected function entityGet(string $action, Request $request, mixed $id, mixed $_format = 'html'): Response
|
||||
{
|
||||
$entity = $this->getEntity($action, $id, $request);
|
||||
|
||||
@@ -474,18 +437,11 @@ class ApiController extends AbstractCRUDController
|
||||
|
||||
protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
|
||||
{
|
||||
switch ($request->getMethod()) {
|
||||
case Request::METHOD_GET:
|
||||
return ['groups' => ['read']];
|
||||
|
||||
case Request::METHOD_PUT:
|
||||
case Request::METHOD_PATCH:
|
||||
case Request::METHOD_POST:
|
||||
return ['groups' => ['write']];
|
||||
|
||||
default:
|
||||
throw new LogicException('get context for serialization is not implemented for this method');
|
||||
}
|
||||
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'),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -496,7 +452,7 @@ class ApiController extends AbstractCRUDController
|
||||
*
|
||||
* @param mixed $entity
|
||||
*/
|
||||
protected function getContextForSerializationPostAlter(string $action, Request $request, string $_format, $entity, array $more = []): array
|
||||
protected function getContextForSerializationPostAlter(string $action, Request $request, string $_format, mixed $entity, array $more = []): array
|
||||
{
|
||||
return ['groups' => ['read']];
|
||||
}
|
||||
@@ -533,7 +489,7 @@ class ApiController extends AbstractCRUDController
|
||||
* @param string $action
|
||||
* @param mixed $_format
|
||||
*/
|
||||
protected function indexApiAction($action, Request $request, $_format)
|
||||
protected function indexApiAction($action, Request $request, mixed $_format)
|
||||
{
|
||||
$this->onPreIndex($action, $request, $_format);
|
||||
|
||||
@@ -611,10 +567,8 @@ class ApiController extends AbstractCRUDController
|
||||
|
||||
/**
|
||||
* Serialize collections.
|
||||
*
|
||||
* @param mixed $entities
|
||||
*/
|
||||
protected function serializeCollection(string $action, Request $request, string $_format, PaginatorInterface $paginator, $entities): Response
|
||||
protected function serializeCollection(string $action, Request $request, string $_format, PaginatorInterface $paginator, mixed $entities): Response
|
||||
{
|
||||
$model = new Collection($entities, $paginator);
|
||||
|
||||
|
Reference in New Issue
Block a user