Completed
Pull Request — master (#1616)
by
unknown
08:27
created

AccountService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 103
Duplicated Lines 8.74 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 70.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 5
dl 9
loc 103
ccs 31
cts 44
cp 0.7045
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findByUserId() 0 15 3
B find() 0 15 5
A delete() 0 7 2
A save() 0 3 1
A buildUnifiedAccount() 0 3 1
A updateSignature() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	/**
117
	 * @param int $accountId
118
	 * @param String $currentUserId
119
	 * @param String $signature
120
	 * @return \OCA\Mail\Db\Alias
121
	 */
122 View Code Duplication
	public function updateSignature($accountId, $currentUserId, $signature) {
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...
123
		try {
124
			$account = $this->mapper->find($currentUserId, $accountId);
125
			$account->setSignature($signature);
126
			return $this->mapper->update($account);
127
		} catch(Exception $e) {
128
			$this->handleException($e);
0 ignored issues
show
Bug introduced by
The method handleException() does not seem to exist on object<OCA\Mail\Service\AccountService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
		}
130
	}
131
}
132