Completed
Push — master ( ee8a58...dc88a3 )
by Individual IT
08:28
created

Provisioning   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 205
Duplicated Lines 29.27 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 60
loc 205
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLockingProvider() 0 7 2
A getType() 0 3 2
A getPath() 0 6 1
A isLockingEnabled() 0 8 2
A acquireLock() 20 20 4
A changeLock() 20 20 4
A releaseLock() 20 20 4
A isLocked() 0 18 4
C releaseAll() 0 29 8

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
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Testing\Locking;
23
24
use OC\Lock\DBLockingProvider;
25
use OC\User\NoUserException;
26
use OCP\AppFramework\Http;
27
use OCP\Files\NotFoundException;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
use OCP\IRequest;
31
use OCP\Lock\ILockingProvider;
32
use OCP\Lock\LockedException;
33
34
class Provisioning {
35
36
	/** @var ILockingProvider */
37
	protected $lockingProvider;
38
39
	/** @var IDBConnection */
40
	protected $connection;
41
42
	/** @var IConfig */
43
	protected $config;
44
45
	/** @var IRequest */
46
	protected $request;
47
48
	/**
49
	 * @param ILockingProvider $lockingProvider
50
	 * @param IDBConnection $connection
51
	 * @param IConfig $config
52
	 * @param IRequest $request
53
	 */
54
	public function __construct(ILockingProvider $lockingProvider, IDBConnection $connection, IConfig $config, IRequest $request) {
55
		$this->lockingProvider = $lockingProvider;
56
		$this->connection = $connection;
57
		$this->config = $config;
58
		$this->request = $request;
59
	}
60
61
	/**
62
	 * @return FakeDBLockingProvider
63
	 */
64
	protected function getLockingProvider() {
65
		if ($this->lockingProvider instanceof DBLockingProvider) {
66
			return \OC::$server->query('OCA\Testing\Locking\FakeDBLockingProvider');
67
		} else {
68
			throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider');
69
		}
70
	}
71
72
	/**
73
	 * @param array $parameters
74
	 * @return int
75
	 */
76
	protected function getType($parameters) {
77
		return isset($parameters['type']) ? (int) $parameters['type'] : 0;
78
	}
79
80
	/**
81
	 * @param array $parameters
82
	 * @return int
83
	 */
84
	protected function getPath($parameters) {
85
		$node = \OC::$server->getRootFolder()
86
			->getUserFolder($parameters['user'])
87
			->get($this->request->getParam('path'));
88
		return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/'));
89
	}
90
91
	/**
92
	 * @return \OC_OCS_Result
93
	 */
94
	public function isLockingEnabled() {
95
		try {
96
			$this->getLockingProvider();
97
			return new \OC_OCS_Result(null, 100);
98
		} catch (\RuntimeException $e) {
99
			return new \OC_OCS_Result(null, Http::STATUS_NOT_IMPLEMENTED, $e->getMessage());
100
		}
101
	}
102
103
	/**
104
	 * @param array $parameters
105
	 * @return \OC_OCS_Result
106
	 */
107 View Code Duplication
	public function acquireLock(array $parameters) {
108
		try {
109
			$path = $this->getPath($parameters);
110
		} catch (NoUserException $e) {
111
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'User not found');
112
		} catch (NotFoundException $e) {
113
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'Path not found');
114
		}
115
		$type = $this->getType($parameters);
116
117
		$lockingProvider = $this->getLockingProvider();
118
119
		try {
120
			$lockingProvider->acquireLock($path, $type);
121
			$this->config->setAppValue('testing', 'locking_' . $path, $type);
122
			return new \OC_OCS_Result(null, 100);
123
		} catch (LockedException $e) {
124
			return new \OC_OCS_Result(null, Http::STATUS_LOCKED);
125
		}
126
	}
127
128
	/**
129
	 * @param array $parameters
130
	 * @return \OC_OCS_Result
131
	 */
132 View Code Duplication
	public function changeLock(array $parameters) {
133
		try {
134
			$path = $this->getPath($parameters);
135
		} catch (NoUserException $e) {
136
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'User not found');
137
		} catch (NotFoundException $e) {
138
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'Path not found');
139
		}
140
		$type = $this->getType($parameters);
141
142
		$lockingProvider = $this->getLockingProvider();
143
144
		try {
145
			$lockingProvider->changeLock($path, $type);
146
			$this->config->setAppValue('testing', 'locking_' . $path, $type);
147
			return new \OC_OCS_Result(null, 100);
148
		} catch (LockedException $e) {
149
			return new \OC_OCS_Result(null, Http::STATUS_LOCKED);
150
		}
151
	}
152
153
	/**
154
	 * @param array $parameters
155
	 * @return \OC_OCS_Result
156
	 */
157 View Code Duplication
	public function releaseLock(array $parameters) {
158
		try {
159
			$path = $this->getPath($parameters);
160
		} catch (NoUserException $e) {
161
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'User not found');
162
		} catch (NotFoundException $e) {
163
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'Path not found');
164
		}
165
		$type = $this->getType($parameters);
166
167
		$lockingProvider = $this->getLockingProvider();
168
169
		try {
170
			$lockingProvider->releaseLock($path, $type);
171
			$this->config->deleteAppValue('testing', 'locking_' . $path);
172
			return new \OC_OCS_Result(null, 100);
173
		} catch (LockedException $e) {
174
			return new \OC_OCS_Result(null, Http::STATUS_LOCKED);
175
		}
176
	}
177
178
	/**
179
	 * @param array $parameters
180
	 * @return \OC_OCS_Result
181
	 */
182
	public function isLocked(array $parameters) {
183
		try {
184
			$path = $this->getPath($parameters);
185
		} catch (NoUserException $e) {
186
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'User not found');
187
		} catch (NotFoundException $e) {
188
			return new \OC_OCS_Result(null, Http::STATUS_NOT_FOUND, 'Path not found');
189
		}
190
		$type = $this->getType($parameters);
191
192
		$lockingProvider = $this->getLockingProvider();
193
194
		if ($lockingProvider->isLocked($path, $type)) {
195
			return new \OC_OCS_Result(null, 100);
196
		}
197
198
		return new \OC_OCS_Result(null, Http::STATUS_LOCKED);
199
	}
200
201
	/**
202
	 * releases all locks that were set by the testing app
203
	 * if $parameters['_delete']['global'] is set to "true"
204
	 * all locks in the files_lock table are released (set to "0")
205
	 * 
206
	 * @param array $parameters
207
	 * @return \OC_OCS_Result
208
	 */
209
	public function releaseAll(array $parameters) {
210
		$type = $this->getType($parameters);
211
		if (isset($parameters['_delete']['global'])
212
			&& $parameters['_delete']['global'] === "true"
213
		) {
214
			$globalRelease = true;
215
		} else {
216
			$globalRelease = false;
217
		}
218
219
		$lockingProvider = $this->getLockingProvider();
220
		if ($globalRelease === true) {
221
			$lockingProvider->releaseAllGlobally();
222
		} else {
223
			foreach ($this->config->getAppKeys('testing') as $lock) {
224
				if (strpos($lock, 'locking_') === 0) {
225
					$path = substr($lock, strlen('locking_'));
226
					$testingAppLock = (int) $this->config->getAppValue(
227
						'testing', $lock
228
					);
229
					if ($type === $testingAppLock || $type === 0) {
230
						$lockingProvider->releaseLock($path, $testingAppLock);
231
					}
232
				}
233
			}
234
		}
235
236
		return new \OC_OCS_Result(null, 100);
237
	}
238
}
239