53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Security;
|
|
|
|
use App\Entity\Employee;
|
|
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 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, Employee $employee, TemplatedEmail $email): void
|
|
{
|
|
$signatureComponents = $this->verifyEmailHelper->generateSignature(
|
|
$verifyEmailRouteName,
|
|
(string) $employee->getId(),
|
|
(string) $employee->getEmail()
|
|
);
|
|
|
|
$context = $email->getContext();
|
|
$context['signedUrl'] = $signatureComponents->getSignedUrl();
|
|
$context['expiresAtMessageKey'] = $signatureComponents->getExpirationMessageKey();
|
|
$context['expiresAtMessageData'] = $signatureComponents->getExpirationMessageData();
|
|
|
|
$email->context($context);
|
|
|
|
}
|
|
|
|
/**
|
|
* @throws VerifyEmailExceptionInterface
|
|
*/
|
|
public function handleEmailConfirmation(Request $request, Employee $employee): void
|
|
{
|
|
$this->verifyEmailHelper->validateEmailConfirmationFromRequest($request, (string) $employee->getId(), (string) $employee->getEmail());
|
|
|
|
$employee->setVerified(true);
|
|
|
|
$this->entityManager->persist($employee);
|
|
$this->entityManager->flush();
|
|
}
|
|
}
|