55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Security;
|
||
|
|
||
|
use App\Entity\User;
|
||
|
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 readonly VerifyEmailHelperInterface $verifyEmailHelper,
|
||
|
private readonly MailerInterface $mailer,
|
||
|
private readonly EntityManagerInterface $entityManager
|
||
|
) {
|
||
|
}
|
||
|
|
||
|
public function sendEmailConfirmation(string $verifyEmailRouteName, User /* UserInterface */ $user, TemplatedEmail $email): void
|
||
|
{
|
||
|
$signatureComponents = $this->verifyEmailHelper->generateSignature(
|
||
|
routeName: $verifyEmailRouteName,
|
||
|
userId: $user->getId(),
|
||
|
userEmail: $user->getEmail(),
|
||
|
extraParams: ['id' => $user->getId()]
|
||
|
);
|
||
|
|
||
|
$context = $email->getContext();
|
||
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
||
|
$context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
|
||
|
$context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
|
||
|
|
||
|
$email->context(context: $context);
|
||
|
|
||
|
$this->mailer->send(message: $email);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @throws VerifyEmailExceptionInterface
|
||
|
*/
|
||
|
public function handleEmailConfirmation(Request $request, User /*UserInterface*/ $user): void
|
||
|
{
|
||
|
$this->verifyEmailHelper->validateEmailConfirmation(signedUrl: $request->getUri(), userId: $user->getId(), userEmail: $user->getEmail());
|
||
|
|
||
|
$user->setIsVerified(isVerified: true);
|
||
|
|
||
|
$this->entityManager->persist(entity: $user);
|
||
|
$this->entityManager->flush();
|
||
|
}
|
||
|
}
|