AbstractAdapter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
sendToRecipient() 0 7 ?
A send() 0 7 2
A getAddresses() 0 6 1
A getFromAddress() 0 4 1
A setFromAddress() 0 4 1
A getFromName() 0 4 1
A setFromName() 0 4 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
15
/**
16
 * A base class for email transports.
17
 */
18
abstract class AbstractAdapter implements AdapterInterface
19
{
20
    /**
21
     * The address used as a "from" address.
22
     *
23
     * @var string
24
     */
25
    private $fromAddress;
26
27
    /**
28
     * The name of the sender that belongs to the from address.
29
     *
30
     * @var string
31
     */
32
    private $fromName;
33
34
    /**
35
     * Sends a message over the transport.
36
     *
37
     * @param array $recipients A list with all recipients that should receive the message.
38
     * @param Message $message The message to send.
39
     * @param string $subject The subject for this message.
40
     * @param string $text The plain text template.
41
     * @param null|string $html The HTML template.
42
     * @return void
43
     */
44
    public function send(array $recipients, Message $message, string $subject, string $text, ?string $html): void
45
    {
46
        /** @var RecipientInterface $recipient */
47
        foreach ($recipients as $recipient) {
48
            $this->sendToRecipient($recipient, $message, $subject, $text, $html);
49
        }
50
    }
51
52
    /**
53
     * Sends a message to the given recipient.
54
     *
55
     * @param RecipientInterface $recipient The recipient that should receive the message.
56
     * @param Message $message The message that should be sent.
57
     * @param string $subject The subject of the message.
58
     * @param string $text The plain text message.
59
     * @param null|string $html An optional HTML version of the message.
60
     */
61
    abstract protected function sendToRecipient(
62
        RecipientInterface $recipient,
63
        Message $message,
64
        string $subject,
65
        string $text,
66
        ?string $html
67
    ): void;
68
69
    /**
70
     * Gets the addresses for this transport.
71
     *
72
     * @param RecipientInterface $recipient
73
     * @param Message $message
74
     * @return array
75
     */
76
    protected function getAddresses(RecipientInterface $recipient, Message $message): array
77
    {
78
        $parameters = (array)$message->getOption('recipient_parameters', []);
79
80
        return $recipient->getNotificationRecipientAddresses('email', $parameters);
81
    }
82
83
    /**
84
     * Gets the value of field "fromAddress".
85
     *
86
     * @return null|string
87
     */
88
    public function getFromAddress(): ?string
89
    {
90
        return $this->fromAddress;
91
    }
92
93
    /**
94
     * Sets the value of field "fromAddress".
95
     *
96
     * @param null|string $fromAddress
97
     */
98
    public function setFromAddress(?string $fromAddress)
99
    {
100
        $this->fromAddress = $fromAddress;
101
    }
102
103
    /**
104
     * Gets the value of field "fromName".
105
     *
106
     * @return null|string
107
     */
108
    public function getFromName(): ?string
109
    {
110
        return $this->fromName;
111
    }
112
113
    /**
114
     * Sets the value of field "fromName".
115
     *
116
     * @param null|string $fromName
117
     */
118
    public function setFromName(?string $fromName)
119
    {
120
        $this->fromName = $fromName;
121
    }
122
}
123