<?php
namespace App\EventSubscriber\App;
use App\Features\Provider\Factory\ProviderFactory;
use App\Features\Provider\Infrastructure\Notification\ProviderLoginLinkNotification;
use App\Features\ProviderRequest\Event\ProviderRequestApprovedEvent;
use App\Features\ProviderRequest\Event\ProviderRequestBaseEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Component\Notifier\Recipient\Recipient;
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
class ProviderRequestEventSubscriber implements EventSubscriberInterface
{
public function __construct(private LoggerInterface $logger, private EntityManagerInterface $manager, private NotifierInterface $notifier, private LoginLinkHandlerInterface $loginLinkHandler){}
public static function getSubscribedEvents(): array
{
return [
ProviderRequestApprovedEvent::NAME => 'creatProvider',
];
}
public function creatProvider(ProviderRequestBaseEvent $event): void
{
try {
$provider= ProviderFactory::fromProviderRequest($event->getRequest());
$this->manager->persist($provider);
$this->manager->flush();
$loginLinkDetails = $this->loginLinkHandler->createLoginLink($provider);
$notification = new ProviderLoginLinkNotification($provider, $loginLinkDetails);
$recipient = new Recipient($provider->getEmail());
$this->notifier->send($notification, $recipient);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}