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

AbstractNode::isFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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
31
abstract class AbstractNode implements NodeInterface
32
{
33
	/** @var MiscService */
34
	protected $miscService;
35
36
	/**
37
	 * AbstractNode constructor.
38
	 */
39 29
	public function __construct()
40
	{
41 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

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