Completed
Push — master ( bd9b49...e48eea )
by Walter
13:41
created

ZendMail::__construct()   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\Adapter;
11
12
use Communicator\Message;
13
use Communicator\Recipient\RecipientInterface;
14
use Zend\Mail\Message as ZendMessage;
15
use Zend\Mail\Transport\TransportInterface;
16
use Zend\Mime\Message as MimeMessage;
17
use Zend\Mime\Part;
18
19
/**
20
 * An e-mail transport that makes use of Zend\Mail.
21
 */
22
final class ZendMail extends AbstractAdapter
23
{
24
    /**
25
     * The mailer used to send messages.
26
     *
27
     * @var TransportInterface
28
     */
29
    private $transport;
30
31
    /**
32
     * Initializes a new instance of this class.
33
     *
34
     * @param TransportInterface $transport
35
     */
36
    public function __construct(TransportInterface $transport)
37
    {
38
        $this->transport = $transport;
39
    }
40
41
    /**
42
     * Sends a message to the given recipient.
43
     *
44
     * @param RecipientInterface $recipient The recipient that should receive the message.
45
     * @param Message $message The message that should be sent.
46
     * @param string $subject The subject of the message.
47
     * @param string $text The plain text message.
48
     * @param null|string $html An optional HTML version of the message.
49
     */
50
    protected function sendToRecipient(
51
        RecipientInterface $recipient,
52
        Message $message,
53
        string $subject,
54
        string $text,
55
        ?string $html
56
    ): void {
57
        $addresses = $this->getAddresses($recipient, $message);
58
59
        foreach ($addresses as $address) {
60
            /** @var ZendMessage $emailMessage */
61
            $emailMessage = new ZendMessage();
62
            $emailMessage->setSubject($subject);
63
            $emailMessage->setTo($address);
64
            $emailMessage->setBody($text);
65
            $emailMessage->setEncoding('UTF-8');
66
67
            if ($this->getFromAddress() !== null) {
68
                $emailMessage->setFrom($this->getFromAddress(), $this->getFromName());
69
            }
70
71
            $textPart = new Part($text);
72
            $textPart->type = 'text/plain';
73
74
            $bodyPart = new MimeMessage();
75
            $bodyPart->addPart($textPart);
76
77
            if ($html) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $html of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
78
                $htmlPart = new Part($html);
79
                $htmlPart->type = 'text/html';
80
81
                $bodyPart->addPart($htmlPart);
82
            }
83
84
            $this->transport->send($emailMessage);
85
        }
86
    }
87
}
88