SwiftEmail::getMessages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace JDR\MailerSwiftMailerBridge\Email;
4
5
use JDR\Mailer\Email\Address;
6
use JDR\Mailer\Email\Email;
7
use JDR\Mailer\Email\Message;
8
use Swift_Message;
9
10
/**
11
 * Email.
12
 */
13
class SwiftEmail implements Email
14
{
15
    /**
16
     * @var Swift_Message
17
     */
18
    private $message;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param Swift_Message|null $message
24
     */
25
    public function __construct(Swift_Message $message = null)
26
    {
27
        $this->message = $message ?? new Swift_Message();
28
    }
29
30
    /**
31
     * Get message.
32
     *
33
     * @return Swift_Message
34
     */
35
    public function getMessage()
36
    {
37
        return $this->message;
38
    }
39
40
    /**
41
     * Set the From addresses.
42
     *
43
     * @param Address[] $addresses
44
     *
45
     * @return self
46
     */
47
    public function setFrom(array $addresses): Email
48
    {
49
        $addresses = $this->transformAddressesToArray($addresses);
50
        $this->message->setFrom($addresses);
51
52
        return $this;
53
    }
54
55
    /**
56
     * Get the To addresses for this message.
57
     *
58
     * @return Address[]
59
     */
60
    public function getFrom(): array
61
    {
62
        $addresses = $this->message->getFrom();
63
64
        return $this->transformArrayToAddresses($addresses);
65
    }
66
67
    /**
68
     * Set the Reply-To addresses.
69
     *
70
     * Any replies from the receiver will be sent to this address.
71
     *
72
     * @param Address[] $addresses
73
     *
74
     * @return self
75
     */
76
    public function setReplyTo(array $addresses): Email
77
    {
78
        $addresses = $this->transformAddressesToArray($addresses);
79
        $this->message->setReplyTo($addresses);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Get the Reply-To addresses for this message.
86
     *
87
     * @return Address[]
88
     */
89
    public function getReplyTo(): array
90
    {
91
        $addresses = $this->message->getReplyTo();
92
93
        return $this->transformArrayToAddresses($addresses);
0 ignored issues
show
Documentation introduced by
$addresses is of type string, but the function expects a array<integer,object<JDR\Mailer\Email\Address>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
    }
95
96
    /**
97
     * Set the To addresses.
98
     *
99
     * Recipients set in this field will receive a copy of this message.
100
     *
101
     * @param Address[] $addresses
102
     *
103
     * @return self
104
     */
105
    public function setTo(array $addresses): Email
106
    {
107
        $addresses = $this->transformAddressesToArray($addresses);
108
        $this->message->setTo($addresses);
109
110
        return $this;
111
    }
112
113
    /**
114
     * Get the To addresses for this message.
115
     *
116
     * @return Address[]
117
     */
118
    public function getTo(): array
119
    {
120
        $addresses = $this->message->getTo();
121
122
        return $this->transformArrayToAddresses($addresses);
123
    }
124
125
    /**
126
     * Set the Cc addresses.
127
     *
128
     * Recipients set in this field will receive a 'carbon-copy' of this message.
129
     *
130
     * @param Address[] $addresses
131
     *
132
     * @return self
133
     */
134
    public function setCc(array $addresses): Email
135
    {
136
        $addresses = $this->transformAddressesToArray($addresses);
137
        $this->message->setCc($addresses);
138
139
        return $this;
140
    }
141
142
    /**
143
     * Get the Cc addresses for this message.
144
     *
145
     * @return Address[]
146
     */
147
    public function getCc(): array
148
    {
149
        $addresses = $this->message->getCc();
150
151
        return $this->transformArrayToAddresses($addresses);
152
    }
153
154
    /**
155
     * Set the Bcc addresses.
156
     *
157
     * Recipients set in this field will receive a 'blind-carbon-copy' of this message.
158
     *
159
     * @param Address[] $addresses
160
     *
161
     * @return self
162
     */
163
    public function setBcc(array $addresses): Email
164
    {
165
        $addresses = $this->transformAddressesToArray($addresses);
166
        $this->message->setBcc($addresses);
167
168
        return $this;
169
    }
170
171
    /**
172
     * Get the Bcc addresses for this message.
173
     *
174
     * @return Address[]
175
     */
176
    public function getBcc(): array
177
    {
178
        $addresses = $this->message->getBcc();
179
180
        return $this->transformArrayToAddresses($addresses);
181
    }
182
183
    /**
184
     * Set the sender of this message.
185
     *
186
     * @param Address $address
187
     *
188
     * @return self
189
     */
190
    public function setSender(Address $address): Email
191
    {
192
        $this->message->setSender($address->getEmail(), $address->getName());
193
194
        return $this;
195
    }
196
197
    /**
198
     * Get the sender address for this message.
199
     *
200
     * @return Address|null
201
     */
202
    public function getSender()
203
    {
204
        $addresses = $this->transformArrayToAddresses($this->message->getSender());
0 ignored issues
show
Documentation introduced by
$this->message->getSender() is of type string, but the function expects a array<integer,object<JDR\Mailer\Email\Address>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
205
206
        if ($address = reset($addresses)) {
207
            return $address;
208
        }
209
210
        return null;
211
    }
212
213
    /**
214
     * Set the bounce address for this message.
215
     *
216
     * @param Address $address
217
     */
218
    public function setBounce(Address $address): Email
219
    {
220
        $this->message->setReturnPath($address->getEmail(), $address->getName());
0 ignored issues
show
Unused Code introduced by
The call to Swift_Message::setReturnPath() has too many arguments starting with $address->getName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
221
222
        return $this;
223
    }
224
225
    /**
226
     * Get the bounce address for this message.
227
     *
228
     * @return Address|null
229
     */
230
    public function getBounce()
231
    {
232
        $addresses = $this->transformArrayToAddresses($this->message->getReturnPath());
0 ignored issues
show
Documentation introduced by
$this->message->getReturnPath() is of type string, but the function expects a array<integer,object<JDR\Mailer\Email\Address>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
233
234
        if ($address = reset($addresses)) {
235
            return $address;
236
        }
237
238
        return null;
239
    }
240
241
    /**
242
     * Set the subject of the message.
243
     *
244
     * @param string $subject
245
     *
246
     * @return self
247
     */
248
    public function setSubject($subject): Email
249
    {
250
        $this->message->setSubject($subject);
251
252
        return $this;
253
    }
254
255
    /**
256
     * Get the subject of the message.
257
     *
258
     * @return string
259
     */
260
    public function getSubject(): string
261
    {
262
        return $this->message->getSubject();
263
    }
264
265
    /**
266
     * Add message body to email.
267
     *
268
     * @param Message $message
269
     *
270
     * @return self
271
     */
272
    public function addMessage(Message $message): Email
273
    {
274
        if (null === $this->message->getBody()) {
275
            $this->message->setBody($message->getBody(), $message->getContentType());
276
277
            return $this;
278
        }
279
280
        $this->message->addPart($message->getBody(), $message->getContentType());
281
282
        return $this;
283
    }
284
285
    /**
286
     * Get all message body parts from the email.
287
     *
288
     * @return Message[]
289
     */
290
    public function getMessages(): array
291
    {
292
        $templates = [
293
            new Message($this->message->getBody(), $this->message->getContentType()),
294
        ];
295
296
        // TODO: Loop through parts
297
298
        return $templates;
299
    }
300
301
    /**
302
     * Transform addresses.
303
     *
304
     * Convert an array of Addresses to a format swift mailer can work with.
305
     *
306
     * @param Address[] $addresses
307
     *
308
     * @return array
309
     */
310
    private function transformAddressesToArray($addresses): array
311
    {
312
        $transformed = [];
313
        foreach ($addresses as $address) {
314
            if (null === $address->getName()) {
315
                $transformed[] = $address->getEmail();
316
317
                continue;
318
            }
319
320
            $transformed[$address->getEmail()] = $address->getName();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $transformed[$address->getEmail()] is correct as $address->getName() (which targets JDR\Mailer\Email\Address::getName()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
321
        }
322
323
        return $transformed;
324
    }
325
326
    /**
327
     * Transform addresses.
328
     *
329
     * Convert an array of Addresses to a format swift mailer can work with.
330
     *
331
     * @param Address[] $addresses
332
     *
333
     * @return array
334
     */
335
    private function transformArrayToAddresses($addresses): array
336
    {
337
        $transformed = [];
338
        foreach ($addresses as $email => $name) {
339
            $address = new Address($email, $name);
0 ignored issues
show
Documentation introduced by
$name is of type object<JDR\Mailer\Email\Address>, but the function expects a null|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
340
            $transformed[] = $address;
341
        }
342
343
        return $transformed;
344
    }
345
}
346