diff --git a/src/Controller/CapsuleEditorController.php b/src/Controller/CapsuleEditorController.php index 932deed6aecf72e9618b8b6bc141174421d6981e..3fdc4dc8414f3f89b911e674148384507f7b1757 100644 --- a/src/Controller/CapsuleEditorController.php +++ b/src/Controller/CapsuleEditorController.php @@ -8,6 +8,9 @@ use App\Entity\CapsulePendingEditor; use App\Entity\User; use App\Form\CapsuleEditorsFormType; use App\Repository\CapsulePendingEditorRepository; +use App\Repository\CapsuleRepository; +use App\Repository\UserRepository; +use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -20,11 +23,19 @@ class CapsuleEditorController extends AbstractController { private TranslatorInterface $translator; private MailerInterface $mailer; - - public function __construct(MailerInterface $mailer, TranslatorInterface $translator) - { + private CapsulePendingEditorRepository $capsule_pending_editor_repository; + private EntityManagerInterface $entity_manager; + + public function __construct( + MailerInterface $mailer, + TranslatorInterface $translator, + CapsulePendingEditorRepository $capsule_pending_editor_repository, + EntityManagerInterface $entity_manager + ) { $this->mailer = $mailer; $this->translator = $translator; + $this->capsule_pending_editor_repository = $capsule_pending_editor_repository; + $this->entity_manager = $entity_manager; } /** @@ -33,21 +44,15 @@ class CapsuleEditorController extends AbstractController public function editCapsuleEditors( Request $request, int $capsule_id, - CapsulePendingEditorRepository $capsule_pending_editor_repository + CapsuleRepository $capsule_repository, + UserRepository $user_repository ): Response { $current_user = $this->getUser(); - if (! $current_user instanceof User) { return $this->redirectToRoute('app_logout'); } - $form = $this->createForm(CapsuleEditorsFormType::class); - $form->handleRequest($request); - - $entity_manager = $this->getDoctrine()->getManager(); - $capsule_repository = $entity_manager->getRepository(Capsule::class); $capsule = $capsule_repository->find($capsule_id); - if (! $capsule) { throw $this->createNotFoundException( 'No capsule found for id ' . $capsule_id @@ -55,7 +60,6 @@ class CapsuleEditorController extends AbstractController } $current_capsule_editors_users = $capsule->getEditors()->toArray(); - if (! in_array($current_user, $current_capsule_editors_users)) { $this->addFlash( 'warning', @@ -70,118 +74,134 @@ class CapsuleEditorController extends AbstractController return $this->redirectToRoute('capsule_list'); } - $pending_editors = $capsule_pending_editor_repository->findBy(['capsule_id' => $capsule_id]); - $pending_editors_emails = $capsule_pending_editor_repository->getPendingEditorsEmails($capsule_id); + $form = $this->createForm(CapsuleEditorsFormType::class); + $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $editor_email = $form->get('email')->getData(); - - $user_associated_with_email_address = $entity_manager - ->getRepository(User::class) + $user_associated_with_email_address = $user_repository ->findOneBy(['email' => $editor_email]); if (! $user_associated_with_email_address instanceof User) { - if (in_array($editor_email, $pending_editors_emails)) { - $this->addFlash( - 'warning', - $this->translator->trans( - 'editors.add.pending_editor.already_added', - [ - 'user_email' => $editor_email - ] - ) - ); - - return $this->redirectToRoute('edit_capsule_editors', [ - 'capsule_id' => $capsule_id - ]); - } - - $pending_editor = new CapsulePendingEditor(); - $pending_editor->setCapsuleId($capsule_id); - $pending_editor->setEmail($editor_email); - $entity_manager->persist($pending_editor); - $entity_manager->flush(); - - $email = (new TemplatedEmail()) - ->to($editor_email) - ->subject($this->translator->trans('editors.add.pending_editor.email.title')) - ->htmlTemplate('capsule/editors/email_pending_editor.html.twig') - ->context([ - 'user' => $current_user, - 'capsule' => $capsule - ]); - - $this->mailer->send($email); - - $this->addFlash( - 'success', - $this->translator->trans( - 'editors.add.pending_editor.success', - [ - 'user_email' => $editor_email - ] - ) + $this->addPendingEditor($editor_email, $capsule, $current_user); + } else { + $this->addEditor( + $editor_email, + $capsule, + $current_user, + $user_associated_with_email_address, + $current_capsule_editors_users ); - - return $this->redirectToRoute('edit_capsule_editors', [ - 'capsule_id' => $capsule_id - ]); } - if (in_array($user_associated_with_email_address, $current_capsule_editors_users)) { - $this->addFlash( - 'warning', - $this->translator->trans( - 'editors.add.user.already_added', - [ - 'user_email' => $editor_email - ] - ) - ); + return $this->redirectToRoute('edit_capsule_editors', [ + 'capsule_id' => $capsule_id + ]); + } - return $this->redirectToRoute('edit_capsule_editors', [ - 'capsule_id' => $capsule_id - ]); - } - $capsule_editor = new CapsuleEditor(); - $capsule_editor->setUserId($user_associated_with_email_address->getId()); - $capsule_editor->setCapsuleId($capsule_id); - $entity_manager->persist($capsule_editor); - $entity_manager->flush(); - - $email = (new TemplatedEmail()) - ->to($editor_email) - ->subject($this->translator->trans('editors.add.user.email.title')) - ->htmlTemplate('capsule/editors/email_editor.html.twig') - ->context([ - 'user' => $current_user, - 'capsule' => $capsule - ]); - - $this->mailer->send($email); + $pending_editors = $this->capsule_pending_editor_repository->findBy(['capsule_id' => $capsule_id]); + + return $this->render('capsule/editors/list_editors.html.twig', [ + 'userPermissionsCapsuleForm' => $form->createView(), + 'capsule_name' => $capsule->getName(), + 'editors' => $current_capsule_editors_users, + 'pending_editors' => $pending_editors + ]); + } + private function addPendingEditor(string $editor_email, Capsule $capsule, User $current_user): void + { + $pending_editors_emails = $this->capsule_pending_editor_repository->getPendingEditorsEmails($capsule->getId()); + + if (in_array($editor_email, $pending_editors_emails)) { $this->addFlash( - 'success', + 'warning', $this->translator->trans( - 'editors.add.user.success', + 'editors.add.pending_editor.already_added', [ - 'capsule_name' => $capsule->getName(), 'user_email' => $editor_email ] ) ); + return; + } - return $this->redirectToRoute('edit_capsule_editors', [ - 'capsule_id' => $capsule_id + $pending_editor = new CapsulePendingEditor(); + $pending_editor->setCapsuleId($capsule->getId()); + $pending_editor->setEmail($editor_email); + $this->entity_manager->persist($pending_editor); + $this->entity_manager->flush(); + + $email = (new TemplatedEmail()) + ->to($editor_email) + ->subject($this->translator->trans('editors.add.pending_editor.email.title')) + ->htmlTemplate('capsule/editors/email_pending_editor.html.twig') + ->context([ + 'user' => $current_user, + 'capsule' => $capsule ]); + + $this->mailer->send($email); + + $this->addFlash( + 'success', + $this->translator->trans( + 'editors.add.pending_editor.success', + [ + 'user_email' => $editor_email + ] + ) + ); + } + + /** + * @param array<User> $current_capsule_editors_users + */ + private function addEditor( + string $editor_email, + Capsule $capsule, + User $current_user, + User $user_associated_with_email_address, + array $current_capsule_editors_users + ): void { + if (in_array($user_associated_with_email_address, $current_capsule_editors_users)) { + $this->addFlash( + 'warning', + $this->translator->trans( + 'editors.add.user.already_added', + [ + 'user_email' => $editor_email + ] + ) + ); + return; } + $capsule_editor = new CapsuleEditor(); + $capsule_editor->setUserId($user_associated_with_email_address->getId()); + $capsule_editor->setCapsuleId($capsule->getId()); + $this->entity_manager->persist($capsule_editor); + $this->entity_manager->flush(); + + $email = (new TemplatedEmail()) + ->to($editor_email) + ->subject($this->translator->trans('editors.add.user.email.title')) + ->htmlTemplate('capsule/editors/email_editor.html.twig') + ->context([ + 'user' => $current_user, + 'capsule' => $capsule + ]); - return $this->render('capsule/editors/list_editors.html.twig', [ - 'userPermissionsCapsuleForm' => $form->createView(), - 'capsule_name' => $capsule->getName(), - 'editors' => $current_capsule_editors_users, - 'pending_editors' => $pending_editors - ]); + $this->mailer->send($email); + + $this->addFlash( + 'success', + $this->translator->trans( + 'editors.add.user.success', + [ + 'capsule_name' => $capsule->getName(), + 'user_email' => $editor_email + ] + ) + ); } }