Passed
Push — master ( 695eed...e372a6 )
by Daniel
23:18
created

AbstractLocalNode::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 10
cc 3
nc 3
nop 0
crap 3.576
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 16
	public function __construct(string $path, string $basePath)
58
	{
59 16
		parent::__construct();
60
61 16
		$this->path = $this->normalizePath($path);
62 16
		$this->basePath = realpath($basePath ?: \OC::$SERVERROOT);
63
64 16
		if (!file_exists($this->getLocalPath())) {
65
			throw new NotFoundException();
66
		}
67 16
	}
68
69
	/**
70
	 * {@inheritDoc}
71
	 */
72
	public function rename(string $name): NodeInterface
73
	{
74
		$this->assertValidFileName($name);
75
76
		$parentNode = $this->getParentNode();
77
		if ($parentNode->exists($name)) {
78
			throw new AlreadyExistsException();
79
		}
80
		if (!$parentNode->isCreatable()) {
81
			throw new NotPermittedException();
82
		}
83
84
		if (!rename($this->getLocalPath(), dirname($this->getLocalPath()) . '/' . $name)) {
85
			throw new GenericFileException();
86
		}
87
88
		$parentPath = dirname($this->path);
89
		$this->path = (($parentPath !== '/') ? $parentPath : '') . '/' . $name;
90
91
		return $this;
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 */
97 2
	public function copy(FolderInterface $targetPath, string $name = null): NodeInterface
98
	{
99 2
		if (($targetPath instanceof LocalFolder) && $this->isFile()) {
100
			if ($name !== null) {
101
				$this->assertValidFileName($name);
102
			} else {
103
				$name = $this->getName();
104
			}
105
106
			if ($targetPath->exists($name)) {
107
				throw new AlreadyExistsException();
108
			}
109
			if (!$targetPath->isCreatable()) {
110
				throw new NotPermittedException();
111
			}
112
113
			if (!@copy($this->getLocalPath(), $targetPath->getLocalPath() . '/' . $name)) {
114
				throw new GenericFileException();
115
			}
116
117
			return new LocalFile($targetPath->getPath() . '/' . $name, $targetPath->basePath);
118
		} else {
119 2
			return parent::copy($targetPath, $name);
120
		}
121
	}
122
123
	/**
124
	 * {@inheritDoc}
125
	 */
126
	public function move(FolderInterface $targetPath, string $name = null): NodeInterface
127
	{
128
		if (($targetPath instanceof LocalFolder) && $this->isFile()) {
129
			if ($name !== null) {
130
				$this->assertValidFileName($name);
131
			} else {
132
				$name = $this->getName();
133
			}
134
135
			if ($targetPath->exists($name)) {
136
				throw new AlreadyExistsException();
137
			}
138
			if (!$targetPath->isCreatable()) {
139
				throw new NotPermittedException();
140
			}
141
142
			if (!@rename($this->getLocalPath(), $targetPath->getLocalPath() . '/' . $name)) {
143
				throw new GenericFileException();
144
			}
145
146
			return new LocalFile($targetPath->getPath() . '/' . $name, $targetPath->getBasePath());
147
		} else {
148
			return parent::move($targetPath, $name);
149
		}
150
	}
151
152
	/**
153
	 * {@inheritDoc}
154
	 */
155 4
	public function delete(): void
156
	{
157 4
		if (!$this->isDeletable()) {
158
			throw new NotPermittedException();
159
		}
160
161 4
		if (!@unlink($this->getLocalPath())) {
162
			throw new GenericFileException();
163
		}
164 4
	}
165
166
	/**
167
	 * {@inheritDoc}
168
	 */
169
	public function getPath(): string
170
	{
171
		return $this->path;
172
	}
173
174
	/**
175
	 * @return string
176
	 */
177
	public function getBasePath(): string
178
	{
179
		return $this->basePath;
180
	}
181
182
	/**
183
	 * {@inheritDoc}
184
	 */
185 16
	public function getLocalPath(): string
186
	{
187 16
		return $this->basePath . (($this->path !== '/') ? $this->path : '');
188
	}
189
190
	/**
191
	 * {@inheritDoc}
192
	 */
193 11
	public function getName(): string
194
	{
195 11
		return basename($this->path);
196
	}
197
198
	/**
199
	 * {@inheritDoc}
200
	 */
201 13
	public function getParent(): string
202
	{
203 13
		if ($this->path === '/') {
204 11
			throw new InvalidPathException();
205
		}
206
207 13
		return dirname($this->path);
208
	}
209
210
	/**
211
	 * {@inheritDoc}
212
	 */
213 13
	public function getParentNode(): FolderInterface
214
	{
215 13
		if ($this->parentFolder === null) {
216 13
			$this->parentFolder = new LocalFolder($this->getParent(), $this->basePath);
217
		}
218
219 13
		return $this->parentFolder;
220
	}
221
222
	/**
223
	 * {@inheritDoc}
224
	 */
225 7
	public function isFile(): bool
226
	{
227 7
		return is_file($this->getLocalPath());
228
	}
229
230
	/**
231
	 * {@inheritDoc}
232
	 */
233 16
	public function isFolder(): bool
234
	{
235 16
		return is_dir($this->getLocalPath());
236
	}
237
238
	/**
239
	 * {@inheritDoc}
240
	 */
241
	public function isLocal(): bool
242
	{
243
		return true;
244
	}
245
246
	/**
247
	 * {@inheritDoc}
248
	 */
249 13
	public function getPermissions(): int
250
	{
251 13
		if ($this->permissions === null) {
252 13
			$this->permissions = 0;
253
254
			try {
255 13
				$localPath = $this->getLocalPath();
256
			} catch (\Exception $e) {
257
				// can't read a node's permissions without its local path
258
				return $this->permissions;
259
			}
260
261 13
			if (is_readable($localPath)) {
262 13
				$this->permissions |= Constants::PERMISSION_READ;
263
			}
264
265 13
			if (is_writable($localPath)) {
266 13
				$this->permissions |= Constants::PERMISSION_UPDATE;
267
268 13
				if ($this->isFolder()) {
269 13
					$this->permissions |= Constants::PERMISSION_CREATE;
270
				}
271
			}
272
273
			try {
274 13
				if (is_writable($this->getParentNode()->getLocalPath())) {
275 13
					$this->permissions |= Constants::PERMISSION_DELETE;
276
				}
277 11
			} catch (\Exception $e) {
278
				// this is either the root folder or we can't read the parent folder's local path
279
				// in neither case we can delete this node
280
			}
281
		}
282
283 13
		return $this->permissions;
284
	}
285
}
286