1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Tahaa Karim <[email protected]> |
4
|
|
|
* |
5
|
|
|
* ownCloud - Mail |
6
|
|
|
* |
7
|
|
|
* This code is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
9
|
|
|
* as published by the Free Software Foundation. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
namespace OCA\Mail\Service; |
21
|
|
|
|
22
|
|
|
use Exception; |
23
|
|
|
use OCA\Mail\Account; |
24
|
|
|
use OCA\Mail\Db\AliasMapper; |
25
|
|
|
use OCP\IL10N; |
26
|
|
|
|
27
|
|
|
class AliasesService { |
28
|
|
|
|
29
|
|
|
/** @var \OCA\Mail\Db\AliasMapper */ |
30
|
|
|
private $mapper; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Cache accounts for multiple calls to 'findByUserId' |
34
|
|
|
* |
35
|
|
|
* @var IAccount[] |
36
|
|
|
*/ |
37
|
|
|
private $aliases; |
38
|
|
|
|
39
|
|
|
/** @var IL10N */ |
40
|
|
|
private $l10n; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param AliasMapper $mapper |
44
|
|
|
*/ |
45
|
|
|
public function __construct(AliasMapper $mapper, IL10N $l10n) { |
46
|
|
|
$this->mapper = $mapper; |
47
|
|
|
$this->l10n = $l10n; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $currentUserId |
52
|
|
|
* @param $accountId |
53
|
|
|
* @return IAccount |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function findAll($currentUserId, $accountId) { |
|
|
|
|
56
|
|
|
if ($this->accounts !== null) { |
57
|
|
|
foreach ($this->accounts as $account) { |
|
|
|
|
58
|
|
|
if ($account->getId() === $accountId) { |
59
|
|
|
return $account; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
throw new Exception("Invalid account id <$accountId>"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ((int)$accountId === UnifiedAccount::ID) { |
66
|
|
|
return $this->buildUnifiedAccount($currentUserId); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
return new Account($this->mapper->find($currentUserId, $accountId)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $accountId |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function delete($currentUserId, $accountId) { |
|
|
|
|
75
|
|
|
if ((int)$accountId === UnifiedAccount::ID) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
$mailAccount = $this->mapper->find($currentUserId, $accountId); |
79
|
|
|
$this->mapper->delete($mailAccount); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $newAccount |
84
|
|
|
* @return \OCA\Mail\Db\MailAccount |
85
|
|
|
*/ |
86
|
|
|
public function save($newAccount) { |
|
|
|
|
87
|
|
|
return $this->mapper->save($newAlias); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.