src/EventListener/PostValidate/RegisterPostValidateTransformer.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\PostValidate;
  3. use App\DTO\User\Register;
  4. use App\DTO\User\UserOutput;
  5. use App\Service\Manager\AccountManager;
  6. use App\Service\Manager\AccountPermissionManager;
  7. use App\Service\Manager\ClientProfileManager;
  8. use App\Service\Manager\SiteManager;
  9. use App\Service\Manager\SitePermissionManager;
  10. use App\Service\Manager\UserManager;
  11. use App\Service\SiteNotificationSender;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpKernel\Event\ViewEvent;
  16. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  17. class RegisterPostValidateTransformer implements EventSubscriberInterface
  18. {
  19.     private $userManager;
  20.     private $clientProfileManager;
  21.     private $normalizer;
  22.     private $entityManager;
  23.     private $accountManager;
  24.     private $accountPermissionManager;
  25.     private $siteManager;
  26.     private $sitePermissionManager;
  27.     private $notificationSender;
  28.     public function __construct(
  29.         UserManager $userManager,
  30.         AccountManager $accountManager,
  31.         ClientProfileManager $clientProfileManager,
  32.         AccountPermissionManager $accountPermissionManager,
  33.         SiteManager $siteManager,
  34.         NormalizerInterface $normalizer,
  35.         EntityManagerInterface $entityManager,
  36.         SitePermissionManager $sitePermissionManager,
  37.         SiteNotificationSender $notificationSender
  38.     ) {
  39.         $this->userManager $userManager;
  40.         $this->clientProfileManager $clientProfileManager;
  41.         $this->normalizer $normalizer;
  42.         $this->entityManager $entityManager;
  43.         $this->accountManager $accountManager;
  44.         $this->accountPermissionManager $accountPermissionManager;
  45.         $this->siteManager $siteManager;
  46.         $this->sitePermissionManager $sitePermissionManager;
  47.         $this->notificationSender $notificationSender;
  48.     }
  49.     public static function getSubscribedEvents()
  50.     {
  51.         return [
  52.             PostValidateListener::POST_VALIDATE_TRANSFORMATION => 'transform',
  53.         ];
  54.     }
  55.     public function supportTransformation($payload)
  56.     {
  57.         return $payload instanceof Register;
  58.     }
  59.     public function transform(ViewEvent $event)
  60.     {
  61.         /** @var Register $payload */
  62.         $payload $event->getControllerResult();
  63.         if (!$this->supportTransformation($payload)) {
  64.             return;
  65.         }
  66.         $this->entityManager->getConnection()->beginTransaction();
  67.         try {
  68.             $user $this->userManager->createUser(
  69.                 $payload->email,
  70.                 $payload->password,
  71.                 null,
  72.                 true,
  73.                 ['ROLE_CLIENT']
  74.             );
  75.             $this->clientProfileManager->createClientProfile(
  76.                 $user,
  77.                 null,
  78.                 null,
  79.                 null,
  80.                 null,
  81.                 $payload->phoneNumber,
  82.                 null,
  83.                 $payload->preferredLang ?? $event->getRequest()->getLocale()
  84.             );
  85.             $this->entityManager->refresh($user);
  86.             $account $this->accountManager->create($user);
  87.             $this->accountPermissionManager->createPermissionTypeOwner($account$user);
  88.             $site $this->siteManager->create(
  89.                 $account->getId(),
  90.                 $payload->hostname,
  91.                 $user,
  92.                 null !== $payload->phoneNumber
  93.             );
  94.             $this->sitePermissionManager->createPermissionTypeOwner($site$user);
  95.             $this->notificationSender->sendWelcomeEmail($site$user);
  96.             $this->entityManager->getConnection()->commit();
  97.         } catch (\Exception $e) {
  98.             $this->entityManager->getConnection()->rollBack();
  99.             throw $e;
  100.         }
  101.         $event->setResponse(new JsonResponse($this->normalizer->normalize($userUserOutput::class)));
  102.     }
  103. }