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

@@ -33,7 +33,8 @@ class ParticipationController extends AbstractController
/**
* ParticipationController constructor.
*/
public function __construct(private readonly LoggerInterface $logger, private readonly TranslatorInterface $translator) {}
public function __construct(
private readonly LoggerInterface $logger, private readonly TranslatorInterface $translator, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* @\Symfony\Component\Routing\Annotation\Route(path="/{_locale}/event/participation/create", name="chill_event_participation_create")
@@ -169,7 +170,7 @@ class ParticipationController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$data = $form->getData();
foreach ($data['participations'] as $participation) {
@@ -207,7 +208,7 @@ class ParticipationController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($participation);
$em->flush();
@@ -238,7 +239,7 @@ class ParticipationController extends AbstractController
*/
public function deleteAction($participation_id, Request $request): Response|\Symfony\Component\HttpFoundation\RedirectResponse
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$participation = $em->getRepository(\Chill\EventBundle\Entity\Participation::class)->findOneBy([
'id' => $participation_id,
]);
@@ -288,7 +289,7 @@ class ParticipationController extends AbstractController
public function editAction(int $participation_id): Response
{
/** @var Participation $participation */
$participation = $this->getDoctrine()->getManager()
$participation = $this->managerRegistry->getManager()
->getRepository(Participation::class)
->find($participation_id);
@@ -319,7 +320,7 @@ class ParticipationController extends AbstractController
*/
public function editMultipleAction($event_id): Response|\Symfony\Component\HttpFoundation\RedirectResponse
{
$event = $this->getDoctrine()->getRepository(\Chill\EventBundle\Entity\Event::class)
$event = $this->managerRegistry->getRepository(\Chill\EventBundle\Entity\Event::class)
->find($event_id);
if (null === $event) {
@@ -412,7 +413,7 @@ class ParticipationController extends AbstractController
public function updateAction(int $participation_id, Request $request): Response
{
/** @var Participation $participation */
$participation = $this->getDoctrine()->getManager()
$participation = $this->managerRegistry->getManager()
->getRepository(Participation::class)
->find($participation_id);
@@ -431,7 +432,7 @@ class ParticipationController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->flush();
@@ -456,7 +457,7 @@ class ParticipationController extends AbstractController
public function updateMultipleAction(mixed $event_id, Request $request)
{
/** @var \Chill\EventBundle\Entity\Event $event */
$event = $this->getDoctrine()->getRepository(\Chill\EventBundle\Entity\Event::class)
$event = $this->managerRegistry->getRepository(\Chill\EventBundle\Entity\Event::class)
->find($event_id);
if (null === $event) {
@@ -479,7 +480,7 @@ class ParticipationController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('The participations '
.'have been successfully updated.'));
@@ -547,7 +548,7 @@ class ParticipationController extends AbstractController
Participation $participation,
bool $multiple = false
): array|\Chill\EventBundle\Entity\Participation {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
if ($em->contains($participation)) {
throw new \LogicException('The participation object should not be managed by the object manager using the method '.__METHOD__);