Completed
Pull Request — master (#1277)
by Christoph
42:07
created

Message::getBCCList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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