Passed
Pull Request — master (#756)
by Maxim
07:24
created

MailerBootloader::start()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.4042

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 22
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 35
ccs 10
cts 18
cp 0.5556
crap 5.4042
rs 9.568
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\JsonJobSerializer;
29
use Spiral\SendIt\MailQueue;
30
use Symfony\Component\Mailer\Mailer;
31
use Symfony\Component\Mailer\MailerInterface as SymfonyMailer;
32
use Symfony\Component\Mailer\Transport;
33
34
/**
35
 * Enables email sending pipeline.
36
 */
37
class MailerBootloader extends Bootloader
38
{
39
    protected const DEPENDENCIES = [
40
        JobsBootloader::class,
41
        QueueBootloader::class,
42
    ];
43
44
    protected const SINGLETONS = [
45
        SymfonyMailer::class => [self::class, 'mailer'],
46
    ];
47
48
    /** @var ConfiguratorInterface */
49
    private $config;
50
51 2
    public function __construct(ConfiguratorInterface $config)
52
    {
53 2
        $this->config = $config;
54
    }
55
56 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

56
    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...
57
    {
58 2
        $queue = $env->get('MAILER_QUEUE', $env->get('MAILER_PIPELINE', 'local'));
59
60 2
        $this->config->setDefaults(MailerConfig::CONFIG, [
61 2
            'dsn' => $env->get('MAILER_DSN', ''),
62
            'pipeline' => $queue,
63
            'queue' => $queue,
64 2
            'from' => $env->get('MAILER_FROM', 'Spiral <[email protected]>'),
65 2
            'queueConnection' => $env->get('MAILER_QUEUE_CONNECTION'),
66
        ]);
67
    }
68
69 2
    public function start(Container $container): void
70
    {
71 2
        if ($container->has(JobRegistry::class)) {
72
            // Will be removed since v3.0
73 2
            $registry = $container->get(JobRegistry::class);
74 2
            $registry->setHandler(MailQueue::JOB_NAME, \Spiral\SendIt\Jobs\MailJobAdapter::class);
75 2
            $registry->setSerializer(MailQueue::JOB_NAME, JsonJobSerializer::class);
76
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 function (MailerConfig $config, QueueConnectionProviderInterface $provider) {
93
                    return new MailQueue(
94
                        $config,
95
                        $provider->getConnection($config->getQueueConnection())
96
                    );
97
                }
98
            );
99
        }
100
101 2
        if ($container->has(HandlerRegistryInterface::class)) {
102 2
            $registry = $container->get(HandlerRegistryInterface::class);
103 2
            $registry->setHandler(MailQueue::JOB_NAME, \Spiral\SendIt\MailJob::class);
104
        }
105
    }
106
107
    public function mailer(MailerConfig $config): SymfonyMailer
108
    {
109
        return new Mailer(
110
            Transport::fromDsn($config->getDSN())
111
        );
112
    }
113
}
114