Passed
Push — master ( 2cbf33...efd300 )
by Viktor
01:36
created

MailerTransport   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 13
cts 22
cp 0.5909
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPlugin() 0 2 1
A isStarted() 0 3 1
A start() 0 2 1
A ping() 0 3 1
A __construct() 0 3 1
A stop() 0 2 1
A send() 0 13 3
1
<?php
2
/**
3
 * @link https://github.com/Izumi-kun/yii2-spoolmailer
4
 * @copyright Copyright (c) 2017 Viktor Khokhryakov
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace izumi\spoolmailer;
9
10
use Swift_Events_EventListener;
11
use Swift_Mime_SimpleMessage;
12
use yii\base\InvalidParamException;
13
use yii\mail\MailerInterface;
14
15
/**
16
 * Sends Messages using the `MailerInterface`.
17
 *
18
 * @author Viktor Khokhryakov <[email protected]>
19
 */
20
class MailerTransport implements \Swift_Transport
21
{
22
    /**
23
     * @var MailerInterface
24
     */
25
    protected $mailer;
26
27
    /**
28
     * FileTransport constructor.
29
     * @param MailerInterface $mailer
30
     */
31 5
    public function __construct(MailerInterface $mailer)
32
    {
33 5
        $this->mailer = $mailer;
34 5
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 5
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
40
    {
41 5
        if (!$message instanceof \Swift_Message) {
42
            throw new InvalidParamException('The message should be an instance of "Swift_Message".');
43
        }
44 5
        $failedRecipients = (array) $failedRecipients;
45 5
        $importedMessage = new ImportedMessage($message);
46
47 5
        if ($this->mailer->send($importedMessage)) {
48 4
            return count($importedMessage->getTo());
49
        } else {
50 1
            $failedRecipients = (array) $importedMessage->getTo();
51 1
            return 0;
52
        }
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 3
    public function isStarted()
59
    {
60 3
        return true;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function start()
67
    {
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function stop()
74
    {
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function registerPlugin(Swift_Events_EventListener $plugin)
81
    {
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function ping()
88
    {
89
        return true;
90
    }
91
}
92