Passed
Pull Request — master (#77)
by Daniel
51:29
created

AbstractLocalNode   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Test Coverage

Coverage 56.32%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 47
eloc 79
c 4
b 0
f 0
dl 0
loc 246
ccs 49
cts 87
cp 0.5632
rs 8.64

15 Methods

Rating   Name   Duplication   Size   Complexity  
B move() 0 21 8
A isLocal() 0 3 1
A isFolder() 0 3 1
B copy() 0 21 8
A __construct() 0 9 3
A getParentNode() 0 7 2
A getName() 0 3 1
A getPath() 0 3 1
A rename() 0 20 5
A getLocalPath() 0 3 2
A getParent() 0 7 2
A delete() 0 8 3
A getBasePath() 0 3 1
A isFile() 0 3 1
B getPermissions() 0 35 8

How to fix   Complexity   

Complex Class

Complex classes like AbstractLocalNode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractLocalNode, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace OCA\CMSPico\Files;
26
27
use OCP\Constants;
28
use OCP\Files\AlreadyExistsException;
29
use OCP\Files\GenericFileException;
30
use OCP\Files\InvalidPathException;
31
use OCP\Files\NotFoundException;
32
use OCP\Files\NotPermittedException;
33
34
abstract class AbstractLocalNode extends AbstractNode implements NodeInterface
35
{
36
	/** @var string */
37
	protected $path;
38
39
	/** @var string */
40
	protected $basePath;
41
42
	/** @var LocalFolder */
43
	protected $parentFolder;
44
45
	/** @var int */
46
	protected $permissions;
47
48
	/**
49
	 * AbstractLocalNode constructor.
50
	 *
51
	 * @param string $path
52
	 * @param string $basePath
53
	 *
54
	 * @throws InvalidPathException
55
	 * @throws NotFoundException
56
	 */
57 6
	public function __construct(string $path, string $basePath)
58
	{
59 6
		parent::__construct();
60
61 6
		$this->path = $this->normalizePath($path);
62 6
		$this->basePath = realpath($basePath ?: \OC::$SERVERROOT);
63
64 6
		if (!file_exists($this->getLocalPath())) {
65
			throw new NotFoundException();
66
		}
67 6
	}
68
69
	/**
70
	 * {@inheritDoc}
71
	 */
72 1
	public function rename(string $name): NodeInterface
73
	{
74 1
		$this->assertValidFileName($name);
75
76 1
		$parentNode = $this->getParentNode();
77 1
		if ($parentNode->exists($name)) {
78
			throw new AlreadyExistsException();
79
		}
80 1
		if (!$parentNode->isCreatable()) {
81
			throw new NotPermittedException();
82
		}
83
84 1
		if (!rename($this->getLocalPath(), dirname($this->getLocalPath()) . '/' . $name)) {
85
			throw new GenericFileException();
86
		}
87
88 1
		$parentPath = dirname($this->path);
89 1
		$this->path = (($parentPath !== '/') ? $parentPath : '') . '/' . $name;
90
91 1
		return $this;
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 */
97
	public function copy(FolderInterface $targetPath, string $name = null): NodeInterface
98
	{
99
		if (($targetPath instanceof LocalFolder) && $this->isFile()) {
100
			if ($name !== null) {
101
				$this->assertValidFileName($name);
102
			}
103
104
			if ($targetPath->exists($this->getName())) {
105
				throw new AlreadyExistsException();
106
			}
107
			if (!$targetPath->isCreatable()) {
108
				throw new NotPermittedException();
109
			}
110
111
			if (!@copy($this->getLocalPath(), $targetPath->getLocalPath() . '/' . ($name ?: $this->getName()))) {
112
				throw new GenericFileException();
113
			}
114
115
			return new LocalFile($targetPath->getPath() . '/' . $this->getName(), $targetPath->basePath);
116
		} else {
117
			return parent::copy($targetPath, $name);
118
		}
119
	}
120
121
	/**
122
	 * {@inheritDoc}
123
	 */
124
	public function move(FolderInterface $targetPath, string $name = null): NodeInterface
125
	{
126
		if (($targetPath instanceof LocalFolder) && $this->isFile()) {
127
			if ($name !== null) {
128
				$this->assertValidFileName($name);
129
			}
130
131
			if ($targetPath->exists($this->getName())) {
132
				throw new AlreadyExistsException();
133
			}
134
			if (!$targetPath->isCreatable()) {
135
				throw new NotPermittedException();
136
			}
137
138
			if (!@rename($this->getLocalPath(), $targetPath->getLocalPath() . '/' . ($name ?: $this->getName()))) {
139
				throw new GenericFileException();
140
			}
141
142
			return new LocalFile($targetPath->getPath() . '/' . $this->getName(), $targetPath->getBasePath());
143
		} else {
144
			return parent::move($targetPath, $name);
145
		}
146
	}
147
148
	/**
149
	 * {@inheritDoc}
150
	 */
151 1
	public function delete()
152
	{
153 1
		if (!$this->isDeletable()) {
154
			throw new NotPermittedException();
155
		}
156
157 1
		if (!@unlink($this->getLocalPath())) {
158
			throw new GenericFileException();
159
		}
160 1
	}
161
162
	/**
163
	 * {@inheritDoc}
164
	 */
165
	public function getPath(): string
166
	{
167
		return $this->path;
168
	}
169
170
	/**
171
	 * @return string
172
	 */
173
	public function getBasePath(): string
174
	{
175
		return $this->basePath;
176
	}
177
178
	/**
179
	 * {@inheritDoc}
180
	 */
181 6
	public function getLocalPath(): string
182
	{
183 6
		return $this->basePath . (($this->path !== '/') ? $this->path : '');
184
	}
185
186
	/**
187
	 * {@inheritDoc}
188
	 */
189 4
	public function getName(): string
190
	{
191 4
		return basename($this->path);
192
	}
193
194
	/**
195
	 * {@inheritDoc}
196
	 */
197 4
	public function getParent(): string
198
	{
199 4
		if ($this->path === '/') {
200 4
			throw new InvalidPathException();
201
		}
202
203 4
		return dirname($this->path);
204
	}
205
206
	/**
207
	 * {@inheritDoc}
208
	 */
209 4
	public function getParentNode(): FolderInterface
210
	{
211 4
		if ($this->parentFolder === null) {
212 4
			$this->parentFolder = new LocalFolder($this->getParent(), $this->basePath);
213
		}
214
215 4
		return $this->parentFolder;
216
	}
217
218
	/**
219
	 * {@inheritDoc}
220
	 */
221 1
	public function isFile(): bool
222
	{
223 1
		return is_file($this->getLocalPath());
224
	}
225
226
	/**
227
	 * {@inheritDoc}
228
	 */
229 6
	public function isFolder(): bool
230
	{
231 6
		return is_dir($this->getLocalPath());
232
	}
233
234
	/**
235
	 * {@inheritDoc}
236
	 */
237
	public function isLocal(): bool
238
	{
239
		return true;
240
	}
241
242
	/**
243
	 * {@inheritDoc}
244
	 */
245 4
	public function getPermissions(): int
246
	{
247 4
		if ($this->permissions === null) {
248 4
			$this->permissions = 0;
249
250
			try {
251 4
				$localPath = $this->getLocalPath();
252
			} catch (\Exception $e) {
253
				// can't read a node's permissions without its local path
254
				return $this->permissions;
255
			}
256
257 4
			if (is_readable($localPath)) {
258 4
				$this->permissions |= Constants::PERMISSION_READ;
259
			}
260
261 4
			if (is_writable($localPath)) {
262 4
				$this->permissions |= Constants::PERMISSION_UPDATE;
263
264 4
				if ($this->isFolder()) {
265 4
					$this->permissions |= Constants::PERMISSION_CREATE;
266
				}
267
			}
268
269
			try {
270 4
				if (is_writable($this->getParentNode()->getLocalPath())) {
271 4
					$this->permissions |= Constants::PERMISSION_DELETE;
272
				}
273 4
			} catch (\Exception $e) {
274
				// this is either the root folder or we can't read the parent folder's local path
275
				// in neither case we can delete this node
276
			}
277
		}
278
279 4
		return $this->permissions;
280
	}
281
}
282