Completed
Push — master ( 4115f0...fecdd4 )
by
unknown
16:34
created

UnifiedAccount::saveDraft()   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
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * @author Christoph Wurst <[email protected]>
4
 * @author Thomas Müller <[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\Service;
22
23
use OCP\IL10N;
24
use OCA\Mail\Model\IMessage;
25
26
class UnifiedAccount implements IAccount {
27
28
	const ID = -1;
29
	const INBOX_ID = 'all-inboxes';
30
31
	/** @var AccountService */
32
	private $accountService;
33
34
	/** @var string */
35
	private $userId;
36
37
	/** @var IL10N */
38
	private $l10n;
39
40
	/** @var Horde_Mail_Rfc822_List */
41
	private $email;
42
43
	/**
44
	 * @param AccountService $accountService
45
	 * @param string $userId
46
	 * @param IL10N $l10n
47
	 */
48 1
	public function __construct(AccountService $accountService, $userId, IL10N $l10n) {
49 1
		$this->accountService = $accountService;
50 1
		$this->userId = $userId;
51 1
		$this->l10n = $l10n;
52 1
	}
53
54
	/**
55
	 * @return array
56
	 */
57
	public function getConfiguration() {
58
		return [
59
			'accountId' => UnifiedAccount::ID,
60
		];
61
	}
62
63
	/**
64
	 * @return array
65
	 * TODO: function name is :hankey:
66
	 */
67
	public function getListArray() {
68
		$inbox = $this->buildInbox();
69
		return [
70
			'id'             => UnifiedAccount::ID,
71
			'email'          => '',
72
			'folders'        => [$inbox],
73
			'specialFolders' => [],
74
			'delimiter' => '.',
75
		];
76
	}
77
78
	private function buildInbox() {
79
		$displayName = (string)$this->l10n->t('All inboxes');
80
81
		$allAccounts = $this->accountService->findByUserId($this->userId);
82
83
		$uidValidity = [];
84
		$uidNext = [];
85
		$unseen = 0;
86
87
		foreach($allAccounts as $account) {
88
			/** @var IAccount $account */
89
			$inbox = $account->getInbox();
90
			if (is_null($inbox)) {
91
				continue;
92
			}
93
94
			$status = $inbox->getStatus();
95
			$unseen += isset($status['unseen']) ? $status['unseen'] : 0;
96
			$uidValidity[$account->getId()] = isset($status['uidvalidity']) ? $status['uidvalidity'] : 0;
97
			$uidNext[$account->getId()] = isset($status['uidnext']) ? $status['uidnext'] : 0;
98
		}
99
100
		return [
101
			'id' => base64_encode(self::INBOX_ID),
102
			'parent' => null,
103
			'name' => $displayName,
104
			'specialRole' => 'inbox',
105
			'unseen' => $unseen,
106
			'total' => 100,
107
			'isEmpty' => false,
108
			'accountId' => UnifiedAccount::ID,
109
			'noSelect' => false,
110
			'uidvalidity' => $uidValidity,
111
			'uidnext' => $uidNext,
112
			'delimiter' => '.'
113
		];
114
	}
115
116
	/**
117
	 * @param $folderId
118
	 * @return IMailBox
119
	 */
120
	public function getMailbox($folderId) {
121
		return new UnifiedMailbox($this->accountService, $this->userId);
122
	}
123
124
	/**
125
	 * @return string
126
	 */
127
	public function getEmail() {
128
		if ($this->email === null) {
129
			$allAccounts = $this->accountService->findByUserId($this->userId);
130
			$addressesList = new \Horde_Mail_Rfc822_List();
131
			foreach ($allAccounts as $account) {
132
				$inbox = $account->getInbox();
133
				if (is_null($inbox)) {
134
					continue;
135
				}
136
				$addressesList->add($account->getEmail());
137
			}
138
			$this->email = $addressesList;
0 ignored issues
show
Documentation Bug introduced by
It seems like $addressesList of type object<Horde_Mail_Rfc822_List> is incompatible with the declared type object<OCA\Mail\Service\Horde_Mail_Rfc822_List> of property $email.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
139
		}
140
		return $this->email;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->email; (Horde_Mail_Rfc822_List|O...\Horde_Mail_Rfc822_List) is incompatible with the return type declared by the interface OCA\Mail\Service\IAccount::getEmail of type string.

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...
141
	}
142
143
	/**
144
	 * @param IMessage $message
145
	 * @param int|null $draftUID
146
	 */
147
	public function sendMessage(IMessage $message, $draftUID) {
148
		throw new Exception('Not implemented');
149
	}
150
151
	/**
152
	 * @param IMessage $message
153
	 * @param int|null $previousUID
154
	 * @return int
155
	 */
156
	public function saveDraft(IMessage $message, $previousUID) {
157
		throw new Exception('Not implemented');
158
	}
159
160
	/**
161
	 * @param string $folderId
162
	 * @param string $messageId
163
	 */
164
	public function deleteMessage($folderId, $messageId) {
165
		$data = json_decode(base64_decode($messageId), true);
166
		$account = $this->accountService->find($this->userId, $data[0]);
167
		$inbox = $account->getInbox();
168
		$messageId = $data[1];
169
170
		$account->deleteMessage($inbox->getFolderId(), $messageId);
171
	}
172
173
	/**
174
	 * @param string[] $query
175
	 * @return array
176
	 */
177
	public function getChangedMailboxes($query) {
178
		$accounts = $this->accountService->findByUserId($this->userId);
179
		$changedBoxes = [];
180
181
		foreach($accounts as $account) {
182
			/** @var IAccount $account */
183
			if ($account->getId() === UnifiedAccount::ID) {
184
				continue;
185
			}
186
			$inbox = $account->getInbox();
187
			$inboxName = $inbox->getFolderId();
188
			$changes = $account->getChangedMailboxes([$inboxName => [
0 ignored issues
show
Documentation introduced by
array($inboxName => arra...'][$account->getId()])) is of type array<string,array<strin...","uidnext":"string"}>>, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
				'uidvalidity' => $query[self::INBOX_ID]['uidvalidity'][$account->getId()],
190
				'uidnext' => $query[self::INBOX_ID]['uidnext'][$account->getId()],
191
			]]);
192
			if (!isset($changes[$inboxName])) {
193
				continue;
194
			}
195
			if (!isset($changedBoxes[self::INBOX_ID])) {
196
				$changedBoxes[self::INBOX_ID] = $this->buildInbox();
197
				$changedBoxes[self::INBOX_ID]['messages'] = [];
198
				$changedBoxes[self::INBOX_ID]['newUnReadCounter'] = 0;
199
			}
200
			// Create special unified inbox message IDs
201
			foreach ($changes[$inboxName]['messages'] as &$message) {
202
				$id = base64_encode(json_encode([$account->getId(), $message['id']]));
203
				$message['id'] = $id;
204
				$message['accountMail'] = $account->getEmail();
205
			}
206
			$changedBoxes[self::INBOX_ID]['messages'] = array_merge($changedBoxes[self::INBOX_ID]['messages'], $changes[$inboxName]['messages']);
207
			$changedBoxes[self::INBOX_ID]['newUnReadCounter'] += $changes[$inboxName]['newUnReadCounter'];
208
		}
209
		return $changedBoxes;
210
	}
211
212
	/**
213
	 * @return IMailBox
214
	 */
215
	public function getInbox() {
216
		return null;
217
	}
218
219
	/**
220
	 * @return int
221
	 */
222
	public function getId() {
223
		return UnifiedAccount::ID;
224
	}
225
226
	/**
227
	 * @param string $messageId
228
	 * @return array
229
	 */
230 View Code Duplication
	public function resolve($messageId) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
		$data = json_decode(base64_decode($messageId), true);
232
		$account = $this->accountService->find($this->userId, $data[0]);
233
		$inbox = $account->getInbox();
234
		$messageId = $data[1];
235
236
		return [$account, base64_encode($inbox->getFolderId()), $messageId];
237
	}
238
}
239