Completed
Push — master ( 0b821f...7f9113 )
by Christoph
11s
created

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