Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

Email   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Test Coverage

Coverage 96.39%

Importance

Changes 0
Metric Value
wmc 32
dl 0
loc 285
ccs 80
cts 83
cp 0.9639
rs 9.6
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setBody() 0 3 1
B factory() 0 23 4
A getHeaders() 0 2 1
A getSubject() 0 2 1
A getParams() 0 2 1
A getBody() 0 2 1
A getTo() 0 2 1
A setHeaders() 0 3 1
A setSubject() 0 3 1
A setFrom() 0 3 1
A addAttachment() 0 18 4
A setTo() 0 3 1
A getAttachments() 0 2 1
A addHeader() 0 3 1
A getFrom() 0 2 1
A setParams() 0 3 1
B prepareFrom() 0 19 6
A prepareTo() 0 12 4
1
<?php
2
3
namespace Elgg;
4
5
use ElggEntity;
6
use ElggUser;
7
use InvalidParameterException;
8
use Elgg\Email\Address;
9
use Elgg\Email\Attachment;
10
use Zend\Mime\Part;
11
12
/**
13
 * Email message
14
 */
15
final class Email {
16
17
	/**
18
	 * @var \Elgg\Email\Address
19
	 */
20
	protected $from;
21
22
	/**
23
	 * @var \Elgg\Email\Address
24
	 */
25
	protected $to;
26
27
	/**
28
	 * @var string
29
	 */
30
	protected $subject;
31
32
	/**
33
	 * @var string
34
	 */
35
	protected $body;
36
37
	/**
38
	 * @var array
39
	 */
40
	protected $params = [];
41
42
	/**
43
	 * @var array
44
	 */
45
	protected $headers = [];
46
	
47
	/**
48
	 * @var Part[]
49
	 */
50
	protected $attachments = [];
51
52
	/**
53
	 * Create an email instance form an array of options
54
	 *
55
	 * @param array $options Options
56
	 *                       'from' - ElggEntity, or email string, or \Elgg\Email\Address
57
	 *                       'to' - ElggEntity, or email string, or \Elgg\Email\Address
58
	 *                       'subject' - subject string
59
	 *                       'body' - body string
60
	 *                       'params' - additional parameters
61
	 *                       'headers' - HTTP/IMF headers
62
	 * @return \Elgg\Email
63
	 */
64 12
	public static function factory(array $options = []) {
65 12
		$from = elgg_extract('from', $options);
66 12
		$to = elgg_extract('to', $options);
67 12
		$subject = elgg_extract('subject', $options);
68 12
		$body = elgg_extract('body', $options);
69 12
		$params = elgg_extract('params', $options, []);
70 12
		$headers = elgg_extract('headers', $options, []);
71
72 12
		$email = new self();
73 12
		$email->setFrom(self::prepareFrom($from));
74 12
		$email->setTo(self::prepareTo($to));
75 12
		$email->setSubject($subject);
76 12
		$email->setBody($body);
77 12
		$email->setParams($params);
78 12
		$email->setHeaders($headers);
79
		
80 12
		if (isset($params['attachments']) && is_array($params['attachments'])) {
81 3
			foreach ($params['attachments'] as $attachment) {
82 3
				$email->addAttachment($attachment);
83
			}
84
		}
85
		
86 12
		return $email;
87
	}
88
89
	/**
90
	 * Sets sender address
91
	 *
92
	 * @param \Elgg\Email\Address $from Sender address
93
	 * @return self
94
	 */
95 12
	public function setFrom(Address $from) {
96 12
		$this->from = $from;
97 12
		return $this;
98
	}
99
100
	/**
101
	 * Returns sender address
102
	 * @return \Elgg\Email\Address
103
	 */
104 9
	public function getFrom() {
105 9
		return $this->from;
106
	}
107
108
	/**
109
	 * Sets recipient address
110
	 *
111
	 * @param \Elgg\Email\Address $to Recipient address
112
	 * @return self
113
	 */
114 12
	public function setTo(Address $to) {
115 12
		$this->to = $to;
116 12
		return $this;
117
	}
118
119
	/**
120
	 * Returns recipient address
121
	 * @return \Elgg\Email\Address
122
	 */
123 9
	public function getTo() {
124 9
		return $this->to;
125
	}
126
127
	/**
128
	 * Sets email subject
129
	 *
130
	 * @param string $subject Subject
131
	 * @return self
132
	 */
133 12
	public function setSubject($subject = '') {
134 12
		$this->subject = $subject;
135 12
		return $this;
136
	}
137
138
	/**
139
	 * Returns the subject
140
	 * @return string
141
	 */
142 5
	public function getSubject() {
143 5
		return $this->subject;
144
	}
145
146
	/**
147
	 * Sets the email message body
148
	 *
149
	 * @param string $body Body
150
	 * @return self
151
	 */
152 12
	public function setBody($body = '') {
153 12
		$this->body = $body;
154 12
		return $this;
155
	}
156
157
	/**
158
	 * Returns email body
159
	 * @return string
160
	 */
161 5
	public function getBody() {
162 5
		return $this->body;
163
	}
164
165
	/**
166
	 * Sets additional params
167
	 *
168
	 * @param array $params Params
169
	 * @return self
170
	 */
171 12
	public function setParams(array $params = []) {
172 12
		$this->params = $params;
173 12
		return $this;
174
	}
175
176
	/**
177
	 * Returns additional params
178
	 * @return array
179
	 */
180 4
	public function getParams() {
181 4
		return $this->params;
182
	}
183
184
	/**
185
	 * Adds/replaces an HTTP/IMF header
186
	 *
187
	 * @param string $name  Header name
188
	 * @param mixed  $value Header value
189
	 * @return self
190
	 */
191 3
	public function addHeader($name, $value) {
192 3
		$this->headers[$name] = $value;
193 3
		return $this;
194
	}
195
196
	/**
197
	 * Replaces header bag
198
	 *
199
	 * @param array $headers Headers
200
	 * @return self
201
	 */
202 12
	public function setHeaders(array $headers = []) {
203 12
		$this->headers = $headers;
204 12
		return $this;
205
	}
206
207
	/**
208
	 * Returns headers
209
	 * @return array
210
	 */
211 5
	public function getHeaders() {
212 5
		return $this->headers;
213
	}
214
	
215
	/**
216
	 * Add an attachment
217
	 *
218
	 * @param mixed $attachment \Zend\Mime\Part or \Elgg\Email\Attachment or \ElggFile or an array
219
	 *
220
	 * @see \Elgg\Email\Attachment::factory()
221
	 *
222
	 * @return self
223
	 */
224 7
	public function addAttachment($attachment) {
225
		
226 7
		if ($attachment instanceof Part) {
227 1
			$this->attachments[] = $attachment;
228 1
			return $this;
229
		}
230
		
231 6
		if ($attachment instanceof \ElggFile) {
232 3
			$this->attachments[] = Attachment::fromElggFile($attachment);
233 3
			return $this;
234
		}
235
		
236 5
		$attachment = Attachment::factory($attachment);
237 5
		if (!empty($attachment)) {
238 4
			$this->attachments[] = $attachment;
239
		}
240
		
241 5
		return $this;
242
	}
243
	
244
	/**
245
	 * Get all attachments
246
	 *
247
	 * @return \Zend\Mime\Part[]
248
	 */
249 11
	public function getAttachments() {
250 11
		return $this->attachments;
251
	}
252
253
	/**
254
	 * Converts mixed input to an instance of Zend addres
255
	 *
256
	 * @param mixed $from From input
257
	 * @return Address
258
	 * @throws InvalidParameterException
259
	 */
260 12
	protected static function prepareFrom($from) {
261 12
		if ($from instanceof ElggEntity) {
262
			// If there's an email address, use it - but only if it's not from a user.
263 3
			if (!$from instanceof ElggUser && $from->email) {
264
				$from = new Address($from->email, $from->getDisplayName());
0 ignored issues
show
Bug introduced by Ismayil Khayredinov
It seems like $from->email can also be of type array; however, parameter $email of Elgg\Email\Address::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

264
				$from = new Address(/** @scrutinizer ignore-type */ $from->email, $from->getDisplayName());
Loading history...
265
			} else {
266
				// get the site email address
267 3
				$site = elgg_get_site_entity();
268 3
				$from = new Address($site->getEmailAddress(), $site->getDisplayName());
269
			}
270 9
		} elseif (is_string($from)) {
271 8
			$from = Address::fromString($from);
272
		}
273
		
274 12
		if (!$from instanceof Address) {
275
			throw new InvalidParameterException("From address is not in a valid format");
276
		}
277
278 12
		return $from;
279
	}
280
281
	/**
282
	 * Converts mixed input to an instance of Zend addres
283
	 *
284
	 * @param mixed $to To input
285
	 * @return Address
286
	 * @throws InvalidParameterException
287
	 */
288 12
	protected static function prepareTo($to) {
289 12
		if ($to instanceof ElggEntity) {
290 3
			$to = new Address($to->email, $to->getDisplayName());
0 ignored issues
show
Bug introduced by Ismayil Khayredinov
It seems like $to->email can also be of type array; however, parameter $email of Elgg\Email\Address::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

290
			$to = new Address(/** @scrutinizer ignore-type */ $to->email, $to->getDisplayName());
Loading history...
291 9
		} elseif (is_string($to)) {
292 8
			$to = Address::fromString($to);
293
		}
294
295 12
		if (!$to instanceof Address) {
296
			throw new InvalidParameterException("To address is not in a valid format");
297
		}
298
299 12
		return $to;
300
	}
301
}
302