53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Security;
|
||
|
|
||
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
use Symfony\Component\Mailer\MailerInterface;
|
||
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||
|
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
|
||
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
|
||
|
|
||
|
class EmailVerifier
|
||
|
{
|
||
|
public function __construct(
|
||
|
private VerifyEmailHelperInterface $verifyEmailHelper,
|
||
|
private MailerInterface $mailer,
|
||
|
private EntityManagerInterface $entityManager
|
||
|
) {
|
||
|
}
|
||
|
|
||
|
public function sendEmailConfirmation(string $verifyEmailRouteName, UserInterface $user, TemplatedEmail $email): void
|
||
|
{
|
||
|
$signatureComponents = $this->verifyEmailHelper->generateSignature(
|
||
|
$verifyEmailRouteName,
|
||
|
$user->getId(),
|
||
|
$user->getEmail()
|
||
|
);
|
||
|
|
||
|
$context = $email->getContext();
|
||
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
||
|
$context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
|
||
|
$context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
|
||
|
|
||
|
$email->context($context);
|
||
|
|
||
|
$this->mailer->send($email);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws VerifyEmailExceptionInterface
|
||
|
*/
|
||
|
public function handleEmailConfirmation(Request $request, UserInterface $user): void
|
||
|
{
|
||
|
$this->verifyEmailHelper->validateEmailConfirmation($request->getUri(), $user->getId(), $user->getEmail());
|
||
|
|
||
|
$user->setIsVerified(true);
|
||
|
|
||
|
$this->entityManager->persist($user);
|
||
|
$this->entityManager->flush();
|
||
|
}
|
||
|
}
|