Message   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 4
Metric Value
wmc 17
c 11
b 0
f 4
lcom 1
cbo 1
dl 0
loc 239
ccs 50
cts 50
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getFrom() 0 4 1
A __clone() 0 4 1
A __construct() 0 6 1
A setCharset() 0 7 1
A getCharset() 0 4 1
A setFrom() 0 7 1
A setReplyTo() 0 6 1
A setTo() 0 7 1
A getTo() 0 4 1
A setSubject() 0 7 1
A getSubject() 0 4 1
A setText() 0 6 1
A getText() 0 4 1
A inHTML() 0 6 1
A getHeaders() 0 4 1
A setContentType() 0 9 2
1
<?php
2
/**
3
 * Sendmail package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2010, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT MIT
8
 */
9
10
namespace Sendmail;
11
12
use Sendmail\Message\Headers;
13
14
/**
15
 * Message.
16
 *
17
 * @author  Peter Gribanov <[email protected]>
18
 */
19
class Message
20
{
21
    /**
22
     * Message charset.
23
     *
24
     * @var string
25
     */
26
    protected $charset = Headers::DEFAULT_CHARSET;
27
28
    /**
29
     * In HTML format.
30
     *
31
     * @var bool
32
     */
33
    protected $in_html = false;
34
35
    /**
36
     * Headers.
37
     *
38
     * @var Headers
39
     */
40
    protected $headers;
41
42
    /**
43
     * From.
44
     *
45
     * @var string
46
     */
47
    protected $from = '';
48
49
    /**
50
     * To.
51
     *
52
     * @var string
53
     */
54
    protected $to = '';
55
56
    /**
57
     * Subject.
58
     *
59
     * @var string
60
     */
61
    protected $subject = '';
62
63
    /**
64
     * Message text.
65
     *
66
     * @var string
67
     */
68
    protected $text = '';
69
70
    /**
71
     * Construct.
72
     */
73 20
    public function __construct()
74
    {
75 20
        $this->headers = new Headers();
76 20
        $this->setCharset($this->charset)->setSubject('');
77 20
        $this->headers->set('MIME-Version', '1.0');
78 20
    }
79
80
    /**
81
     * Set message charset.
82
     *
83
     * @param string $charset
84
     *
85
     * @return self
86
     */
87 20
    public function setCharset($charset)
88
    {
89 20
        $this->charset = $charset;
90 20
        $this->headers->setCharset($charset);
91
92 20
        return $this->setContentType();
93
    }
94
95
    /**
96
     * Get message charset.
97
     *
98
     * @return string
99
     */
100 1
    public function getCharset()
101
    {
102 1
        return $this->charset;
103
    }
104
105
    /**
106
     * Send from E-mail.
107
     *
108
     * @param string $from
109
     * @param string $name
110
     *
111
     * @return self
112
     */
113 3
    public function setFrom($from, $name = '')
114
    {
115 3
        $this->from = $from;
116 3
        $this->headers->set('From', $this->headers->foramatName($from, $name));
117
118 3
        return $this;
119
    }
120
121
    /**
122
     * Get from E-mail.
123
     *
124
     * @return string
125
     */
126 1
    public function getFrom()
127
    {
128 1
        return $this->from;
129
    }
130
131
    /**
132
     * Set reply to E-mail.
133
     *
134
     * @param string $to
135
     * @param string $name
136
     *
137
     * @return self
138
     */
139 2
    public function setReplyTo($to, $name = '')
140
    {
141 2
        $this->headers->set('Reply-To', $this->headers->foramatName($to, $name));
142
143 2
        return $this;
144
    }
145
146
    /**
147
     * Send to E-mail.
148
     *
149
     * @param string $to
150
     *
151
     * @return self
152
     */
153 8
    public function setTo($to)
154
    {
155 8
        $this->to = $to;
156 8
        $this->headers->set('To', $to);
157
158 8
        return $this;
159
    }
160
161
    /**
162
     * Get to E-mail.
163
     *
164
     * @return string
165
     */
166 1
    public function getTo()
167
    {
168 1
        return $this->to;
169
    }
170
171
    /**
172
     * Set message subject.
173
     *
174
     * @param string $subject
175
     *
176
     * @return self
177
     */
178 20
    public function setSubject($subject)
179
    {
180 20
        $this->subject = $subject;
181 20
        $this->headers->set('Subject', $subject, true);
182
183 20
        return $this;
184
    }
185
186
    /**
187
     * Get message subject.
188
     *
189
     * @return string
190
     */
191 1
    public function getSubject()
192
    {
193 1
        return $this->subject;
194
    }
195
196
    /**
197
     * Set message text.
198
     *
199
     * @param string $text
200
     *
201
     * @return self
202
     */
203 1
    public function setText($text)
204
    {
205 1
        $this->text = $text;
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Get message text.
212
     *
213
     * @return string
214
     */
215 1
    public function getText()
216
    {
217 1
        return $this->text;
218
    }
219
220
    /**
221
     * Send E-mail in HTML format.
222
     *
223
     * @return self
224
     */
225 1
    public function inHTML()
226
    {
227 1
        $this->in_html = true;
228
229 1
        return $this->setContentType();
230
    }
231
232
    /**
233
     * @return string
234
     */
235 17
    public function getHeaders()
236
    {
237 17
        return $this->headers->toString();
238
    }
239
240 16
    public function __clone()
241
    {
242 16
        $this->headers = clone $this->headers;
243 16
    }
244
245
    /**
246
     * @return self
247
     */
248 20
    protected function setContentType()
249
    {
250 20
        $this->headers->set(
251 20
            'Content-type',
252 20
            'text/'.($this->in_html ? 'html' : 'plain').'; charset="'.$this->charset.'"'
253
        );
254
255 20
        return $this;
256
    }
257
}
258