Test Failed
Pull Request — master (#873)
by butschster
08:25
created

MailerBootloader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
eloc 34
c 4
b 1
f 0
dl 0
loc 71
ccs 27
cts 27
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 12 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 283
        MailJob::class => MailJob::class,
42
        SymfonyMailer::class => [self::class, 'mailer'],
43
        TransportResolver::class => [self::class, 'initTransportResolver'],
44 283
        TransportResolverInterface::class => TransportResolver::class,
45
        TransportRegistryInterface::class => TransportResolver::class,
46 283
        TransportInterface::class => [self::class, 'initTransport'],
47
    ];
48 283
49 283
    public function __construct(
50 283
        private readonly ConfiguratorInterface $config
51 283
    ) {
52 283
    }
53 283
54
    public function init(EnvironmentInterface $env): void
55
    {
56 283
        $this->config->setDefaults(MailerConfig::CONFIG, [
57
            'dsn' => $env->get('MAILER_DSN', ''),
58 283
            'queue' => $env->get('MAILER_QUEUE', 'local'),
59 283
            'from' => $env->get('MAILER_FROM', 'Spiral <[email protected]>'),
60 283
            'queueConnection' => $env->get('MAILER_QUEUE_CONNECTION'),
61 283
        ]);
62 283
    }
63 283
64 283
    public function boot(BinderInterface $binder, ContainerInterface $container): void
65
    {
66 283
        $binder->bindSingleton(
67 283
            MailerInterface::class,
68 283
            static fn (MailerConfig $config, QueueConnectionProviderInterface $provider): MailQueue => new MailQueue(
69
                $config,
70
                $provider->getConnection($config->getQueueConnection())
71 3
            )
72
        );
73 3
74
        $registry = $container->get(QueueRegistry::class);
75
        \assert($registry instanceof QueueRegistry);
76 2
        $registry->setHandler(MailQueue::JOB_NAME, MailJob::class);
77
    }
78 2
79 2
    public function initTransport(MailerConfig $config, TransportResolverInterface $transports): TransportInterface
80 2
    {
81 2
        return $transports->resolve($config->getDSN());
82
    }
83
84
    public function mailer(TransportInterface $transport, ?EventDispatcherInterface $dispatcher = null): SymfonyMailer
85
    {
86
        return new Mailer(
87
            transport: $transport,
88
            dispatcher: $dispatcher
89
        );
90
    }
91
92
    protected function initTransportResolver(
93
        ?EventDispatcherInterface $dispatcher = null,
94
        ?LogsInterface $logs = null,
95
    ): TransportResolver {
96
        $defaultTransports = \iterator_to_array(Transport::getDefaultFactories(
97
            $dispatcher,
98
            null,
99
            $logs?->getLogger('mailer')
100
        ));
101
102
        return new TransportResolver(
103
            new Transport($defaultTransports)
104
        );
105
    }
106
}
107