AbstractNode   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 51.92%

Importance

Changes 0
Metric Value
wmc 24
eloc 42
c 0
b 0
f 0
dl 0
loc 154
ccs 27
cts 52
cp 0.5192
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A truncate() 0 10 3
A __construct() 0 3 1
A __toString() 0 3 1
A assertValidFileName() 0 7 4
A normalizePath() 0 3 1
A isDeletable() 0 3 1
A isFolder() 0 3 1
A move() 0 24 5
A copy() 0 19 4
A isReadable() 0 3 1
A isUpdateable() 0 3 1
A isFile() 0 3 1
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 OCA\CMSPico\Service\MiscService;
28
use OCP\Constants;
29
use OCP\Files\InvalidPathException;
30
use OCP\Files\NotPermittedException;
31
32
abstract class AbstractNode implements NodeInterface
33
{
34
	/** @var MiscService */
35
	protected $miscService;
36
37
	/**
38
	 * AbstractNode constructor.
39
	 */
40 29
	public function __construct()
41
	{
42 29
		$this->miscService = \OC::$server->query(MiscService::class);
0 ignored issues
show
Deprecated Code introduced by
The function OC\ServerContainer::query() has been deprecated: 20.0.0 use \Psr\Container\ContainerInterface::get ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

42
		$this->miscService = /** @scrutinizer ignore-deprecated */ \OC::$server->query(MiscService::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43 29
	}
44
45
	/**
46
	 * {@inheritDoc}
47
	 */
48 11
	public function copy(FolderInterface $targetPath, string $name = null): NodeInterface
49
	{
50 11
		if ($name !== null) {
51 2
			$this->assertValidFileName($name);
52
		}
53
54 11
		if ($this->isFolder()) {
55
			/** @var FolderInterface $this */
56 8
			$target = $targetPath->newFolder($name ?? $this->getName());
57 8
			foreach ($this as $child) {
58 8
				$child->copy($target);
59
			}
60
		} else {
61
			/** @var FileInterface $this */
62 11
			$target = $targetPath->newFile($name ?? $this->getName());
63 11
			$target->putContent($this->getContent());
64
		}
65
66 11
		return $target;
67
	}
68
69
	/**
70
	 * {@inheritDoc}
71
	 */
72
	public function move(FolderInterface $targetPath, string $name = null): NodeInterface
73
	{
74
		if ($name !== null) {
75
			$this->assertValidFileName($name);
76
		}
77
78
		if (!$this->isDeletable()) {
79
			throw new NotPermittedException();
80
		}
81
82
		if ($this->isFolder()) {
83
			/** @var FolderInterface $this */
84
			$target = $targetPath->newFolder($name ?? $this->getName());
85
			foreach ($this as $child) {
86
				$child->move($target);
87
			}
88
		} else {
89
			/** @var FileInterface $this */
90
			$target = $targetPath->newFile($name ?? $this->getName());
91
			$target->putContent($this->getContent());
92
		}
93
94
		$this->delete();
95
		return $target;
96
	}
97
98
	/**
99
	 * {@inheritDoc}
100
	 */
101
	public function truncate(): void
102
	{
103
		if ($this->isFolder()) {
104
			/** @var FolderInterface $this */
105
			foreach ($this as $child) {
106
				$child->delete();
107
			}
108
		} else {
109
			/** @var FileInterface $this */
110
			$this->putContent('');
111
		}
112
	}
113
114
	/**
115
	 * {@inheritDoc}
116
	 */
117
	public function isFile(): bool
118
	{
119
		return ($this instanceof FileInterface);
120
	}
121
122
	/**
123
	 * {@inheritDoc}
124
	 */
125 3
	public function isFolder(): bool
126
	{
127 3
		return ($this instanceof FolderInterface);
128
	}
129
130
	/**
131
	 * {@inheritDoc}
132
	 */
133 12
	public function isReadable(): bool
134
	{
135 12
		return ($this->getPermissions() & Constants::PERMISSION_READ) === Constants::PERMISSION_READ;
136
	}
137
138
	/**
139
	 * {@inheritDoc}
140
	 */
141 7
	public function isUpdateable(): bool
142
	{
143 7
		return ($this->getPermissions() & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE;
144
	}
145
146
	/**
147
	 * {@inheritDoc}
148
	 */
149 4
	public function isDeletable(): bool
150
	{
151 4
		return ($this->getPermissions() & Constants::PERMISSION_DELETE) === Constants::PERMISSION_DELETE;
152
	}
153
154
	/**
155
	 * {@inheritDoc}
156
	 */
157
	public function __toString(): string
158
	{
159
		return $this->getName();
160
	}
161
162
	/**
163
	 * @param string $name
164
	 *
165
	 * @throws InvalidPathException
166
	 */
167 2
	protected function assertValidFileName(string $name): void
168
	{
169 2
		if (in_array($name, [ '', '.', '..' ], true)) {
170
			throw new InvalidPathException();
171
		}
172 2
		if ((strpos($name, '/') !== false) || (strpos($name, '\\') !== false)) {
173
			throw new InvalidPathException();
174
		}
175 2
	}
176
177
	/**
178
	 * @param string $path
179
	 *
180
	 * @return string
181
	 * @throws InvalidPathException
182
	 */
183 29
	protected function normalizePath(string $path): string
184
	{
185 29
		return '/' . $this->miscService->normalizePath($path);
186
	}
187
}
188