PHPMailer   A
last analyzed

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