Completed
Pull Request — master (#1523)
by
unknown
07:15
created

Message::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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
	 * @var string
51
	 */
52
	private $name = '';
53
54
	/**
55
	 *
56
	 * @var Horde_Mail_Rfc822_List
57
	 */
58
	private $to;
59
60
	/**
61
	 * @var Horde_Mail_Rfc822_List
62
	 */
63
	private $cc;
64
65
	/**
66
	 * @var Horde_Mail_Rfc822_List
67
	 */
68
	private $bcc;
69
70
	/**
71
	 * @var IMessage
72
	 */
73
	private $repliedMessage = null;
74
75
	/**
76
	 * @var array
77
	 */
78
	private $flags = [];
79
80
	/**
81
	 * @var string
82
	 */
83
	private $content = '';
84
85
	/**
86
	 * @var File[]
87
	 */
88
	private $attachments = [];
89 24
90 24
	/**
91
	 * @param string $list
92
	 * @return Horde_Mail_Rfc822_List
93 40
	 */
94 40
	public static function parseAddressList($list) {
95 40
		return new Horde_Mail_Rfc822_List($list);
96 40
	}
97 40
98
	public function __construct() {
99
		$this->to = new Horde_Mail_Rfc822_List();
100
		$this->cc = new Horde_Mail_Rfc822_List();
101
		$this->bcc = new Horde_Mail_Rfc822_List();
102
	}
103
104
	/**
105
	 * Get the ID
106
	 *
107
	 * @return int|null
108
	 */
109
	public function getMessageId() {
110
		return null;
111
	}
112
113 2
	/**
114 2
	 * Get all flags set on this message
115
	 * 
116
	 * @return array
117
	 */
118
	public function getFlags() {
119
		return $this->flags;
120 2
	}
121 2
122 2
	/**
123
	 * @param string[] $flags
124
	 */
125
	public function setFlags(array $flags) {
126
		$this->flags = $flags;
127 2
	}
128 2
129
	/**
130
	 * @return string
131
	 */
132
	public function getFrom() {
133
		return $this->from;
134 2
	}
135 2
136 2
	/**
137
	 * @param string $from
138
	 */
139
	public function setFrom($from) {
140
		$this->from = $from;
141 4
	}
142 4
143 2
	/**
144
	 * @return string
145 2
	 */
146
	public function getName() {
147
		return $this->name;
148
	}
149
150
	/**
151 2
	 * @param string $name
152 2
	 */
153 2
	public function setName($name) {
154
		$this->name = $name;
155
	}
156
157
	/**
158
	 * @return string
159 4
	 */
160 4
	public function getTo() {
161
		if ($this->to->count() > 0) {
162
			return $this->to->first()->writeAddress();
163 4
		}
164
		return null;
165
	}
166
167
	/**
168
	 * @param Horde_Mail_Rfc822_List $to
169
	 */
170
	public function setTo(Horde_Mail_Rfc822_List $to) {
171 6
		$this->to = $to;
172 4
	}
173
174
	/**
175 6
	 * @param bool $assoc
176
	 * @return string[]
177
	 */
178
	public function getToList($assoc = false) {
179
		if ($assoc) {
180
			return $this->hordeListToAssocArray($this->to);
181
		} else {
182 2
			return $this->hordeListToStringArray($this->to);
183 2
		}
184 2
	}
185
186
	/**
187
	 * @param bool $assoc
188
	 * @return Horde_Mail_Rfc822_List
189
	 */
190 4
	public function getCCList($assoc = false) {
191 4
		if ($assoc) {
192
			return $this->hordeListToAssocArray($this->cc);
193
		} else {
194 4
			return $this->hordeListToStringArray($this->cc);
195
		}
196
	}
197
198
	/**
199
	 * @param Horde_Mail_Rfc822_List $cc
200
	 */
201 4
	public function setCC(Horde_Mail_Rfc822_List $cc) {
202 2
		$this->cc = $cc;
203 4
	}
204
205
	/**
206
	 * @param bool $assoc
207
	 * @return Horde_Mail_Rfc822_List
208 2
	 */
209 2
	public function getBCCList($assoc = false) {
210
		if ($assoc) {
211
			return $this->hordeListToAssocArray($this->bcc);
212
		} else {
213
			return $this->hordeListToStringArray($this->bcc);
214
		}
215 2
	}
216 2
217 2
	/**
218
	 * @param Horde_Mail_Rfc822_List $bcc
219
	 */
220
	public function setBcc(Horde_Mail_Rfc822_List $bcc) {
221
		$this->bcc = $bcc;
222 6
	}
223 6
224
	/**
225
	 * @return IMessage
226
	 */
227
	public function getRepliedMessage() {
228
		return $this->repliedMessage;
229 4
	}
230 4
231 4
	/**
232
	 * @param IMessage $message
233
	 */
234
	public function setRepliedMessage(IMessage $message) {
235
		$this->repliedMessage = $message;
236 2
	}
237 2
238
	/**
239
	 * @return string
240
	 */
241
	public function getSubject() {
242
		return $this->subject;
243 2
	}
244 2
245 2
	/**
246
	 * @param string $subject
247
	 */
248
	public function setSubject($subject) {
249
		$this->subject = $subject;
250 2
	}
251 2
252
	/**
253
	 * @return string
254
	 */
255
	public function getContent() {
256
		return $this->content;
257 2
	}
258 2
259 2
	/**
260 2
	 * @param string $content
261 2
	 */
262 2
	public function setContent($content) {
263 2
		$this->content = $content;
264 2
	}
265 2
266
	/**
267
	 * @return File[]
268
	 */
269
	public function getAttachments() {
270
		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...
271
	}
272
273
	/**
274
	 * @param File $file
275
	 */
276
	public function addAttachmentFromFiles(File $file) {
277
		$part = new Horde_Mime_Part();
278
		$part->setCharset('us-ascii');
279
		$part->setDisposition('attachment');
280
		$part->setName($file->getName());
281
		$part->setContents($file->getContent());
282
		$part->setType($file->getMimeType());
283
		$this->attachments[] = $part;
284
	}
285
286
}
287