<?php
namespace App\Subscriber\User\Registration;
use App\Dictionary\MailProviderStatus;
use App\Entity\Account;
use App\Event\User\RegistrationCheckEmailEvent;
use App\Service\Mail\MogelmailService;
use App\Service\System\MailProviderService;
use App\Service\User\AccountService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckEmailSubscriber implements EventSubscriberInterface
{
/**
* @var AccountService
*/
protected $accountService;
/**
* @var MogelmailService
*/
protected $mogelmailService;
/**
* @var MailProviderService
*/
protected $mailProviderService;
/**
* CheckEmailSubscriber constructor.
*/
public function __construct(AccountService $accountService, MailProviderService $mailProviderService,
MogelmailService $mogelmailService)
{
$this->accountService = $accountService;
$this->mogelmailService = $mogelmailService;
$this->mailProviderService = $mailProviderService;
}
public static function getSubscribedEvents(): array
{
return [
RegistrationCheckEmailEvent::class => [
['checkStringFormat', 100],
['checkAlreadyExistingEmailAddress', 95],
['checkDomainIsBlockedByMailProviders', 90],
['checkMogelmailSuspected', 85],
],
];
}
public function checkStringFormat(RegistrationCheckEmailEvent $event)
{
$input = $event->getEmail();
if (!strstr($input, '@')) {
$event->setDeclineReason('Invalid email format.');
$event->stopPropagation();
}
}
public function checkAlreadyExistingEmailAddress(RegistrationCheckEmailEvent $event)
{
$email = $event->getEmail();
if ($this->accountService->findAccountByEmail($email) instanceof Account) {
$event->setDeclineReason('Email address already taken.');
$event->stopPropagation();
}
}
public function checkDomainIsBlockedByMailProviders(RegistrationCheckEmailEvent $event)
{
$email = $event->getEmail();
$domain = $this->mogelmailService->extractDomain($email);
$provider = $this->mailProviderService->findByName($domain);
if (!$provider) {
return;
}
$status = $provider->getBounceStatus();
if (MailProviderStatus::OK == $status) {
return;
}
foreach (explode("\n", $provider->getDomains()) as $blockedDomain) {
if ($blockedDomain == $domain) {
$event->setDeclineReason('Domain '.$domain.' is blocked ('.$status.').');
$event->stopPropagation();
return;
}
}
}
public function checkMogelmailSuspected(RegistrationCheckEmailEvent $event)
{
$email = $event->getEmail();
if (!$this->mogelmailService->isEmailSuspectForTrash($email)) {
return;
}
// create provider entity with status
$this->mogelmailService->triggerSuspectMatch($email);
$event->setDeclineReason('Email address appears to be suspect of spam.');
$event->stopPropagation();
}
}