File size: 2,140 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?php
declare(strict_types=1);
namespace Shared\Infrastructure\Bootstrappers;
use Application;
use Easy\Container\Attributes\Inject;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;
use Shared\Infrastructure\BootstrapperInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\NullTransport;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
class MailerBootstrapper implements BootstrapperInterface
{
/**
* @param Application $app
* @param EventDispatcherInterface $dispatcher
* @param LoggerInterface $logger
* @param null|string $transport
* @param array $smtp
* @return void
*/
public function __construct(
private Application $app,
private EventDispatcherInterface $dispatcher,
private LoggerInterface $logger,
#[Inject('option.mail.transport')]
private ?string $transport = null,
#[Inject('option.smtp')]
private array $smtp = [],
) {
}
/** @return void */
public function bootstrap(): void
{
switch ($this->transport) {
case 'smtp':
$transport = new EsmtpTransport(
$this->smtp['host'] ?? 'localhost',
(int)($this->smtp['port'] ?? 0),
dispatcher: $this->dispatcher,
logger: $this->logger,
);
if (isset($this->smtp['username'])) {
$transport->setUsername($this->smtp['username']);
}
if (isset($this->smtp['password'])) {
$transport->setPassword($this->smtp['password']);
}
break;
default:
$transport = new NullTransport(
dispatcher: $this->dispatcher,
logger: $this->logger,
);
break;
}
$mailer = new Mailer($transport, null, $this->dispatcher);
$this->app->set(MailerInterface::class, $mailer);
}
}
|