UnifiedMailbox::getMessages()   B
last analyzed

Complexity

Conditions 6
Paths 2

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
cc 6
eloc 27
nc 2
nop 3
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * Mail
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
namespace OCA\Mail\Service;
23
24
use OCA\Mail\Attachment;
25
use OCA\Mail\Message;
26
27
class UnifiedMailbox implements IMailBox {
28
29
	/**
30
	 * @param string $userId
31
	 */
32
	public function __construct(AccountService $accountService, $userId) {
33
		$this->accountService = $accountService;
0 ignored issues
show
Bug introduced by
The property accountService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
		$this->userId = $userId;
0 ignored issues
show
Bug introduced by
The property userId does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
	}
36
37
	/**
38
	 * @return string
39
	 */
40
	public function getFolderId() {
41
		// TODO: Implement getFolderId() method.
42
	}
43
44
	/**
45
	 * @param int $from
46
	 * @param int $count
47
	 * @param string|\Horde_Imap_Client_Search_Query $filter
48
	 * @return array
49
	 */
50
	public function getMessages($from, $count, $filter) {
51
		$allAccounts = $this->accountService->findByUserId($this->userId);
52
		$allMessages = array_map(function($account) use ($from, $count, $filter) {
53
			/** @var IAccount $account */
54
			if ($account->getId() === UnifiedAccount::ID) {
55
				return [];
56
			}
57
			$inbox = $account->getInbox();
58
			if (is_null($inbox)) {
59
				return [];
60
			}
61
62
			$messages = $inbox->getMessages($from, $count, $filter);
63
			$messages = array_map(function($message) use ($account) {
64
				$message['id'] = base64_encode(json_encode([$account->getId(), $message['id']]));
65
				$message['accountMail'] = $account->getEmail();
66
				return $message;
67
			}, $messages);
68
69
			return $messages;
70
		}, $allAccounts);
71
72
		$allMessages = array_reduce($allMessages, function($a, $b) {
73
			if (is_null($a)) {
74
				return $b;
75
			}
76
			if (is_null($b)) {
77
				return $a;
78
			}
79
			return array_merge($a, $b);
80
		});
81
82
		// sort by time
83
		usort($allMessages, function($a, $b) {
84
			return $a['dateInt'] < $b['dateInt'];
85
		});
86
87
		if ($count >= 0) {
88
			$allMessages = array_slice($allMessages, 0, $count);
89
		}
90
91
		return $allMessages;
92
	}
93
94
	/**
95
	 * @return string
96
	 */
97
	public function getSpecialRole() {
98
		return 'inbox';
99
	}
100
101
	/**
102
	 * @param string $messageId
103
	 * @return Message
104
	 */
105
	public function getMessage($messageId, $loadHtmlMessageBody = false) {
106
		/** @var IMailBox $inbox */
107
		/** @var IAccount $account */
108
		list($inbox, $messageId, $account) = $this->resolve($messageId);
109
		/** @var Message $message */
110
		$message = $inbox->getMessage($messageId, $loadHtmlMessageBody);
111
		$message->setUid(base64_encode(json_encode([$account->getId(), $message->getUid()])));
112
113
		return $message;
114
	}
115
116
	/**
117
	 * @param string $messageId
118
	 * @param string $attachmentId
119
	 * @return Attachment
120
	 */
121
	public function getAttachment($messageId, $attachmentId) {
122
		/** @var IMailBox $inbox */
123
		list($inbox, $messageId) = $this->resolve($messageId);
124
		return $inbox->getAttachment($messageId, $attachmentId);
125
	}
126
127
	/**
128
	 * @param string $messageId
129
	 * @param string $flag
130
	 * @param mixed $value
131
	 */
132
	public function setMessageFlag($messageId, $flag, $value) {
133
		/** @var IMailBox $inbox */
134
		list($inbox, $messageId) = $this->resolve($messageId);
135
		return $inbox->setMessageFlag($messageId, $flag, $value);
136
	}
137
138
	/**
139
	 * @return array
140
	 */
141
	public function getStatus() {
142
		return [];
143
	}
144
145
	/**
146
	 * @param string $messageId
147
	 * @return array
148
	 */
149 View Code Duplication
	private 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...
150
		$data = json_decode(base64_decode($messageId), true);
151
		$account = $this->accountService->find($this->userId, $data[0]);
152
		$inbox = $account->getInbox();
153
		$messageId = $data[1];
154
		return array($inbox, $messageId, $account);
155
	}
156
}
157