Completed
Push — master ( 6405ba...83526e )
by Walter
19:53 queued 04:58
created

SwiftMailer::setFromName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Communicator (https://github.com/waltertamboer/communicator)
4
 *
5
 * @link https://github.com/waltertamboer/communicator for the canonical source repository
6
 * @copyright Copyright (c) 2017 Communicator (https://github.com/waltertamboer/communicator)
7
 * @license https://github.com/waltertamboer/communicator/blob/master/LICENSE.md MIT
8
 */
9
10
namespace Communicator\Transport\Email\Transport;
11
12
use Communicator\Message;
13
use Communicator\Recipient\RecipientInterface;
14
use Swift_Mailer;
15
use Swift_Message;
16
17
/**
18
 * An e-mail transport that makes use of Swift Mailer.
19
 */
20
final class SwiftMailer extends AbstractTransport
21
{
22
    /**
23
     * The mailer used to send messages.
24
     *
25
     * @var Swift_Mailer
26
     */
27
    private $mailer;
28
29
    /**
30
     * Initializes a new instance of this class.
31
     *
32
     * @param Swift_Mailer $mailer
33
     */
34
    public function __construct(Swift_Mailer $mailer)
35
    {
36
        $this->mailer = $mailer;
37
    }
38
39
    /**
40
     * Sends a message over the transport.
41
     *
42
     * @param array $recipients A list with all recipients that should receive the message.
43
     * @param Message $message The message to send.
44
     * @param string $subject The subject for this message.
45
     * @param string $text The plain text template.
46
     * @param null|string $html The HTML template.
47
     * @return void
48
     */
49
    public function send(array $recipients, Message $message, string $subject, string $text, ?string $html): void
50
    {
51
        /** @var RecipientInterface $recipient */
52
        foreach ($recipients as $recipient) {
53
            $this->sendToRecipient($recipient, $message, $subject, $text, $html);
54
        }
55
    }
56
57
    /**
58
     * Sends a message to the given recipient.
59
     *
60
     * @param RecipientInterface $recipient The recipient that should receive the message.
61
     * @param Message $message The message that should be sent.
62
     * @param string $subject The subject of the message.
63
     * @param string $text The plain text message.
64
     * @param null|string $html An optional HTML version of the message.
65
     */
66
    private function sendToRecipient(
67
        RecipientInterface $recipient,
68
        Message $message,
69
        string $subject,
70
        string $text,
71
        ?string $html
72
    ): void {
73
        $addresses = $this->getAddresses($recipient, $message);
74
75
        foreach ($addresses as $address) {
76
            /** @var Swift_Message $emailMessage */
77
            $emailMessage = $this->mailer->createMessage();
78
            $emailMessage->setSubject($subject);
79
            $emailMessage->setTo($address);
80
            $emailMessage->setBody($text);
81
82
            if ($this->getFromAddress() !== null) {
83
                $emailMessage->setFrom($this->getFromAddress(), $this->getFromName());
84
            }
85
86
            if ($html !== null) {
87
                $emailMessage->addPart($html, 'text/html');
88
            }
89
90
            $this->mailer->send($emailMessage);
91
        }
92
    }
93
}
94