vendor/ibexa/user/src/lib/Form/Processor/UserRegisterFormProcessor.php line 67

Open in your IDE?
  1. <?php
  2. /**
  3. * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4. * @license For full copyright and license information view LICENSE file distributed with this source code.
  5. */
  6. namespace Ibexa\User\Form\Processor;
  7. use Ibexa\ContentForms\Event\FormActionEvent;
  8. use Ibexa\Contracts\Core\Repository\Repository;
  9. use Ibexa\Contracts\Core\Repository\RoleService;
  10. use Ibexa\Contracts\Core\Repository\UserService;
  11. use Ibexa\Contracts\Core\Repository\Values\User\User;
  12. use Ibexa\Contracts\Notifications\Service\NotificationServiceInterface;
  13. use Ibexa\Contracts\Notifications\Value\Notification\SymfonyNotificationAdapter;
  14. use Ibexa\Contracts\Notifications\Value\Recipent\SymfonyRecipientAdapter;
  15. use Ibexa\Contracts\Notifications\Value\Recipent\UserRecipient;
  16. use Ibexa\Contracts\User\Notification\UserRegister;
  17. use Ibexa\User\Form\Data\UserRegisterData;
  18. use Ibexa\User\Form\UserFormEvents;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  22. use Symfony\Component\Routing\RouterInterface;
  23. /**
  24. * Listens for and processes User register events.
  25. */
  26. class UserRegisterFormProcessor implements EventSubscriberInterface
  27. {
  28. private UserService $userService;
  29. private UrlGeneratorInterface $urlGenerator;
  30. private Repository $repository;
  31. private RoleService $roleService;
  32. private NotificationServiceInterface $notificationService;
  33. public function __construct(
  34. Repository $repository,
  35. UserService $userService,
  36. RouterInterface $router,
  37. RoleService $roleService,
  38. NotificationServiceInterface $notificationService
  39. ) {
  40. $this->userService = $userService;
  41. $this->urlGenerator = $router;
  42. $this->repository = $repository;
  43. $this->roleService = $roleService;
  44. $this->notificationService = $notificationService;
  45. }
  46. public static function getSubscribedEvents()
  47. {
  48. return [
  49. UserFormEvents::USER_REGISTER => ['processRegister', 20],
  50. ];
  51. }
  52. /**
  53. * @param \Ibexa\ContentForms\Event\FormActionEvent $event
  54. *
  55. * @throws \Exception
  56. */
  57. public function processRegister(FormActionEvent $event)
  58. {
  59. /** @var \Ibexa\User\Form\Data\UserRegisterData $data */
  60. if (!($data = $event->getData()) instanceof UserRegisterData) {
  61. return;
  62. }
  63. $form = $event->getForm();
  64. $user = $this->createUser($data, $form->getConfig()->getOption('languageCode'));
  65. $this->sendNotification($user);
  66. $redirectUrl = $this->urlGenerator->generate('ibexa.user.register_confirmation');
  67. $event->setResponse(new RedirectResponse($redirectUrl));
  68. $event->stopPropagation();
  69. }
  70. private function createUser(UserRegisterData $data, string $languageCode): User
  71. {
  72. foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) {
  73. $data->setField($fieldDefIdentifier, $fieldData->value, $languageCode);
  74. }
  75. return $this->repository->sudo(
  76. function () use ($data) {
  77. $user = $this->userService->createUser($data, $data->getParentGroups());
  78. if ($data->getRole() !== null) {
  79. $this->roleService->assignRoleToUser($data->getRole(), $user, $data->getRoleLimitation());
  80. }
  81. return $user;
  82. }
  83. );
  84. }
  85. private function sendNotification(User $user): void
  86. {
  87. $this->notificationService->send(
  88. new SymfonyNotificationAdapter(
  89. new UserRegister($user),
  90. ),
  91. [new SymfonyRecipientAdapter(new UserRecipient($user))],
  92. );
  93. }
  94. }
  95. class_alias(UserRegisterFormProcessor::class, 'EzSystems\EzPlatformUser\Form\Processor\UserRegisterFormProcessor');