MailerTransport::stop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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_Message;
12
use Swift_Mime_SimpleMessage;
13
use Swift_Transport;
14
use yii\base\InvalidParamException;
15
use yii\base\NotSupportedException;
16
use yii\mail\MailerInterface;
17
18
/**
19
 * Sends Messages using the `MailerInterface`.
20
 *
21
 * @author Viktor Khokhryakov <[email protected]>
22
 */
23
class MailerTransport implements Swift_Transport
24
{
25
    /**
26
     * @var MailerInterface
27
     */
28
    protected $mailer;
29
30
    /**
31
     * FileTransport constructor.
32
     * @param MailerInterface $mailer
33
     */
34 9
    public function __construct(MailerInterface $mailer)
35
    {
36 9
        $this->mailer = $mailer;
37 9
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 7
    public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
43
    {
44 7
        if (!$message instanceof Swift_Message) {
45 1
            throw new InvalidParamException('The message should be an instance of "Swift_Message".');
46
        }
47 6
        $failedRecipients = (array) $failedRecipients;
48 6
        $importedMessage = new ImportedMessage($message);
49
50 6
        if ($this->mailer->send($importedMessage)) {
51 5
            return count((array) $importedMessage->getTo());
52
        } else {
53 1
            $failedRecipients = (array) $importedMessage->getTo();
54 1
            return 0;
55
        }
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 4
    public function isStarted()
62
    {
63 4
        return true;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 1
    public function start()
70
    {
71 1
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 1
    public function stop()
77
    {
78 1
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 1
    public function registerPlugin(Swift_Events_EventListener $plugin)
84
    {
85 1
        throw new NotSupportedException();
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 1
    public function ping()
92
    {
93 1
        return true;
94
    }
95
}
96