Mailer::sendMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mailer\SwiftMailer;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use RuntimeException;
9
use Swift_Events_EventListener;
10
use Swift_Mailer;
11
use Swift_Transport;
12
use Yiisoft\Mailer\Mailer as BaseMailer;
13
use Yiisoft\Mailer\MessageBodyRenderer;
14
use Yiisoft\Mailer\MessageFactoryInterface;
15
use Yiisoft\Mailer\MessageInterface;
16
17
/**
18
 * Mailer implements a mailer based on SwiftMailer.
19
 *
20
 * @see https://swiftmailer.symfony.com
21
 */
22
final class Mailer extends BaseMailer
23
{
24
    private Swift_Mailer $swiftMailer;
25
26
    /**
27
     * @param MessageFactoryInterface $messageFactory
28
     * @param MessageBodyRenderer $messageBodyRenderer
29
     * @param EventDispatcherInterface $eventDispatcher
30
     * @param Swift_Transport $transport
31
     * @param Swift_Events_EventListener[] $plugins
32
     */
33 60
    public function __construct(
34
        MessageFactoryInterface $messageFactory,
35
        MessageBodyRenderer $messageBodyRenderer,
36
        EventDispatcherInterface $eventDispatcher,
37
        Swift_Transport $transport,
38
        array $plugins = []
39
    ) {
40 60
        parent::__construct($messageFactory, $messageBodyRenderer, $eventDispatcher);
41 60
        $this->swiftMailer = new Swift_Mailer($transport);
42
43 60
        foreach ($plugins as $plugin) {
44 2
            $this->swiftMailer->registerPlugin($plugin);
45
        }
46
    }
47
48 1
    protected function sendMessage(MessageInterface $message): void
49
    {
50
        /** @var Message $message */
51 1
        $sent = $this->swiftMailer->send($message->getSwiftMessage());
52
53 1
        if ($sent === 0) {
54 1
            throw new RuntimeException('Unable send message.');
55
        }
56
    }
57
}
58