<?php
namespace App\EventListener\PostValidate;
use App\DTO\User\Register;
use App\DTO\User\UserOutput;
use App\Service\Manager\AccountManager;
use App\Service\Manager\AccountPermissionManager;
use App\Service\Manager\ClientProfileManager;
use App\Service\Manager\SiteManager;
use App\Service\Manager\SitePermissionManager;
use App\Service\Manager\UserManager;
use App\Service\SiteNotificationSender;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class RegisterPostValidateTransformer implements EventSubscriberInterface
{
private $userManager;
private $clientProfileManager;
private $normalizer;
private $entityManager;
private $accountManager;
private $accountPermissionManager;
private $siteManager;
private $sitePermissionManager;
private $notificationSender;
public function __construct(
UserManager $userManager,
AccountManager $accountManager,
ClientProfileManager $clientProfileManager,
AccountPermissionManager $accountPermissionManager,
SiteManager $siteManager,
NormalizerInterface $normalizer,
EntityManagerInterface $entityManager,
SitePermissionManager $sitePermissionManager,
SiteNotificationSender $notificationSender
) {
$this->userManager = $userManager;
$this->clientProfileManager = $clientProfileManager;
$this->normalizer = $normalizer;
$this->entityManager = $entityManager;
$this->accountManager = $accountManager;
$this->accountPermissionManager = $accountPermissionManager;
$this->siteManager = $siteManager;
$this->sitePermissionManager = $sitePermissionManager;
$this->notificationSender = $notificationSender;
}
public static function getSubscribedEvents()
{
return [
PostValidateListener::POST_VALIDATE_TRANSFORMATION => 'transform',
];
}
public function supportTransformation($payload)
{
return $payload instanceof Register;
}
public function transform(ViewEvent $event)
{
/** @var Register $payload */
$payload = $event->getControllerResult();
if (!$this->supportTransformation($payload)) {
return;
}
$this->entityManager->getConnection()->beginTransaction();
try {
$user = $this->userManager->createUser(
$payload->email,
$payload->password,
null,
true,
['ROLE_CLIENT']
);
$this->clientProfileManager->createClientProfile(
$user,
null,
null,
null,
null,
$payload->phoneNumber,
null,
$payload->preferredLang ?? $event->getRequest()->getLocale()
);
$this->entityManager->refresh($user);
$account = $this->accountManager->create($user);
$this->accountPermissionManager->createPermissionTypeOwner($account, $user);
$site = $this->siteManager->create(
$account->getId(),
$payload->hostname,
$user,
null !== $payload->phoneNumber
);
$this->sitePermissionManager->createPermissionTypeOwner($site, $user);
$this->notificationSender->sendWelcomeEmail($site, $user);
$this->entityManager->getConnection()->commit();
} catch (\Exception $e) {
$this->entityManager->getConnection()->rollBack();
throw $e;
}
$event->setResponse(new JsonResponse($this->normalizer->normalize($user, UserOutput::class)));
}
}