Failed Conditions
Push — master ( 2a7938...ff349a )
by Sander
27:39 queued 19:45
created

VaultController   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 141
Duplicated Lines 12.06 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 17
loc 141
ccs 29
cts 29
cp 1
rs 10
c 2
b 0
f 0
wmc 18
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A delete() 0 4 1
B listVaults() 0 25 4
B get() 0 30 3
B update() 0 10 5
A updateSharingKeys() 0 14 3
A __construct() 17 17 1

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
 * Nextcloud - passman
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Sander Brand <[email protected]>
9
 * @copyright Sander Brand 2016
10
 */
11
12
namespace OCA\Passman\Controller;
13
14
use OCA\Passman\Service\EncryptService;
15
use OCA\Passman\Service\SettingsService;
16
use OCA\Passman\Utility\NotFoundJSONResponse;
17
use OCP\AppFramework\Db\DoesNotExistException;
18
use OCP\IRequest;
19
use OCP\AppFramework\Http\JSONResponse;
20
use OCP\AppFramework\ApiController;
21
use OCA\Passman\Service\VaultService;
22
use OCA\Passman\Service\CredentialService;
23
24
25
class VaultController extends ApiController {
26
	private $userId;
27
	private $vaultService;
28
	private $credentialService;
29
	private $settings;
30
31 View Code Duplication
	public function __construct($AppName,
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...
32
								IRequest $request,
33
								$UserId,
34
								VaultService $vaultService,
35
								CredentialService $credentialService,
36
								SettingsService $settings) {
37
		parent::__construct(
38
			$AppName,
39
			$request,
40
			'GET, POST, DELETE, PUT, PATCH, OPTIONS',
41
			'Authorization, Content-Type, Accept',
42
			86400);
43
		$this->userId = $UserId;
44
		$this->vaultService = $vaultService;
45
		$this->credentialService = $credentialService;
46
		$this->settings = $settings;
47
	}
48
49
	/**
50
	 * @NoAdminRequired
51
	 * @NoCSRFRequired
52
	 */
53 1
	public function listVaults() {
54 1
		$result = array();
55 1
		$vaults = $this->vaultService->getByUser($this->userId);
56
57 1
		$protected_credential_fields = array('getDescription', 'getEmail', 'getUsername', 'getPassword');
58 1
		if (isset($vaults)) {
59
			foreach ($vaults as $vault) {
60
				$credential = $this->credentialService->getRandomCredentialByVaultId($vault->getId(), $this->userId);
61
				$secret_field = $protected_credential_fields[array_rand($protected_credential_fields)];
62
				if(isset($credential)) {
63
					array_push($result, array(
64
						'vault_id' => $vault->getId(),
65
						'guid' => $vault->getGuid(),
66
						'name' => $vault->getName(),
67
						'created' => $vault->getCreated(),
68
						'public_sharing_key' => $vault->getPublicSharingKey(),
69
						'last_access' => $vault->getlastAccess(),
70
						'challenge_password' => $credential->{$secret_field}(),
71
					));
72
				}
73
			}
74
		}
75
76 1
		return new JSONResponse($result);
77
	}
78
79
	/**
80
	 * @NoAdminRequired
81
	 * @NoCSRFRequired
82
	 */
83 1
	public function create($vault_name) {
84 1
		$vault = $this->vaultService->createVault($vault_name, $this->userId);
85 1
		return new JSONResponse($vault);
86
	}
87
88
	/**
89
	 * @NoAdminRequired
90
	 * @NoCSRFRequired
91
	 */
92 1
	public function get($vault_guid) {
93 1
		$vault = null;
94
		try {
95 1
			$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
96
		} catch (\Exception $e) {
97
			return new NotFoundJSONResponse();
98
		}
99 1
		$result = array();
100 1
		if (isset($vault)) {
101
			$credentials = $this->credentialService->getCredentialsByVaultId($vault->getId(), $this->userId);
102
103
			$result = array(
104
				'vault_id' => $vault->getId(),
105
				'guid' => $vault->getGuid(),
106
				'name' => $vault->getName(),
107
				'created' => $vault->getCreated(),
108
				'private_sharing_key' => $vault->getPrivateSharingKey(),
109
				'public_sharing_key' => $vault->getPublicSharingKey(),
110
				'sharing_keys_generated' => $vault->getSharingKeysGenerated(),
111
				'vault_settings' => $vault->getVaultSettings(),
112
				'last_access' => $vault->getlastAccess()
113
			);
114
			$result['credentials'] = $credentials;
115
116
			$this->vaultService->setLastAccess($vault->getId(), $this->userId);
117
		}
118
119
120 1
		return new JSONResponse($result);
121
	}
122
123
	/**
124
	 * @NoAdminRequired
125
	 * @NoCSRFRequired
126
	 */
127 1
	public function update($vault_guid, $name, $vault_settings) {
128 1
		$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
129 1
		if ($name && $vault) {
130
			$vault->setName($name);
131
		}
132 1
		if ($vault_settings && $vault) {
133
			$vault->setVaultSettings($vault_settings);
134
		}
135 1
		$this->vaultService->updateVault($vault);
136 1
	}
137
138
	/**
139
	 * @NoAdminRequired
140
	 * @NoCSRFRequired
141
	 */
142 1
	public function updateSharingKeys($vault_guid, $private_sharing_key, $public_sharing_key) {
143 1
		$vault = null;
144
		try {
145 1
			$vault = $this->vaultService->getByGuid($vault_guid, $this->userId);
146
		} catch (\Exception $e) {
147
			// No need to catch the execption
148
		}
149
150 1
		if ($vault) {
151
			$this->vaultService->updateSharingKeys($vault->getId(), $private_sharing_key, $public_sharing_key);
152
		}
153
154 1
		return;
155
	}
156
157
	/**
158
	 * @NoAdminRequired
159
	 * @NoCSRFRequired
160
	 */
161 1
	public function delete($vault_guid) {
162 1
		$this->vaultService->deleteVault($vault_guid, $this->userId);
163 1
		return new JSONResponse(array('ok' => true));
164
	}
165
}