feat: connector credentials check

This commit is contained in:
Maël Gangloff
2025-01-16 21:49:02 +01:00
parent 2441eb2925
commit 63bc105de9
8 changed files with 159 additions and 3 deletions

View File

@@ -24,7 +24,7 @@ class ConnectorController extends AbstractController
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
private readonly ContainerInterface $locator,
) {
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Message;
final class ValidateConnectorCredentials
{
/*
* Add whatever properties and methods you need
* to hold the data for this message class.
*/
// public function __construct(
// public readonly string $name,
// ) {
// }
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\MessageHandler;
use App\Message\ValidateConnectorCredentials;
use App\Notifier\ValidateConnectorCredentialsErrorNotification;
use App\Repository\ConnectorRepository;
use App\Service\Connector\AbstractProvider;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Recipient\Recipient;
#[AsMessageHandler]
final readonly class ValidateConnectorCredentialsHandler
{
public function __construct(
private ConnectorRepository $connectorRepository,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator,
private MailerInterface $mailer,
) {
}
/**
* @throws TransportExceptionInterface
*/
public function __invoke(ValidateConnectorCredentials $message): void
{
foreach ($this->connectorRepository->findAll() as $connector) {
$provider = $connector->getProvider();
try {
if (null === $provider) {
throw new \Exception('Provider not found');
}
/** @var AbstractProvider $providerClient */
$providerClient = $this->locator->get($provider->getConnectorProvider());
$providerClient->authenticate($connector->getAuthData());
} catch (\Exception) {
$email = $connector->getUser()->getEmail();
$this->mailer->send(
(new ValidateConnectorCredentialsErrorNotification(new Address($email), $connector))
->asEmailMessage(new Recipient($email))->getMessage()
);
}
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Notifier;
use App\Entity\Connector;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use Symfony\Component\Notifier\Message\EmailMessage;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
class ValidateConnectorCredentialsErrorNotification extends Notification
{
public function __construct(
private readonly Address $sender,
private readonly Connector $connector,
) {
parent::__construct();
}
public function asEmailMessage(EmailRecipientInterface $recipient): EmailMessage
{
return new EmailMessage((new TemplatedEmail())
->from($this->sender)
->to($recipient->getEmail())
->subject('Connector credentials error')
->htmlTemplate('emails/errors/connector_credentials.html.twig')
->locale('en')
->context([
'connector' => $this->connector,
]));
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Scheduler;
use App\Message\ValidateConnectorCredentials;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\ScheduleProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;
#[AsSchedule('ValidateConnectorCredentials')]
final class ValidateConnectorCredentialsSchedule implements ScheduleProviderInterface
{
public function __construct(
private CacheInterface $cache,
) {
}
public function getSchedule(): Schedule
{
return (new Schedule())
->add(
RecurringMessage::every('1 week', new ValidateConnectorCredentials()),
)
->stateful($this->cache)
;
}
}