Refactor code to directly use Doctrine's ManagerRegistry

Replaced most of the invocations of getDoctrine()->getManager() with ManagerRegistry->getManager(), and added ManagerRegistry injection to controllers where needed. This is part of an ongoing effort to improve code clarity, and avoid unnecessary method chaining in various parts of the codebase.
This commit is contained in:
2023-12-16 19:09:34 +01:00
parent 655dc02538
commit 5703fd0046
54 changed files with 281 additions and 228 deletions

View File

@@ -19,7 +19,7 @@ use Symfony\Component\Routing\Annotation\Route;
class AbsenceController extends AbstractController
{
public function __construct(private readonly ChillSecurity $security) {}
public function __construct(private readonly ChillSecurity $security, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* @Route(
@@ -36,7 +36,7 @@ class AbsenceController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->flush();
return $this->redirectToRoute('chill_main_user_absence_index');
@@ -60,7 +60,7 @@ class AbsenceController extends AbstractController
$user = $this->security->getUser();
$user->setAbsenceStart(null);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->flush();
return $this->redirectToRoute('chill_main_user_absence_index');

View File

@@ -20,6 +20,9 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
class AddressApiController extends ApiController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
/**
* Duplicate an existing address.
*
@@ -31,7 +34,7 @@ class AddressApiController extends ApiController
$this->denyAccessUnlessGranted('ROLE_USER');
$new = Address::createFromAddress($address);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($new);
$em->flush();

View File

@@ -40,7 +40,7 @@ use function in_array;
*/
class NotificationController extends AbstractController
{
public function __construct(private readonly EntityManagerInterface $em, private readonly LoggerInterface $chillLogger, private readonly LoggerInterface $logger, private readonly ChillSecurity $security, private readonly NotificationRepository $notificationRepository, private readonly NotificationHandlerManager $notificationHandlerManager, private readonly PaginatorFactory $paginatorFactory, private readonly TranslatorInterface $translator, private readonly UserRepository $userRepository) {}
public function __construct(private readonly EntityManagerInterface $em, private readonly LoggerInterface $chillLogger, private readonly LoggerInterface $logger, private readonly ChillSecurity $security, private readonly NotificationRepository $notificationRepository, private readonly NotificationHandlerManager $notificationHandlerManager, private readonly PaginatorFactory $paginatorFactory, private readonly TranslatorInterface $translator, private readonly UserRepository $userRepository, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* @Route("/create", name="chill_main_notification_create")
@@ -159,7 +159,7 @@ class NotificationController extends AbstractController
$notification->addAddressee($this->security->getUser());
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$logMsg = '[Notification] a user is granted access to notification trough an access key';
$context = [

View File

@@ -40,7 +40,7 @@ final class PasswordController extends AbstractController
/**
* PasswordController constructor.
*/
public function __construct(private readonly LoggerInterface $chillLogger, private readonly UserPasswordEncoderInterface $passwordEncoder, private readonly RecoverPasswordHelper $recoverPasswordHelper, private readonly TokenManager $tokenManager, private readonly TranslatorInterface $translator, private readonly EventDispatcherInterface $eventDispatcher, private readonly ChillSecurity $security) {}
public function __construct(private readonly LoggerInterface $chillLogger, private readonly UserPasswordEncoderInterface $passwordEncoder, private readonly RecoverPasswordHelper $recoverPasswordHelper, private readonly TokenManager $tokenManager, private readonly TranslatorInterface $translator, private readonly EventDispatcherInterface $eventDispatcher, private readonly ChillSecurity $security, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* @return Response
@@ -68,7 +68,7 @@ final class PasswordController extends AbstractController
$hash = $query->getAlnum(TokenManager::HASH);
$token = $query->getAlnum(TokenManager::TOKEN);
$timestamp = $query->getAlnum(TokenManager::TIMESTAMP);
$user = $this->getDoctrine()->getRepository(User::class)
$user = $this->managerRegistry->getRepository(User::class)
->findOneByUsernameCanonical($username);
if (null === $user) {
@@ -107,7 +107,7 @@ final class PasswordController extends AbstractController
]
);
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
return $this->redirectToRoute('password_request_recover_changed');
}
@@ -137,7 +137,7 @@ final class PasswordController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
/** @var \Doctrine\ORM\QueryBuilder $qb */
$qb = $this->getDoctrine()->getManager()
$qb = $this->managerRegistry->getManager()
->createQueryBuilder();
$qb->select('u')
->from(User::class, 'u')
@@ -236,7 +236,7 @@ final class PasswordController extends AbstractController
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->flush();
$this->addFlash('success', $this->translator->trans('Password successfully updated!'));
@@ -262,7 +262,7 @@ final class PasswordController extends AbstractController
'constraints' => [
new Callback([
'callback' => function ($pattern, ExecutionContextInterface $context, $payload) {
$qb = $this->getDoctrine()->getManager()
$qb = $this->managerRegistry->getManager()
->createQueryBuilder();
$qb->select('COUNT(u)')
->from(User::class, 'u')

View File

@@ -29,7 +29,7 @@ class PostalCodeController extends AbstractController
*/
protected $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
public function __construct(TranslatableStringHelper $translatableStringHelper, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
$this->translatableStringHelper = $translatableStringHelper;
}
@@ -49,7 +49,7 @@ class PostalCodeController extends AbstractController
return new JsonResponse(['results' => [], 'pagination' => ['more' => false]]);
}
$query = $this->getDoctrine()->getManager()
$query = $this->managerRegistry->getManager()
->createQuery(
sprintf(
'SELECT p.id AS id, p.name AS name, p.code AS code, '

View File

@@ -26,7 +26,7 @@ use Symfony\Component\HttpFoundation\Response;
class ScopeController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager
private readonly EntityManagerInterface $entityManager, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
) {}
/**
@@ -41,7 +41,7 @@ class ScopeController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($scope);
$em->flush();
@@ -83,7 +83,7 @@ class ScopeController extends AbstractController
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entities = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->findAll();
@@ -113,7 +113,7 @@ class ScopeController extends AbstractController
*/
public function showAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$scope = $em->getRepository(\Chill\MainBundle\Entity\Scope::class)->find($id);

View File

@@ -46,7 +46,8 @@ class UserController extends CRUDController
private readonly UserRepository $userRepository,
protected ParameterBagInterface $parameterBag,
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security
private readonly ChillSecurity $security,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
) {}
/**
@@ -55,7 +56,7 @@ class UserController extends CRUDController
*/
public function addLinkGroupCenterAction(Request $request, mixed $uid): Response
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$user = $em->getRepository(\Chill\MainBundle\Entity\User::class)->find($uid);
@@ -107,7 +108,7 @@ class UserController extends CRUDController
*/
public function deleteLinkGroupCenterAction(mixed $uid, mixed $gcid, Request $request): RedirectResponse
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$user = $em->getRepository(\Chill\MainBundle\Entity\User::class)->find($uid);
@@ -165,7 +166,7 @@ class UserController extends CRUDController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
@@ -216,7 +217,7 @@ class UserController extends CRUDController
$user->setCurrentLocation($currentLocation);
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('Current location successfully updated'));
return $this->redirect(
@@ -252,7 +253,7 @@ class UserController extends CRUDController
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('Password successfully updated!'));
return $this->redirect(
@@ -431,7 +432,7 @@ class UserController extends CRUDController
private function getPersistedGroupCenter(GroupCenter $groupCenter)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$groupCenterManaged = $em->getRepository(\Chill\MainBundle\Entity\GroupCenter::class)
->findOneBy([

View File

@@ -27,6 +27,7 @@ final class UserProfileController extends AbstractController
public function __construct(
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
) {}
/**
@@ -49,7 +50,7 @@ final class UserProfileController extends AbstractController
$user->setPhonenumber($phonenumber);
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('user.profile.Phonenumber successfully updated!'));
return $this->redirectToRoute('chill_main_user_profile');

View File

@@ -38,7 +38,7 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class WorkflowController extends AbstractController
{
public function __construct(private readonly EntityWorkflowManager $entityWorkflowManager, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly ValidatorInterface $validator, private readonly PaginatorFactory $paginatorFactory, private readonly Registry $registry, private readonly EntityManagerInterface $entityManager, private readonly TranslatorInterface $translator, private readonly ChillSecurity $security) {}
public function __construct(private readonly EntityWorkflowManager $entityWorkflowManager, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly ValidatorInterface $validator, private readonly PaginatorFactory $paginatorFactory, private readonly Registry $registry, private readonly EntityManagerInterface $entityManager, private readonly TranslatorInterface $translator, private readonly ChillSecurity $security, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* @Route("/{_locale}/main/workflow/create", name="chill_main_workflow_create")
@@ -79,7 +79,7 @@ class WorkflowController extends AbstractController
$this->denyAccessUnlessGranted(EntityWorkflowVoter::CREATE, $entityWorkflow);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entityWorkflow);
$em->flush();