Completed
Push — master ( 947cff...af97d0 )
by Nikola
04:03
created

EmailMessage::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Notifier\Channel\Email;
6
7
class EmailMessage
8
{
9
    private $headers = [];
10
    private $subject = '';
11
    private $body = '';
12
    private $contentType = 'text/plain';
13
14 1
    public function getHeaders(): array
15
    {
16 1
        return $this->headers;
17
    }
18
19 5
    public function from(string $email, string $name = null)
20
    {
21 5
        $this->headers['From'][] = $this->createAddress($email, $name);
22
23 5
        return $this;
24
    }
25
26
    public function getFrom(): array
27
    {
28
        return $this->headers['From'] ?? [];
29
    }
30
31 1
    public function sender(string $email, string $name = null)
32
    {
33 1
        $this->headers['Sender'] = $this->createAddress($email, $name);
34
35 1
        return $this;
36
    }
37
38
    public function getSender(): string
39
    {
40
        return $this->headers['Sender'] ?? '';
41
    }
42
43 1
    public function replyTo(string $email, string $name = null)
44
    {
45 1
        $this->headers['Reply-To'][] = $this->createAddress($email, $name);
46
47 1
        return $this;
48
    }
49
50
    public function getReplyTo(): array
51
    {
52
        return $this->headers['Reply-To'] ?? [];
53
    }
54
55 4
    public function to(string $email, string $name = null)
56
    {
57 4
        $this->headers['To'][] = $this->createAddress($email, $name);
58
59 4
        return $this;
60
    }
61
62 4
    public function getTo(): array
63
    {
64 4
        return $this->headers['To'] ?? [];
65
    }
66
67 1
    public function cc(string $email, string $name = null)
68
    {
69 1
        $this->headers['Cc'][] = $this->createAddress($email, $name);
70
71 1
        return $this;
72
    }
73
74
    public function getCc(): array
75
    {
76
        return $this->headers['Cc'] ?? [];
77
    }
78
79 1
    public function bcc(string $email, string $name = null)
80
    {
81 1
        $this->headers['Bcc'][] = $this->createAddress($email, $name);
82
83 1
        return $this;
84
    }
85
86
    public function getBcc(): array
87
    {
88
        return $this->headers['Bcc'] ?? [];
89
    }
90
91 5
    public function subject(string $subject)
92
    {
93 5
        $this->subject = $subject;
94
95 5
        return $this;
96
    }
97
98 1
    public function getSubject(): string
99
    {
100 1
        return $this->subject;
101
    }
102
103 5
    public function textBody(string $textBody)
104
    {
105 5
        $this->body = $textBody;
106
107 5
        return $this;
108
    }
109
110
    public function htmlBody(string $htmlBody)
111
    {
112
        $this->body = $htmlBody;
113
        $this->headers['MIME-Version'] = '1.0';
114
        $this->headers['Content-type'] = 'text/html; charset=utf-8';
115
116
        return $this;
117
    }
118
119 2
    public function getBody(): string
120
    {
121 2
        return $this->body;
122
    }
123
124
    public function getContentType(): string
125
    {
126
        return $this->contentType;
127
    }
128
129 5
    private function createAddress(string $email, ?string $name): string
130
    {
131 5
        return (null !== $name) ? $name . ' <' . $email . '>' : $email;
132
    }
133
}
134