Completed
Push — master ( 83526e...bd9b49 )
by Walter
13:46
created

PHPMailer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B sendToRecipient() 0 29 4
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 PHPMailer as PHPMailerInstance;
15
use Swift_Message;
16
17
/**
18
 * An e-mail transport that makes use of PHPMailer.
19
 */
20
final class PHPMailer extends AbstractTransport
21
{
22
    /**
23
     * The mailer used to send messages.
24
     *
25
     * @var PHPMailerInstance
26
     */
27
    private $mailer;
28
29
    /**
30
     * Initializes a new instance of this class.
31
     *
32
     * @param PHPMailerInstance $mailer
33
     */
34
    public function __construct(PHPMailerInstance $mailer)
35
    {
36
        $this->mailer = $mailer;
37
    }
38
39
    /**
40
     * Sends a message to the given recipient.
41
     *
42
     * @param RecipientInterface $recipient The recipient that should receive the message.
43
     * @param Message $message The message that should be sent.
44
     * @param string $subject The subject of the message.
45
     * @param string $text The plain text message.
46
     * @param null|string $html An optional HTML version of the message.
47
     */
48
    protected function sendToRecipient(
49
        RecipientInterface $recipient,
50
        Message $message,
51
        string $subject,
52
        string $text,
53
        ?string $html
54
    ): void {
55
        $addresses = $this->getAddresses($recipient, $message);
56
57
        foreach ($addresses as $address) {
58
            $phpMailer = clone $this->mailer;
59
            $phpMailer->Subject = $subject;
60
            $phpMailer->AddAddress($address, "First name last name");
61
62
            if ($this->getFromAddress() !== null) {
63
                $phpMailer->From = $this->getFromAddress();
64
                $phpMailer->FromName = $this->getFromName();
65
            }
66
67
            if ($html !== null) {
68
                $phpMailer->Body = $html;
69
                $phpMailer->AltBody = $text;
70
            } else {
71
                $phpMailer->Body = $text;
72
            }
73
74
            $phpMailer->send();
75
        }
76
    }
77
}
78