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

ZendMail   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 7 2
B sendToRecipient() 0 37 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 Zend\Mail\Message as ZendMessage;
15
use Zend\Mail\Transport\TransportInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Communicator\Transport\E...port\TransportInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 AbstractTransport
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 over the transport.
43
     *
44
     * @param array $recipients A list with all recipients that should receive the message.
45
     * @param Message $message The message to send.
46
     * @param string $subject The subject for this message.
47
     * @param string $text The plain text template.
48
     * @param null|string $html The HTML template.
49
     * @return void
50
     */
51
    public function send(array $recipients, Message $message, string $subject, string $text, ?string $html): void
52
    {
53
        /** @var RecipientInterface $recipient */
54
        foreach ($recipients as $recipient) {
55
            $this->sendToRecipient($recipient, $message, $subject, $text, $html);
56
        }
57
    }
58
59
    /**
60
     * Sends a message to the given recipient.
61
     *
62
     * @param RecipientInterface $recipient The recipient that should receive the message.
63
     * @param Message $message The message that should be sent.
64
     * @param string $subject The subject of the message.
65
     * @param string $text The plain text message.
66
     * @param null|string $html An optional HTML version of the message.
67
     */
68
    private function sendToRecipient(
69
        RecipientInterface $recipient,
70
        Message $message,
71
        string $subject,
72
        string $text,
73
        ?string $html
74
    ): void {
75
        $addresses = $this->getAddresses($recipient, $message);
76
77
        foreach ($addresses as $address) {
78
            /** @var ZendMessage $emailMessage */
79
            $emailMessage = new ZendMessage();
80
            $emailMessage->setSubject($subject);
81
            $emailMessage->setTo($address);
82
            $emailMessage->setBody($text);
83
            $emailMessage->setEncoding('UTF-8');
84
85
            if ($this->getFromAddress() !== null) {
86
                $emailMessage->setFrom($this->getFromAddress(), $this->getFromName());
87
            }
88
89
            $textPart = new Part($text);
90
            $textPart->type = 'text/plain';
91
92
            $bodyPart = new MimeMessage();
93
            $bodyPart->addPart($textPart);
94
95
            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...
96
                $htmlPart = new Part($html);
97
                $htmlPart->type = 'text/html';
98
99
                $bodyPart->addPart($htmlPart);
100
            }
101
102
            $this->transport->send($emailMessage);
103
        }
104
    }
105
}
106