src/Common/Mailer/Configurator.php line 29

Open in your IDE?
  1. <?php
  2. namespace Common\Mailer;
  3. use PDOException;
  4. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  5. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Common\ModulesSettings;
  8. class Configurator
  9. {
  10.     /**
  11.      * @var ModulesSettings
  12.      */
  13.     private $modulesSettings;
  14.     /**
  15.      * @var ContainerInterface
  16.      */
  17.     private $container;
  18.     public function __construct(ModulesSettings $modulesSettingsContainerInterface $container)
  19.     {
  20.         $this->modulesSettings $modulesSettings;
  21.         $this->container $container;
  22.     }
  23.     public function onKernelRequest(GetResponseEvent $event): void
  24.     {
  25.         $this->configureMail();
  26.     }
  27.     public function onConsoleCommand(ConsoleCommandEvent $event): void
  28.     {
  29.         $this->configureMail();
  30.     }
  31.     private function configureMail(): void
  32.     {
  33.         try {
  34.             $transport TransportFactory::create(
  35.                 (string) $this->modulesSettings->get('Core''mailer_type''sendmail'),
  36.                 $this->modulesSettings->get('Core''smtp_server'),
  37.                 (int) $this->modulesSettings->get('Core''smtp_port'25),
  38.                 $this->modulesSettings->get('Core''smtp_username'),
  39.                 $this->modulesSettings->get('Core''smtp_password'),
  40.                 $this->modulesSettings->get('Core''smtp_secure_layer')
  41.             );
  42.             $mailer $this->container->get('mailer');
  43.             if ($mailer !== null) {
  44.                 $this->container->get('mailer')->__construct($transport);
  45.             }
  46.             $this->container->set(
  47.                 'swiftmailer.transport',
  48.                 $transport
  49.             );
  50.         } catch (PDOException $e) {
  51.             // we'll just use the mail transport thats pre-configured
  52.         }
  53.     }
  54. }