src/EventSubscriber/App/ProviderRequestEventSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\App;
  3. use App\Features\Provider\Factory\ProviderFactory;
  4. use App\Features\Provider\Infrastructure\Notification\ProviderLoginLinkNotification;
  5. use App\Features\ProviderRequest\Event\ProviderRequestApprovedEvent;
  6. use App\Features\ProviderRequest\Event\ProviderRequestBaseEvent;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Notifier\NotifierInterface;
  11. use Symfony\Component\Notifier\Recipient\Recipient;
  12. use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface;
  13. class ProviderRequestEventSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(private LoggerInterface $logger, private EntityManagerInterface $manager, private NotifierInterface $notifier, private LoginLinkHandlerInterface $loginLinkHandler){}
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             ProviderRequestApprovedEvent::NAME => 'creatProvider',
  20.         ];
  21.     }
  22.     public function creatProvider(ProviderRequestBaseEvent $event): void
  23.     {
  24.         try {
  25.             $providerProviderFactory::fromProviderRequest($event->getRequest());
  26.             $this->manager->persist($provider);
  27.             $this->manager->flush();
  28.             $loginLinkDetails $this->loginLinkHandler->createLoginLink($provider);
  29.             $notification = new ProviderLoginLinkNotification($provider$loginLinkDetails);
  30.             $recipient = new Recipient($provider->getEmail());
  31.             $this->notifier->send($notification$recipient);
  32.         } catch (\Exception $e) {
  33.             $this->logger->error($e->getMessage());
  34.         }
  35.     }
  36. }