Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MailerServiceProvider
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 getTransport
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getSmtpTransport
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Mail;
6
7use Engelsystem\Config\Config;
8use Engelsystem\Container\ServiceProvider;
9use Engelsystem\Mail\Transport\LogTransport;
10use Symfony\Component\Mailer\Mailer as SymfonyMailer;
11use Symfony\Component\Mailer\MailerInterface;
12use Symfony\Component\Mailer\Transport;
13use Symfony\Component\Mailer\Transport\SendmailTransport;
14use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
15use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
16use Symfony\Component\Mailer\Transport\TransportInterface;
17
18class MailerServiceProvider extends ServiceProvider
19{
20    public function register(): void
21    {
22        /** @var Config $config */
23        $config = $this->app->get('config');
24        $mailConfig = $config->get('email');
25
26        $transport = $this->getTransport($mailConfig['driver'], $mailConfig);
27        $this->app->instance(TransportInterface::class, $transport);
28        $this->app->instance('mailer.transport', $transport);
29
30        /** @var SymfonyMailer $symfonyMailer */
31        $symfonyMailer = $this->app->make(SymfonyMailer::class);
32        $this->app->instance(SymfonyMailer::class, $symfonyMailer);
33        $this->app->instance(MailerInterface::class, $symfonyMailer);
34        $this->app->instance('mailer.symfony', $symfonyMailer);
35
36        /** @var EngelsystemMailer $mailer */
37        $mailer = $this->app->make(EngelsystemMailer::class);
38        $mailer->setFromAddress($mailConfig['from']['address']);
39        $mailer->setSubjectPrefix($config->get('app_name'));
40        if (!empty($mailConfig['from']['name'])) {
41            $mailer->setFromName($mailConfig['from']['name']);
42        }
43
44        $this->app->instance(EngelsystemMailer::class, $mailer);
45        $this->app->instance(Mailer::class, $mailer);
46        $this->app->instance('mailer', $mailer);
47    }
48
49    protected function getTransport(?string $transport, array $config): TransportInterface
50    {
51        return match ($transport) {
52            'log'              => $this->app->make(LogTransport::class),
53            'mail', 'sendmail' => $this->app->make(
54                SendmailTransport::class,
55                ['command' => $config['sendmail'] ?? null]
56            ),
57            'smtp'             => $this->getSmtpTransport($config),
58            default            => Transport::fromDsn($transport ?? ''),
59        };
60    }
61
62    protected function getSmtpTransport(array $config): SmtpTransport
63    {
64        /** @var EsmtpTransport $transport */
65        $transport = $this->app->make(EsmtpTransport::class, [
66            'host' => $config['host'] ?? 'localhost',
67            'port' => $config['port'] ?? 0,
68            'tls'  => $config['tls'] ?? null,
69            'logger' => null,
70        ]);
71
72        if (!empty($config['username'])) {
73            $transport->setUsername($config['username']);
74        }
75
76        if (!empty($config['password'])) {
77            $transport->setPassword($config['password']);
78        }
79
80        return $transport;
81    }
82}