Test Failed
Push — master ( 4112a3...6b8d5c )
by Florian
05:27
created

Email::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 17
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Email;
15
16
use RuntimeException;
17
18
/**
19
 * Email
20
 *
21
 * This is a framework agnostic object that describes a single email
22
 */
23
class Email implements EmailInterface
24
{
25
    protected $messageId = null;
26
    protected $sender = null;
27
    protected $replyTo = null;
28
    protected $receivers = [];
29
    protected $bcc = [];
30
    protected $cc = [];
31
    protected $subject = '';
32
    protected $htmlContent = null;
33
    protected $textContent = null;
34
    protected $contentType = '';
35
    protected $attachments = [];
36
    protected $priority = Priority::NORMAL;
37
    protected $attributes = [];
38
    protected $headers = [];
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function setSender(EmailAddressInterface $email): EmailInterface
44
    {
45
        $this->sender = $email;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function setSubject(string $subject): EmailInterface
54
    {
55
        $this->subject = $subject;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function setReceivers(array $receivers): EmailInterface
64
    {
65
        foreach ($receivers as $receiver) {
66
            $this->addReceiver($receiver);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function setCc(array $cc): EmailInterface
76
    {
77
        foreach ($cc as $carbonCopy) {
78
            $this->addCc($carbonCopy);
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function setBcc(array $bcc): EmailInterface
88
    {
89
        foreach ($bcc as $blindCarbonCopy) {
90
            $this->addBcc($blindCarbonCopy);
91
        }
92
93
        return $this;
94
    }
95
96
    /**
97
     * @inheritDoc
98
     */
99
    public function setContentType(string $type): EmailInterface
100
    {
101
        $this->contentType = $type;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function setHtmlContent(?string $html): EmailInterface
110
    {
111
        $this->htmlContent = $html;
112
113
        return $this;
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function setTextContent(?string $text): EmailInterface
120
    {
121
        $this->textContent = $text;
122
123
        return $this;
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function setPriority(int $priority): EmailInterface
130
    {
131
        $this->priority = $priority;
132
133
        return $this;
134
    }
135
136
    /**
137
     * @inheritDoc
138
     */
139
    public function setMessageId(string $messageId): EmailInterface
140
    {
141
        $this->messageId = $messageId;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Set custom attributes for vendor specific things if needed
148
     *
149
     * @return \Phauthentic\Email\EmailInterface
150
     */
151
    public function setAttribute(string $name, $value): EmailInterface
152
    {
153
        $this->attributes[$name] = $value;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Set custom attributes
160
     *
161
     * @return \Phauthentic\Email\EmailInterface
162
     */
163
    public function setHeaders(array $headers): EmailInterface
164
    {
165
        $this->headers = array_merge($this->headers, $headers);
166
167
        return $this;
168
    }
169
170
    /**
171
     * @inheritDoc
172
     */
173
    public function getSender(): EmailAddressInterface
174
    {
175
        if (empty($this->sender)) {
176
            throw new RuntimeException('No sender was defined for this email');
177
        }
178
179
        return $this->sender;
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function getSubject(): string
186
    {
187
        return $this->subject;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function getReceivers(): array
194
    {
195
        if (empty($this->receivers)) {
196
            throw new RuntimeException('You must set at least one receiver for the email');
197
        }
198
199
        return $this->receivers;
200
    }
201
202
    /**
203
     * @inheritDoc
204
     */
205
    public function getCc(): array
206
    {
207
        return $this->cc;
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function getBcc(): array
214
    {
215
        return $this->bcc;
216
    }
217
218
    /**
219
     * @inheritDoc
220
     */
221
    public function getContentType(): string
222
    {
223
        return $this->contentType;
224
    }
225
226
    /**
227
     * @inheritDoc
228
     */
229
    public function getHtmlContent(): ?string
230
    {
231
        return $this->htmlContent;
232
    }
233
234
    /**
235
     * @inheritDoc
236
     */
237
    public function getTextContent(): ?string
238
    {
239
        return $this->textContent;
240
    }
241
242
    /**
243
     * @inheritDoc
244
     */
245
    public function addAttachment(AttachmentInterface $attachment): EmailInterface
246
    {
247
        $this->attachments[] = $attachment;
248
249
        return $this;
250
    }
251
252
    /**
253
     * @inheritDoc
254
     */
255
    public function getAttachments(): array
256
    {
257
        return $this->attachments;
258
    }
259
260
    /**
261
     * @inheritDoc
262
     */
263
    public function setReplyTo(EmailAddressInterface $email): EmailInterface
264
    {
265
        $this->replyTo = $email;
266
267
        return $this;
268
    }
269
270
    /**
271
     * @inheritDoc
272
     */
273
    public function addReceiver(EmailAddressInterface $email): EmailInterface
274
    {
275
        $this->receivers[] = $email;
276
277
        return $this;
278
    }
279
280
    /**
281
     * @inheritDoc
282
     */
283
    public function addCc(EmailAddressInterface $email): EmailInterface
284
    {
285
        $this->cc[] = $email;
286
287
        return $this;
288
    }
289
290
    /**
291
     * @inheritDoc
292
     */
293
    public function addBcc(EmailAddressInterface $email): EmailInterface
294
    {
295
        $this->bcc[] = $email;
296
297
        return $this;
298
    }
299
300
    /**
301
     * @inheritDoc
302
     */
303
    public function getReplyTo(): ?EmailAddressInterface
304
    {
305
        return $this->replyTo;
306
    }
307
308
    /**
309
     * @inheritDoc
310
     */
311
    public function getPriority(): int
312
    {
313
        return $this->priority;
314
    }
315
316
    /**
317
     * @inheritDoc
318
     */
319
    public function getMessageId()
320
    {
321
        if (empty($this->messageId)) {
322
            $this->messageId = time() . '.' . mt_srand((double)microtime() * 10000) . '@' . $_SERVER['HTTP_HOST'];
0 ignored issues
show
Bug introduced by
(double)microtime() * 10000 of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

322
            $this->messageId = time() . '.' . mt_srand(/** @scrutinizer ignore-type */ (double)microtime() * 10000) . '@' . $_SERVER['HTTP_HOST'];
Loading history...
Bug introduced by
Are you sure the usage of mt_srand((double)microtime() * 10000) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Bug introduced by
Are you sure mt_srand((double)microtime() * 10000) of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

322
            $this->messageId = time() . '.' . /** @scrutinizer ignore-type */ mt_srand((double)microtime() * 10000) . '@' . $_SERVER['HTTP_HOST'];
Loading history...
323
        }
324
325
        return $this->messageId;
326
    }
327
328
    /**
329
     * Gets a custom attribute
330
     *
331
     * @param string $name Attribute name
332
     * @return mixed
333
     */
334
    public function getAttribute(string $name)
335
    {
336
        if (isset($this->attributes[$name])) {
337
            return $this->attributes[$name];
338
        }
339
340
        return null;
341
    }
342
343
    /**
344
     * Gets a custom attribute
345
     *
346
     * @param string $name Attribute name
347
     * @return mixed
348
     */
349
    public function getHeaders(): array
350
    {
351
        return $this->headers;
352
    }
353
354
    /**
355
     * Returns an array representation of the email object
356
     *
357
     * @return array
358
     */
359
    public function toArray(): array
360
    {
361
        return [
362
            'messageId' => $this->messageId,
363
            'sender' => $this->sender,
364
            'replyTo' => $this->replyTo,
365
            'receivers' => $this->receivers,
366
            'bcc' => $this->bcc,
367
            'cc' => $this->cc,
368
            'subject' => $this->subject,
369
            'htmlContent' => $this->htmlContent,
370
            'textContent' => $this->textContent,
371
            'contentType' => $this->contentType,
372
            'attachments' => $this->attachments,
373
            'priority' => $this->priority,
374
            'attributes' => $this->attributes ,
375
            'headers' => $this->headers
376
        ];
377
    }
378
}
379