Completed
Pull Request — master (#1605)
by Christoph
19:41
created

Message   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 26
c 1
b 1
f 0
lcom 2
cbo 1
dl 0
loc 233
ccs 65
cts 70
cp 0.9286
rs 10

22 Methods

Rating   Name   Duplication   Size   Complexity  
A parseAddressList() 0 3 1
A __construct() 0 5 1
A getMessageId() 0 3 1
A getFlags() 0 3 1
A setFlags() 0 3 1
A getFrom() 0 3 1
A setFrom() 0 3 1
A getTo() 0 6 2
A setTo() 0 3 1
A getToList() 0 7 2
A getCCList() 0 7 2
A setCC() 0 3 1
A getBCCList() 0 7 2
A setBcc() 0 3 1
A getRepliedMessage() 0 3 1
A setRepliedMessage() 0 3 1
A getSubject() 0 3 1
A setSubject() 0 3 1
A getContent() 0 3 1
A setContent() 0 3 1
A getAttachments() 0 3 1
A addAttachmentFromFiles() 0 9 1
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 *
6
 * 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
use Horde_Mail_Rfc822_List;
24
use Horde_Mime_Part;
25
use OCP\Files\File;
26
27
class Message implements IMessage {
28
29
	use ConvertAddresses;
30
31
	/**
32
	 * @var string
33
	 */
34
	private $subject = '';
35
36
	/**
37
	 * @var string
38
	 */
39
	private $from = '';
40
41
	/**
42
	 *
43
	 * @var Horde_Mail_Rfc822_List
44
	 */
45
	private $to;
46
47
	/**
48
	 * @var Horde_Mail_Rfc822_List
49
	 */
50
	private $cc;
51
52
	/**
53
	 * @var Horde_Mail_Rfc822_List
54
	 */
55
	private $bcc;
56
57
	/**
58
	 * @var IMessage
59
	 */
60
	private $repliedMessage = null;
61
62
	/**
63
	 * @var array
64
	 */
65
	private $flags = [];
66
67
	/**
68
	 * @var string
69
	 */
70
	private $content = '';
71
72
	/**
73
	 * @var File[]
74
	 */
75
	private $attachments = [];
76
77
	/**
78
	 * @param string $list
79
	 * @return Horde_Mail_Rfc822_List
80
	 */
81 24
	public static function parseAddressList($list) {
82 24
		return new Horde_Mail_Rfc822_List($list);
83
	}
84
85 40
	public function __construct() {
86 40
		$this->to = new Horde_Mail_Rfc822_List();
87 40
		$this->cc = new Horde_Mail_Rfc822_List();
88 40
		$this->bcc = new Horde_Mail_Rfc822_List();
89 40
	}
90
91
	/**
92
	 * Get the ID
93
	 *
94
	 * @return int|null
95
	 */
96
	public function getMessageId() {
97
		return null;
98
	}
99
100
	/**
101
	 * Get all flags set on this message
102
	 * 
103
	 * @return array
104
	 */
105 2
	public function getFlags() {
106 2
		return $this->flags;
107
	}
108
109
	/**
110
	 * @param string[] $flags
111
	 */
112 2
	public function setFlags(array $flags) {
113 2
		$this->flags = $flags;
114 2
	}
115
116
	/**
117
	 * @return string
118
	 */
119 2
	public function getFrom() {
120 2
		return $this->from;
121
	}
122
123
	/**
124
	 * @param string $from
125
	 */
126 2
	public function setFrom($from) {
127 2
		$this->from = $from;
128 2
	}
129
130
	/**
131
	 * @return string
132
	 */
133 4
	public function getTo() {
134 4
		if ($this->to->count() > 0) {
135 2
			return $this->to->first()->writeAddress();
136
		}
137 2
		return null;
138
	}
139
140
	/**
141
	 * @param Horde_Mail_Rfc822_List $to
142
	 */
143 2
	public function setTo(Horde_Mail_Rfc822_List $to) {
144 2
		$this->to = $to;
145 2
	}
146
147
	/**
148
	 * @param bool $assoc
149
	 * @return string[]
150
	 */
151 4
	public function getToList($assoc = false) {
152 4
		if ($assoc) {
153
			return $this->hordeListToAssocArray($this->to);
154
		} else {
155 4
			return $this->hordeListToStringArray($this->to);
156
		}
157
	}
158
159
	/**
160
	 * @param bool $assoc
161
	 * @return Horde_Mail_Rfc822_List
162
	 */
163 4
	public function getCCList($assoc = false) {
164 4
		if ($assoc) {
165
			return $this->hordeListToAssocArray($this->cc);
166
		} else {
167 4
			return $this->hordeListToStringArray($this->cc);
168
		}
169
	}
170
171
	/**
172
	 * @param Horde_Mail_Rfc822_List $cc
173
	 */
174 2
	public function setCC(Horde_Mail_Rfc822_List $cc) {
175 2
		$this->cc = $cc;
176 2
	}
177
178
	/**
179
	 * @param bool $assoc
180
	 * @return Horde_Mail_Rfc822_List
181
	 */
182 4
	public function getBCCList($assoc = false) {
183 4
		if ($assoc) {
184
			return $this->hordeListToAssocArray($this->bcc);
185
		} else {
186 4
			return $this->hordeListToStringArray($this->bcc);
187
		}
188
	}
189
190
	/**
191
	 * @param Horde_Mail_Rfc822_List $bcc
192
	 */
193 2
	public function setBcc(Horde_Mail_Rfc822_List $bcc) {
194 2
		$this->bcc = $bcc;
195 2
	}
196
197
	/**
198
	 * @return IMessage
199
	 */
200 2
	public function getRepliedMessage() {
201 2
		return $this->repliedMessage;
202
	}
203
204
	/**
205
	 * @param IMessage $message
206
	 */
207 2
	public function setRepliedMessage(IMessage $message) {
208 2
		$this->repliedMessage = $message;
209 2
	}
210
211
	/**
212
	 * @return string
213
	 */
214 6
	public function getSubject() {
215 6
		return $this->subject;
216
	}
217
218
	/**
219
	 * @param string $subject
220
	 */
221 4
	public function setSubject($subject) {
222 4
		$this->subject = $subject;
223 4
	}
224
225
	/**
226
	 * @return string
227
	 */
228 2
	public function getContent() {
229 2
		return $this->content;
230
	}
231
232
	/**
233
	 * @param string $content
234
	 */
235 2
	public function setContent($content) {
236 2
		$this->content = $content;
237 2
	}
238
239
	/**
240
	 * @return File[]
241
	 */
242 2
	public function getAttachments() {
243 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...
244
	}
245
246
	/**
247
	 * @param File $file
248
	 */
249 2
	public function addAttachmentFromFiles(File $file) {
250 2
		$part = new Horde_Mime_Part();
251 2
		$part->setCharset('us-ascii');
252 2
		$part->setDisposition('attachment');
253 2
		$part->setName($file->getName());
254 2
		$part->setContents($file->getContent());
255 2
		$part->setType($file->getMimeType());
256 2
		$this->attachments[] = $part;
257 2
	}
258
259
}
260