Passed
Pull Request — master (#656)
by Abdul Malik
09:06 queued 02:28
created

MailerBootloader::start()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1173

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 21
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 32
ccs 10
cts 17
cp 0.5881
crap 5.1173
rs 9.584
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\SendIt\Bootloader;
13
14
use Spiral\Boot\AbstractKernel;
15
use Spiral\Boot\Bootloader\Bootloader;
16
use Spiral\Boot\EnvironmentInterface;
17
use Spiral\Bootloader\Jobs\JobsBootloader;
18
use Spiral\Config\ConfiguratorInterface;
19
use Spiral\Core\Container;
20
use Spiral\Jobs\JobRegistry;
21
use Spiral\Jobs\QueueInterface;
22
use Spiral\Jobs\ShortCircuit;
23
use Spiral\Mailer\MailerInterface;
24
use Spiral\Queue\Bootloader\QueueBootloader;
25
use Spiral\Queue\HandlerRegistryInterface;
26
use Spiral\Queue\QueueConnectionProviderInterface;
27
use Spiral\SendIt\Config\MailerConfig;
28
use Spiral\SendIt\MailJob;
29
use Spiral\SendIt\MailQueue;
30
use Spiral\SendIt\MessageSerializer;
31
use Symfony\Component\Mailer\Mailer;
32
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer;
33
use Symfony\Component\Mailer\Transport;
34
35
/**
36
 * Enables email sending pipeline.
37
 */
38
final class MailerBootloader extends Bootloader
39
{
40
    protected const DEPENDENCIES = [
41
        JobsBootloader::class,
42
        QueueBootloader::class,
43
    ];
44
45
    protected const SINGLETONS = [
46
        MailJob::class => MailJob::class,
47
        SymfonyMailer::class => [self::class, 'mailer'],
48
    ];
49
50
    private ConfiguratorInterface $config;
51
52 2
    public function __construct(ConfiguratorInterface $config)
53
    {
54 2
        $this->config = $config;
55
    }
56
57 2
    public function boot(EnvironmentInterface $env, AbstractKernel $kernel): void
0 ignored issues
show
Unused Code introduced by
The parameter $kernel is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

57
    public function boot(EnvironmentInterface $env, /** @scrutinizer ignore-unused */ AbstractKernel $kernel): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59 2
        $queue = $env->get('MAILER_QUEUE', $env->get('MAILER_PIPELINE', 'local'));
60
61 2
        $this->config->setDefaults(MailerConfig::CONFIG, [
62 2
            'dsn' => $env->get('MAILER_DSN', ''),
63
            'pipeline' => $queue,
64
            'queue' => $queue,
65 2
            'from' => $env->get('MAILER_FROM', 'Spiral <[email protected]>'),
66 2
            'queueConnection' => $env->get('MAILER_QUEUE_CONNECTION'),
67
        ]);
68
    }
69
70 2
    public function start(Container $container): void
71
    {
72 2
        if ($container->has(JobRegistry::class)) {
73
            // Will be removed since v3.0
74 2
            $registry = $container->get(JobRegistry::class);
75 2
            $registry->setHandler(MailQueue::JOB_NAME, MailJob::class);
76 2
            $registry->setSerializer(MailQueue::JOB_NAME, MessageSerializer::class);
77 2
            $container->bindSingleton(
78
                MailerInterface::class,
79 2
                static function (MailerConfig $config) use ($container) {
80
                    if ($config->getQueueConnection() === 'sync') {
81
                        $queue = $container->get(ShortCircuit::class);
82
                    } else {
83
                        $queue = $container->get(QueueInterface::class);
84
                    }
85
86
                    return new MailQueue($config, $queue);
87
                }
88
            );
89
        } else {
90
            $container->bindSingleton(
91
                MailerInterface::class,
92
                static fn(MailerConfig $config, QueueConnectionProviderInterface $provider) => new MailQueue(
93
                    $config,
94
                    $provider->getConnection($config->getQueueConnection())
95
                )
96
            );
97
        }
98
99 2
        if ($container->has(HandlerRegistryInterface::class)) {
100 2
            $registry = $container->get(HandlerRegistryInterface::class);
101 2
            $registry->setHandler(MailQueue::JOB_NAME, MailJob::class);
102
        }
103
    }
104
105
    public function mailer(MailerConfig $config): SymfonyMailer
106
    {
107
        return new Mailer(
108
            Transport::fromDsn($config->getDSN())
109
        );
110
    }
111
}
112