1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OCA\Mail\Service; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use OCA\Mail\Account; |
7
|
|
|
use OCA\Mail\Db\MailAccountMapper; |
8
|
|
|
use OCP\IL10N; |
9
|
|
|
|
10
|
|
|
class AccountService { |
11
|
|
|
|
12
|
|
|
/** @var \OCA\Mail\Db\MailAccountMapper */ |
13
|
|
|
private $mapper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Cache accounts for multiple calls to 'findByUserId' |
17
|
|
|
* |
18
|
|
|
* @var IAccount[] |
19
|
|
|
*/ |
20
|
|
|
private $accounts; |
21
|
|
|
|
22
|
|
|
/** @var IL10N */ |
23
|
|
|
private $l10n; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param MailAccountMapper $mapper |
27
|
|
|
*/ |
28
|
7 |
|
public function __construct(MailAccountMapper $mapper, IL10N $l10n) { |
29
|
7 |
|
$this->mapper = $mapper; |
30
|
7 |
|
$this->l10n = $l10n; |
31
|
7 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $currentUserId |
35
|
|
|
* @return IAccount[] |
36
|
|
|
*/ |
37
|
2 |
|
public function findByUserId($currentUserId) { |
38
|
2 |
|
if ($this->accounts === null) { |
39
|
2 |
|
$accounts = $this->mapper->findByUserId($currentUserId); |
40
|
2 |
|
$accounts = array_map(function($a) { |
41
|
2 |
|
return new Account($a); |
42
|
2 |
|
}, $accounts); |
43
|
2 |
|
if (count($accounts) > 1) { |
44
|
1 |
|
$unifiedAccount = $this->buildUnifiedAccount($currentUserId); |
45
|
1 |
|
$accounts = array_merge([$unifiedAccount], $accounts); |
46
|
1 |
|
} |
47
|
2 |
|
$this->accounts = $accounts; |
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
return $this->accounts; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param $currentUserId |
55
|
|
|
* @param $accountId |
56
|
|
|
* @return IAccount |
57
|
|
|
*/ |
58
|
1 |
|
public function find($currentUserId, $accountId) { |
59
|
1 |
|
if ($this->accounts !== null) { |
60
|
|
|
foreach ($this->accounts as $account) { |
61
|
|
|
if ($account->getId() === $accountId) { |
62
|
|
|
return $account; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
throw new Exception("Invalid account id <$accountId>"); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
if ((int)$accountId === UnifiedAccount::ID) { |
69
|
|
|
return $this->buildUnifiedAccount($currentUserId); |
70
|
|
|
} |
71
|
1 |
|
return new Account($this->mapper->find($currentUserId, $accountId)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $accountId |
76
|
|
|
*/ |
77
|
2 |
|
public function delete($currentUserId, $accountId) { |
78
|
2 |
|
if ((int)$accountId === UnifiedAccount::ID) { |
79
|
1 |
|
return; |
80
|
|
|
} |
81
|
1 |
|
$mailAccount = $this->mapper->find($currentUserId, $accountId); |
82
|
1 |
|
$this->mapper->delete($mailAccount); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $newAccount |
87
|
|
|
* @return \OCA\Mail\Db\MailAccount |
88
|
|
|
*/ |
89
|
1 |
|
public function save($newAccount) { |
90
|
1 |
|
return $this->mapper->save($newAccount); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
private function buildUnifiedAccount($userId) { |
94
|
1 |
|
return new UnifiedAccount($this, $userId, $this->l10n); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|