Move to controller as a service

Mostly using autowiring to inject deps.
The only tricky part was for import because all producer use the same class and have a different alias. So we must write them down in the service definition, autowiring doesn't work in that case.

Usually:
- if a controller has a constructor, it means injected services are at least re-used once in actions
- otherwise, service are injected per action
This commit is contained in:
Jeremy Benoist
2022-12-19 10:37:22 +01:00
parent 39f603e015
commit 6aca334d53
36 changed files with 855 additions and 699 deletions

View File

@ -2,6 +2,7 @@
namespace Wallabag\ApiBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
@ -12,10 +13,26 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Wallabag\UserBundle\Entity\User;
class WallabagRestController extends AbstractFOSRestController
{
protected EntityManagerInterface $entityManager;
protected SerializerInterface $serializer;
protected AuthorizationCheckerInterface $authorizationChecker;
protected TokenStorageInterface $tokenStorage;
protected TranslatorInterface $translator;
public function __construct(EntityManagerInterface $entityManager, SerializerInterface $serializer, AuthorizationCheckerInterface $authorizationChecker, TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->entityManager = $entityManager;
$this->serializer = $serializer;
$this->authorizationChecker = $authorizationChecker;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}
/**
* Retrieve version number.
*
@ -36,8 +53,8 @@ class WallabagRestController extends AbstractFOSRestController
*/
public function getVersionAction()
{
$version = $this->container->getParameter('wallabag_core.version');
$json = $this->get(SerializerInterface::class)->serialize($version, 'json');
$version = $this->getParameter('wallabag_core.version');
$json = $this->serializer->serialize($version, 'json');
return (new JsonResponse())->setJson($json);
}
@ -62,16 +79,16 @@ class WallabagRestController extends AbstractFOSRestController
{
$info = [
'appname' => 'wallabag',
'version' => $this->container->getParameter('wallabag_core.version'),
'allowed_registration' => $this->container->getParameter('fosuser_registration'),
'version' => $this->getParameter('wallabag_core.version'),
'allowed_registration' => $this->getParameter('fosuser_registration'),
];
return (new JsonResponse())->setJson($this->get(SerializerInterface::class)->serialize($info, 'json'));
return (new JsonResponse())->setJson($this->serializer->serialize($info, 'json'));
}
protected function validateAuthentication()
{
if (false === $this->get(AuthorizationCheckerInterface::class)->isGranted('IS_AUTHENTICATED_FULLY')) {
if (false === $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
throw new AccessDeniedException();
}
}
@ -84,8 +101,9 @@ class WallabagRestController extends AbstractFOSRestController
*/
protected function validateUserAccess($requestUserId)
{
$user = $this->get(TokenStorageInterface::class)->getToken()->getUser();
$user = $this->tokenStorage->getToken()->getUser();
\assert($user instanceof User);
if ($requestUserId !== $user->getId()) {
throw $this->createAccessDeniedException('Access forbidden. Entry user id: ' . $requestUserId . ', logged user id: ' . $user->getId());
}
@ -104,7 +122,7 @@ class WallabagRestController extends AbstractFOSRestController
$context = new SerializationContext();
$context->setSerializeNull(true);
$json = $this->get(SerializerInterface::class)->serialize($data, 'json', $context);
$json = $this->serializer->serialize($data, 'json', $context);
return (new JsonResponse())->setJson($json);
}