Completed
Pull Request — master (#1536)
by
unknown
09:24
created

Message::parseAddressList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 *
5
 * ownCloud - Mail
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 *
19
 */
20
namespace OCA\Mail\Model;
21
22
/**
23
 * ownCloud - Mail
24
 *
25
 * This file is licensed under the Affero General Public License version 3 or
26
 * later. See the COPYING file.
27
 *
28
 * @author Christoph Wurst <[email protected]>
29
 * @copyright Christoph Wurst 2015
30
 */
31
use Horde_Mail_Rfc822_List;
32
use Horde_Mime_Part;
33
use OCP\Files\File;
34
35
class Message implements IMessage {
36
37
	use ConvertAddresses;
38
39
	/**
40
	 * @var string
41
	 */
42
	private $subject = '';
43
44
	/**
45
	 * @var string
46
	 */
47
	private $from = '';
48
49
	/**
50
	 *
51
	 * @var Horde_Mail_Rfc822_List
52
	 */
53
	private $to;
54
55
	/**
56
	 * @var Horde_Mail_Rfc822_List
57
	 */
58
	private $cc;
59
60
	/**
61
	 * @var Horde_Mail_Rfc822_List
62
	 */
63
	private $bcc;
64
65
	/**
66
	 * @var IMessage
67
	 */
68
	private $repliedMessage = null;
69
70
	/**
71
	 * @var array
72
	 */
73
	private $flags = [];
74
75
	/**
76
	 * @var string
77
	 */
78
	private $content = '';
79
80
	/**
81
	 * @var File[]
82
	 */
83
	private $attachments = [];
84
85
	/**
86
	 * @var string
87
	 */
88
	private $type;
89
90
	/**
91
	 * @param string $list
92
	 * @return Horde_Mail_Rfc822_List
93
	 */
94 24
	public static function parseAddressList($list) {
95 24
		return new Horde_Mail_Rfc822_List($list);
96
	}
97
98 40
	public function __construct() {
99 40
		$this->to = new Horde_Mail_Rfc822_List();
100 40
		$this->cc = new Horde_Mail_Rfc822_List();
101 40
		$this->bcc = new Horde_Mail_Rfc822_List();
102 40
	}
103
104
	/**
105
	 * Get the ID
106
	 *
107
	 * @return int|null
108
	 */
109
	public function getMessageId() {
110
		return null;
111
	}
112
113
	/**
114
	 * Get all flags set on this message
115
	 *
116
	 * @return array
117
	 */
118 2
	public function getFlags() {
119 2
		return $this->flags;
120
	}
121
122
	/**
123
	 * @param string[] $flags
124
	 */
125 2
	public function setFlags(array $flags) {
126 2
		$this->flags = $flags;
127 2
	}
128
129
	/**
130
	 * @return string
131
	 */
132 2
	public function getFrom() {
133 2
		return $this->from;
134
	}
135
136
	/**
137
	 * @param string $from
138
	 */
139 2
	public function setFrom($from) {
140 2
		$this->from = $from;
141 2
	}
142
143
	/**
144
	 * @return string
145
	 */
146 4
	public function getTo() {
147 4
		if ($this->to->count() > 0) {
148 2
			return $this->to->first()->writeAddress();
149
		}
150 2
		return null;
151
	}
152
153
	/**
154
	 * @param Horde_Mail_Rfc822_List $to
155
	 */
156 2
	public function setTo(Horde_Mail_Rfc822_List $to) {
157 2
		$this->to = $to;
158 2
	}
159
160
	/**
161
	 * @param bool $assoc
162
	 * @return string[]
163
	 */
164 4
	public function getToList($assoc = false) {
165 4
		if ($assoc) {
166
			return $this->hordeListToAssocArray($this->to);
167
		} else {
168 4
			return $this->hordeListToStringArray($this->to);
169
		}
170
	}
171
172
	/**
173
	 * @param bool $assoc
174
	 * @return Horde_Mail_Rfc822_List
175
	 */
176 4
	public function getCCList($assoc = false) {
177 4
		if ($assoc) {
178
			return $this->hordeListToAssocArray($this->cc);
179
		} else {
180 4
			return $this->hordeListToStringArray($this->cc);
181
		}
182
	}
183
184
	/**
185
	 * @param Horde_Mail_Rfc822_List $cc
186
	 */
187 2
	public function setCC(Horde_Mail_Rfc822_List $cc) {
188 2
		$this->cc = $cc;
189 2
	}
190
191
	/**
192
	 * @param bool $assoc
193
	 * @return Horde_Mail_Rfc822_List
194
	 */
195 4
	public function getBCCList($assoc = false) {
196 4
		if ($assoc) {
197
			return $this->hordeListToAssocArray($this->bcc);
198
		} else {
199 4
			return $this->hordeListToStringArray($this->bcc);
200
		}
201
	}
202
203
	/**
204
	 * @param Horde_Mail_Rfc822_List $bcc
205
	 */
206 2
	public function setBcc(Horde_Mail_Rfc822_List $bcc) {
207 2
		$this->bcc = $bcc;
208 2
	}
209
210
	/**
211
	 * @return IMessage
212
	 */
213 2
	public function getRepliedMessage() {
214 2
		return $this->repliedMessage;
215
	}
216
217
	/**
218
	 * @param IMessage $message
219
	 */
220 2
	public function setRepliedMessage(IMessage $message) {
221 2
		$this->repliedMessage = $message;
222 2
	}
223
224
	/**
225
	 * @return string
226
	 */
227 6
	public function getSubject() {
228 6
		return $this->subject;
229
	}
230
231
	/**
232
	 * @param string $subject
233
	 */
234 4
	public function setSubject($subject) {
235 4
		$this->subject = $subject;
236 4
	}
237
238
	/**
239
	 * @return string
240
	 */
241 2
	public function getContent() {
242 2
		return $this->content;
243
	}
244
245
	/**
246
	 * @param string $content
247
	 */
248 2
	public function setContent($content) {
249 2
		$this->content = $content;
250 2
	}
251
252
	/**
253
	 * @return File[]
254
	 */
255 2
	public function getAttachments() {
256 2
		return $this->attachments;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->attachments; (OCP\Files\File[]) is incompatible with the return type declared by the interface OCA\Mail\Model\IMessage::getAttachments of type OCA\Mail\Model\Horde_Mime_Part[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
257
	}
258
259
	/**
260
	 * @param File $file
261
	 */
262 2
	public function addAttachmentFromFiles(File $file) {
263 2
		$part = new Horde_Mime_Part();
264 2
		$part->setCharset('us-ascii');
265 2
		$part->setDisposition('attachment');
266 2
		$part->setName($file->getName());
267 2
		$part->setContents($file->getContent());
268 2
		$part->setType($file->getMimeType());
269 2
		$this->attachments[] = $part;
270 2
	}
271
272
	/**
273
	 * @param string $type
274
	 */
275
	public function setType($type) {
276
		$this->type = $type;
277
	}
278
279
	/**
280
	 * @return string
281
	 */
282
	public function getType() {
283
		return $this->type;
284
	}
285
286
}
287