Passed
Pull Request — master (#873)
by butschster
07:05
created

MailerBootloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
eloc 33
c 3
b 1
f 0
dl 0
loc 70
ccs 34
cts 34
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 1
A mailer() 0 5 1
A boot() 0 13 1
A initTransportResolver() 0 11 1
A initTransport() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\SendIt\Bootloader;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Psr\Log\LoggerInterface;
10
use Spiral\Boot\Bootloader\Bootloader;
11
use Spiral\Boot\EnvironmentInterface;
12
use Spiral\Config\ConfiguratorInterface;
13
use Spiral\Core\BinderInterface;
14
use Spiral\Logger\LogsInterface;
15
use Spiral\Mailer\MailerInterface;
16
use Spiral\Queue\Bootloader\QueueBootloader;
17
use Spiral\Queue\QueueConnectionProviderInterface;
18
use Spiral\Queue\QueueRegistry;
19
use Spiral\SendIt\Config\MailerConfig;
20
use Spiral\SendIt\MailJob;
21
use Spiral\SendIt\MailQueue;
22
use Spiral\SendIt\TransportRegistryInterface;
23
use Spiral\SendIt\TransportResolver;
24
use Spiral\SendIt\TransportResolverInterface;
25
use Symfony\Component\Mailer\Mailer;
26
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer;
27
use Symfony\Component\Mailer\Transport;
28
use Symfony\Component\Mailer\Transport\TransportInterface;
29
30
/**
31
 * Enables email sending pipeline.
32
 */
33
class MailerBootloader extends Bootloader
34
{
35
    protected const DEPENDENCIES = [
36
        QueueBootloader::class,
37
        BuilderBootloader::class,
38
    ];
39
40
    protected const SINGLETONS = [
41
        MailJob::class => MailJob::class,
42
        SymfonyMailer::class => [self::class, 'mailer'],
43
        TransportResolver::class => [self::class, 'initTransportResolver'],
44
        TransportResolverInterface::class => TransportResolver::class,
45
        TransportRegistryInterface::class => TransportResolver::class,
46
        TransportInterface::class => [self::class, 'initTransport'],
47
    ];
48
49 273
    public function __construct(
50
        private readonly ConfiguratorInterface $config
51
    ) {
52 273
    }
53
54 273
    public function init(EnvironmentInterface $env): void
55
    {
56 273
        $this->config->setDefaults(MailerConfig::CONFIG, [
57 273
            'dsn' => $env->get('MAILER_DSN', ''),
58 273
            'queue' => $env->get('MAILER_QUEUE', 'local'),
59 273
            'from' => $env->get('MAILER_FROM', 'Spiral <[email protected]>'),
60 273
            'queueConnection' => $env->get('MAILER_QUEUE_CONNECTION'),
61 273
        ]);
62
    }
63
64 273
    public function boot(BinderInterface $binder, ContainerInterface $container): void
65
    {
66 273
        $binder->bindSingleton(
67 273
            MailerInterface::class,
68 273
            static fn (MailerConfig $config, QueueConnectionProviderInterface $provider): MailQueue => new MailQueue(
69 273
                $config,
70 273
                $provider->getConnection($config->getQueueConnection())
71 273
            )
72 273
        );
73
74 273
        $registry = $container->get(QueueRegistry::class);
75 273
        \assert($registry instanceof QueueRegistry);
76 273
        $registry->setHandler(MailQueue::JOB_NAME, MailJob::class);
77
    }
78
79 3
    public function initTransport(MailerConfig $config, TransportResolverInterface $transports): TransportInterface
80
    {
81 3
        return $transports->resolve($config->getDSN());
82
    }
83
84 2
    public function mailer(TransportInterface $transport, ?EventDispatcherInterface $dispatcher = null): SymfonyMailer
85
    {
86 2
        return new Mailer(
87 2
            transport: $transport,
88 2
            dispatcher: $dispatcher
89 2
        );
90
    }
91
92 4
    private function initTransportResolver(
93
        ?EventDispatcherInterface $dispatcher = null,
94
        ?LogsInterface $logs = null,
95
    ): TransportResolver {
96 4
        $defaultTransports = \iterator_to_array(Transport::getDefaultFactories(
97 4
            dispatcher: $dispatcher,
98 4
            logger: $logs?->getLogger('mailer')
99 4
        ));
100
101 4
        return new TransportResolver(
102 4
            new Transport($defaultTransports)
103 4
        );
104
    }
105
}
106