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

@@ -77,7 +77,8 @@ class EventController extends AbstractController
AuthorizationHelper $authorizationHelper,
FormFactoryInterface $formFactoryInterface,
TranslatorInterface $translator,
PaginatorFactory $paginator
PaginatorFactory $paginator,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
) {
$this->eventDispatcher = $eventDispatcher;
$this->authorizationHelper = $authorizationHelper;
@@ -91,7 +92,7 @@ class EventController extends AbstractController
*/
public function deleteAction($event_id, Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$event = $em->getRepository(\Chill\EventBundle\Entity\Event::class)->findOneBy([
'id' => $event_id,
]);
@@ -144,7 +145,7 @@ class EventController extends AbstractController
*/
public function editAction($event_id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Event::class)->find($event_id);
@@ -171,7 +172,7 @@ class EventController extends AbstractController
*/
public function listByPersonAction($person_id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$person = $em->getRepository(\Chill\PersonBundle\Entity\Person::class)->find($person_id);
@@ -235,7 +236,7 @@ class EventController extends AbstractController
{
if (null === $center) {
$center_id = $request->query->get('center_id');
$center = $this->getDoctrine()->getRepository(Center::class)->find($center_id);
$center = $this->managerRegistry->getRepository(Center::class)->find($center_id);
}
$entity = new Event();
@@ -245,7 +246,7 @@ class EventController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -350,7 +351,7 @@ class EventController extends AbstractController
*/
public function updateAction(Request $request, $event_id): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Event::class)->find($event_id);

View File

@@ -22,6 +22,9 @@ use Symfony\Component\HttpFoundation\Request;
*/
class EventTypeController extends AbstractController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
/**
* Creates a new EventType entity.
*
@@ -34,7 +37,7 @@ class EventTypeController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -58,7 +61,7 @@ class EventTypeController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\EventType::class)->find($id);
if (!$entity) {
@@ -79,7 +82,7 @@ class EventTypeController extends AbstractController
*/
public function editAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\EventType::class)->find($id);
@@ -104,7 +107,7 @@ class EventTypeController extends AbstractController
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entities = $em->getRepository(\Chill\EventBundle\Entity\EventType::class)->findAll();
@@ -136,7 +139,7 @@ class EventTypeController extends AbstractController
*/
public function showAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\EventType::class)->find($id);
@@ -159,7 +162,7 @@ class EventTypeController extends AbstractController
*/
public function updateAction(Request $request, mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\EventType::class)->find($id);

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__);

View File

@@ -22,6 +22,9 @@ use Symfony\Component\HttpFoundation\Request;
*/
class RoleController extends AbstractController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
/**
* Creates a new Role entity.
*
@@ -34,7 +37,7 @@ class RoleController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -58,7 +61,7 @@ class RoleController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Role::class)->find($id);
if (!$entity) {
@@ -79,7 +82,7 @@ class RoleController extends AbstractController
*/
public function editAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Role::class)->find($id);
@@ -104,7 +107,7 @@ class RoleController extends AbstractController
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entities = $em->getRepository(\Chill\EventBundle\Entity\Role::class)->findAll();
@@ -136,7 +139,7 @@ class RoleController extends AbstractController
*/
public function showAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Role::class)->find($id);
@@ -159,7 +162,7 @@ class RoleController extends AbstractController
*/
public function updateAction(Request $request, mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Role::class)->find($id);

View File

@@ -22,6 +22,9 @@ use Symfony\Component\HttpFoundation\Request;
*/
class StatusController extends AbstractController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
/**
* Creates a new Status entity.
*
@@ -34,7 +37,7 @@ class StatusController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -58,7 +61,7 @@ class StatusController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Status::class)->find($id);
if (!$entity) {
@@ -79,7 +82,7 @@ class StatusController extends AbstractController
*/
public function editAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Status::class)->find($id);
@@ -104,7 +107,7 @@ class StatusController extends AbstractController
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entities = $em->getRepository(\Chill\EventBundle\Entity\Status::class)->findAll();
@@ -136,7 +139,7 @@ class StatusController extends AbstractController
*/
public function showAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Status::class)->find($id);
@@ -159,7 +162,7 @@ class StatusController extends AbstractController
*/
public function updateAction(Request $request, mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\EventBundle\Entity\Status::class)->find($id);

View File

@@ -7,10 +7,12 @@ services:
$formFactoryInterface: '@Symfony\Component\Form\FormFactoryInterface'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
$paginator: '@chill_main.paginator_factory'
$managerRegistry: '@Doctrine\Persistence\ManagerRegistry'
public: true
tags: ['controller.service_arguments']
Chill\EventBundle\Controller\ParticipationController:
arguments:
$logger: '@Psr\Log\LoggerInterface'
$managerRegistry: '@Doctrine\Persistence\ManagerRegistry'
tags: ['controller.service_arguments']