1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

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
No known key found for this signature in database
GPG key ID: 7168D5DD29F38552
36 changed files with 855 additions and 699 deletions

View file

@ -2,6 +2,7 @@
namespace Wallabag\UserBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserManagerInterface;
@ -9,33 +10,40 @@ use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
use Pagerfanta\Pagerfanta;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Wallabag\UserBundle\Entity\User;
use Wallabag\UserBundle\Form\NewUserType;
use Wallabag\UserBundle\Form\SearchUserType;
use Wallabag\UserBundle\Form\UserType;
use Wallabag\UserBundle\Repository\UserRepository;
/**
* User controller.
*/
class ManageController extends Controller
class ManageController extends AbstractController
{
private EntityManagerInterface $entityManager;
private TranslatorInterface $translator;
public function __construct(EntityManagerInterface $entityManager, TranslatorInterface $translator)
{
$this->entityManager = $entityManager;
$this->translator = $translator;
}
/**
* Creates a new User entity.
*
* @Route("/new", name="user_new", methods={"GET", "POST"})
*/
public function newAction(Request $request)
public function newAction(Request $request, UserManagerInterface $userManager, EventDispatcherInterface $eventDispatcher)
{
$userManager = $this->container->get(UserManagerInterface::class);
$user = $userManager->createUser();
\assert($user instanceof User);
// enable created user by default
@ -49,11 +57,11 @@ class ManageController extends Controller
// dispatch a created event so the associated config will be created
$event = new UserEvent($user, $request);
$this->get(EventDispatcherInterface::class)->dispatch($event, FOSUserEvents::USER_CREATED);
$eventDispatcher->dispatch($event, FOSUserEvents::USER_CREATED);
$this->get(SessionInterface::class)->getFlashBag()->add(
$this->addFlash(
'notice',
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
$this->translator->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
);
return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
@ -70,10 +78,8 @@ class ManageController extends Controller
*
* @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
*/
public function editAction(Request $request, User $user)
public function editAction(Request $request, User $user, UserManagerInterface $userManager, GoogleAuthenticatorInterface $googleAuthenticator)
{
$userManager = $this->container->get(UserManagerInterface::class);
$deleteForm = $this->createDeleteForm($user);
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
@ -87,7 +93,7 @@ class ManageController extends Controller
// handle creation / reset of the OTP secret if checkbox changed from the previous state
if ($this->getParameter('twofactor_auth')) {
if (true === $form->get('googleTwoFactor')->getData() && false === $user->isGoogleAuthenticatorEnabled()) {
$user->setGoogleAuthenticatorSecret($this->get(GoogleAuthenticatorInterface::class)->generateSecret());
$user->setGoogleAuthenticatorSecret($googleAuthenticator->generateSecret());
$user->setEmailTwoFactor(false);
} elseif (false === $form->get('googleTwoFactor')->getData() && true === $user->isGoogleAuthenticatorEnabled()) {
$user->setGoogleAuthenticatorSecret(null);
@ -96,9 +102,9 @@ class ManageController extends Controller
$userManager->updateUser($user);
$this->get(SessionInterface::class)->getFlashBag()->add(
$this->addFlash(
'notice',
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
$this->translator->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
);
return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
@ -123,14 +129,13 @@ class ManageController extends Controller
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->get(SessionInterface::class)->getFlashBag()->add(
$this->addFlash(
'notice',
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
$this->translator->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
);
$em = $this->get('doctrine')->getManager();
$em->remove($user);
$em->flush();
$this->entityManager->remove($user);
$this->entityManager->flush();
}
return $this->redirectToRoute('user_index');
@ -146,10 +151,9 @@ class ManageController extends Controller
*
* @return Response
*/
public function searchFormAction(Request $request, $page = 1)
public function searchFormAction(Request $request, UserRepository $userRepository, $page = 1)
{
$em = $this->get('doctrine')->getManager();
$qb = $em->getRepository(User::class)->createQueryBuilder('u');
$qb = $userRepository->createQueryBuilder('u');
$form = $this->createForm(SearchUserType::class);
$form->handleRequest($request);
@ -157,7 +161,7 @@ class ManageController extends Controller
if ($form->isSubmitted() && $form->isValid()) {
$searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
$qb = $em->getRepository(User::class)->getQueryBuilderForSearch($searchTerm);
$qb = $userRepository->getQueryBuilderForSearch($searchTerm);
}
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);