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

@@ -59,6 +59,7 @@ final class SingleTaskController extends AbstractController
private readonly SingleTaskStateRepository $singleTaskStateRepository,
private readonly SingleTaskRepository $singleTaskRepository,
private readonly Security $security,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
) {}
/**
@@ -70,7 +71,7 @@ final class SingleTaskController extends AbstractController
public function deleteAction(Request $request, mixed $id)
{
$course = null;
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$task = $em->getRepository(SingleTask::class)->find($id);
if (null === $task) {
@@ -84,7 +85,7 @@ final class SingleTaskController extends AbstractController
return new Response('You must provide a person_id', Response::HTTP_BAD_REQUEST);
}
$person = $this->getDoctrine()->getManager()
$person = $this->managerRegistry->getManager()
->getRepository(Person::class)
->find($personId);
@@ -98,7 +99,7 @@ final class SingleTaskController extends AbstractController
return new Response('You must provide a course_id', Response::HTTP_BAD_REQUEST);
}
$course = $this->getDoctrine()->getManager()
$course = $this->managerRegistry->getManager()
->getRepository(AccompanyingPeriod::class)
->find($courseId);
@@ -127,7 +128,7 @@ final class SingleTaskController extends AbstractController
// 'scope_id' => $task->getScope()->getId(),
]);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->remove($task);
$em->flush();
@@ -185,7 +186,7 @@ final class SingleTaskController extends AbstractController
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($task);
$em->flush();
@@ -515,7 +516,7 @@ final class SingleTaskController extends AbstractController
switch ($entityType) {
case 'person':
$person = $this->getDoctrine()->getManager()
$person = $this->managerRegistry->getManager()
->getRepository(Person::class)
->find($entityId);
@@ -529,7 +530,7 @@ final class SingleTaskController extends AbstractController
break;
case 'course':
$course = $this->getDoctrine()->getManager()
$course = $this->managerRegistry->getManager()
->getRepository(AccompanyingPeriod::class)
->find($entityId);
@@ -555,7 +556,7 @@ final class SingleTaskController extends AbstractController
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($task);
$this->eventDispatcher->dispatch(new TaskEvent($task), TaskEvent::PERSIST);