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 Exception; |
25
|
|
|
use OCA\Mail\Account; |
26
|
|
|
use OCA\Mail\Db\MailAccountMapper; |
27
|
|
|
use OCP\IL10N; |
28
|
|
|
|
29
|
|
|
class AccountService { |
30
|
|
|
|
31
|
|
|
/** @var \OCA\Mail\Db\MailAccountMapper */ |
32
|
|
|
private $mapper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cache accounts for multiple calls to 'findByUserId' |
36
|
|
|
* |
37
|
|
|
* @var IAccount[] |
38
|
|
|
*/ |
39
|
|
|
private $accounts; |
40
|
|
|
|
41
|
|
|
/** @var IL10N */ |
42
|
|
|
private $l10n; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param MailAccountMapper $mapper |
46
|
|
|
*/ |
47
|
7 |
|
public function __construct(MailAccountMapper $mapper, IL10N $l10n) { |
48
|
7 |
|
$this->mapper = $mapper; |
49
|
7 |
|
$this->l10n = $l10n; |
50
|
7 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $currentUserId |
54
|
|
|
* @return IAccount[] |
55
|
|
|
*/ |
56
|
2 |
|
public function findByUserId($currentUserId) { |
57
|
2 |
|
if ($this->accounts === null) { |
58
|
2 |
|
$accounts = $this->mapper->findByUserId($currentUserId); |
59
|
2 |
|
$accounts = array_map(function($a) { |
60
|
2 |
|
return new Account($a); |
61
|
2 |
|
}, $accounts); |
62
|
2 |
|
if (count($accounts) > 1) { |
63
|
1 |
|
$unifiedAccount = $this->buildUnifiedAccount($currentUserId); |
64
|
1 |
|
$accounts = array_merge([$unifiedAccount], $accounts); |
65
|
1 |
|
} |
66
|
2 |
|
$this->accounts = $accounts; |
67
|
2 |
|
} |
68
|
|
|
|
69
|
2 |
|
return $this->accounts; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param $currentUserId |
74
|
|
|
* @param $accountId |
75
|
|
|
* @return IAccount |
76
|
|
|
*/ |
77
|
1 |
|
public function find($currentUserId, $accountId) { |
78
|
1 |
|
if ($this->accounts !== null) { |
79
|
|
|
foreach ($this->accounts as $account) { |
80
|
|
|
if ($account->getId() === $accountId) { |
81
|
|
|
return $account; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
throw new Exception("Invalid account id <$accountId>"); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
if ((int)$accountId === UnifiedAccount::ID) { |
88
|
|
|
return $this->buildUnifiedAccount($currentUserId); |
89
|
|
|
} |
90
|
1 |
|
return new Account($this->mapper->find($currentUserId, $accountId)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int $accountId |
95
|
|
|
*/ |
96
|
2 |
|
public function delete($currentUserId, $accountId) { |
97
|
2 |
|
if ((int)$accountId === UnifiedAccount::ID) { |
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
1 |
|
$mailAccount = $this->mapper->find($currentUserId, $accountId); |
101
|
1 |
|
$this->mapper->delete($mailAccount); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $newAccount |
106
|
|
|
* @return \OCA\Mail\Db\MailAccount |
107
|
|
|
*/ |
108
|
1 |
|
public function save($newAccount) { |
109
|
1 |
|
return $this->mapper->save($newAccount); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
private function buildUnifiedAccount($userId) { |
113
|
1 |
|
return new UnifiedAccount($this, $userId, $this->l10n); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|