Passed
Pull Request — master (#736)
by Abdul Malik
07:17 queued 45s
created

MailerBootloader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 64.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 71
ccs 18
cts 28
cp 0.6429
rs 10
c 1
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 10 1
A __construct() 0 3 1
A mailer() 0 4 1
A start() 0 33 4
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\SendIt\Jobs\MailJobAdapter;
15
use Spiral\SendIt\MailJob;
16
use Spiral\Boot\AbstractKernel;
17
use Spiral\Boot\Bootloader\Bootloader;
18
use Spiral\Boot\EnvironmentInterface;
19
use Spiral\Bootloader\Jobs\JobsBootloader;
20
use Spiral\Config\ConfiguratorInterface;
21
use Spiral\Core\Container;
22
use Spiral\Jobs\JobRegistry;
23
use Spiral\Jobs\QueueInterface;
24
use Spiral\Jobs\ShortCircuit;
25
use Spiral\Mailer\MailerInterface;
26
use Spiral\Queue\Bootloader\QueueBootloader;
27
use Spiral\Queue\HandlerRegistryInterface;
28
use Spiral\Queue\QueueConnectionProviderInterface;
29
use Spiral\SendIt\Config\MailerConfig;
30
use Spiral\SendIt\JsonJobSerializer;
31
use Spiral\SendIt\MailQueue;
32
use Symfony\Component\Mailer\Mailer;
33
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer;
34
use Symfony\Component\Mailer\Transport;
35
36
/**
37
 * Enables email sending pipeline.
38
 */
39
class MailerBootloader extends Bootloader
40
{
41
    protected const DEPENDENCIES = [
42
        JobsBootloader::class,
43
        QueueBootloader::class,
44
    ];
45
46
    protected const SINGLETONS = [
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, MailJobAdapter::class);
76 2
            $registry->setSerializer(MailQueue::JOB_NAME, JsonJobSerializer::class);
77
78 2
            $container->bindSingleton(
79
                MailerInterface::class,
80 2
                static function (MailerConfig $config) use ($container) {
81
                    if ($config->getQueueConnection() === 'sync') {
82
                        $queue = $container->get(ShortCircuit::class);
83
                    } else {
84
                        $queue = $container->get(QueueInterface::class);
85
                    }
86
87
                    return new MailQueue($config, $queue);
88
                }
89
            );
90
        } else {
91
            $container->bindSingleton(
92
                MailerInterface::class,
93
                static fn (MailerConfig $config, QueueConnectionProviderInterface $provider) => new MailQueue(
94
                    $config,
95
                    $provider->getConnection($config->getQueueConnection())
96
                )
97
            );
98
        }
99
100 2
        if ($container->has(HandlerRegistryInterface::class)) {
101 2
            $registry = $container->get(HandlerRegistryInterface::class);
102 2
            $registry->setHandler(MailQueue::JOB_NAME, MailJob::class);
103
        }
104
    }
105
106
    public function mailer(MailerConfig $config): SymfonyMailer
107
    {
108
        return new Mailer(
109
            Transport::fromDsn($config->getDSN())
110
        );
111
    }
112
}
113