Passed
Push — master ( 91c768...85bc68 )
by
unknown
05:40
created

StorageFolder::getGenerator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
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 OCP\Files\AlreadyExistsException;
28
use OCP\Files\Folder as OCFolder;
29
use OCP\Files\InvalidPathException;
30
use OCP\Files\NotFoundException;
31
use OCP\Files\NotPermittedException;
32
use OCP\ITempManager;
33
34
class StorageFolder extends AbstractStorageNode implements FolderInterface
35
{
36
	use FolderTrait;
37
38
	/** @var array<string,string> */
39
	private static $localPathCache = [];
40
41
	/** @var OCFolder */
42
	protected $node;
43
44
	/** @var ITempManager */
45
	private $tempManager;
46
47
	/** @var StorageScanner */
48
	private $scanner;
49
50
	/** @var StorageFolder|null */
51
	protected $rootFolder;
52
53
	/**
54
	 * StorageFolder constructor.
55
	 *
56
	 * @param OCFolder    $folder
57
	 * @param string|null $parentPath
58
	 *
59
	 * @throws InvalidPathException
60
	 */
61 29
	public function __construct(OCFolder $folder, string $parentPath = null)
62
	{
63 29
		$this->tempManager = \OC::$server->getTempManager();
0 ignored issues
show
Deprecated Code introduced by
The function OC\Server::getTempManager() has been deprecated: 20.0.0 ( Ignorable by Annotation )

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

63
		$this->tempManager = /** @scrutinizer ignore-deprecated */ \OC::$server->getTempManager();

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...
64 29
		$this->scanner = \OC::$server->query(StorageScanner::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

64
		$this->scanner = /** @scrutinizer ignore-deprecated */ \OC::$server->query(StorageScanner::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...
65
66 29
		parent::__construct($folder, $parentPath);
67 29
	}
68
69
	/**
70
	 * {@inheritDoc}
71
	 */
72 5
	public function getLocalPath(): string
73
	{
74 5
		if ($this->isLocal()) {
75 5
			return parent::getLocalPath();
76
		}
77
78
		$cachePath = $this->getOCNode()->getPath();
79
		if (!isset(self::$localPathCache[$cachePath])) {
80
			self::$localPathCache[$cachePath] = $this->tempManager->getTemporaryFolder();
81
		}
82
83
		return self::$localPathCache[$cachePath];
84
	}
85
86
	/**
87
	 * {@inheritDoc}
88
	 */
89
	public function listing(): array
90
	{
91
		return iterator_to_array($this->getGenerator());
92
	}
93
94
	/**
95
	 * {@inheritDoc}
96
	 */
97 18
	protected function getGenerator(): \Generator
98
	{
99 18
		$parentPath = $this->getPath();
100 18
		foreach ($this->node->getDirectoryListing() as $node) {
101 15
			yield $this->repackNode($node, $parentPath);
102
		}
103 18
	}
104
105
	/**
106
	 * {@inheritDoc}
107
	 */
108 5
	public function exists(string $path): bool
109
	{
110 5
		$path = $this->normalizePath($this->getPath() . '/' . $path);
111 5
		return $this->getRootFolder()->getOCNode()->nodeExists($path);
112
	}
113
114
	/**
115
	 * {@inheritDoc}
116
	 */
117 29
	public function get(string $path): NodeInterface
118
	{
119 29
		$path = $this->normalizePath($this->getPath() . '/' . $path);
120 29
		$parentPath = ($path !== '/') ? dirname($path) : null;
121 29
		return $this->repackNode($this->getRootFolder()->getOCNode()->get($path), $parentPath);
122
	}
123
124
	/**
125
	 * {@inheritDoc}
126
	 */
127 5
	public function newFolder(string $path): FolderInterface
128
	{
129 5
		if ($this->exists($path)) {
130
			throw new AlreadyExistsException();
131
		}
132
133 5
		$path = $this->normalizePath($this->getPath() . '/' . $path);
134
135 5
		$name = basename($path);
136 5
		$parentPath = dirname($path);
137
138
		/** @var StorageFolder $parentFolder */
139 5
		$parentFolder = $this->newFolderRecursive($parentPath);
140
141 5
		if (!$parentFolder->isCreatable()) {
142
			throw new NotPermittedException();
143
		}
144
145 5
		return new StorageFolder(
146 5
			$parentFolder->getOCNode()->newFolder($name),
147 5
			($path !== '/') ? $parentPath : null
148
		);
149
	}
150
151
	/**
152
	 * {@inheritDoc}
153
	 */
154 5
	public function newFile(string $path): FileInterface
155
	{
156 5
		if ($this->exists($path)) {
157
			throw new AlreadyExistsException();
158
		}
159
160 5
		$path = $this->normalizePath($this->getPath() . '/' . $path);
161
162 5
		$name = basename($path);
163 5
		$parentPath = dirname($path);
164
165
		/** @var StorageFolder $parentFolder */
166 5
		$parentFolder = $this->newFolderRecursive($parentPath);
167
168 5
		if (!$parentFolder->isCreatable()) {
169
			throw new NotPermittedException();
170
		}
171
172 5
		return new StorageFile(
173 5
			$parentFolder->getOCNode()->newFile($name),
174 5
			($path !== '/') ? $parentPath : null
175
		);
176
	}
177
178
	/**
179
	 * {@inheritDoc}
180
	 */
181 9
	public function fakeRoot(): FolderInterface
182
	{
183 9
		return new StorageFolder($this->node);
184
	}
185
186
	/**
187
	 * {@inheritDoc}
188
	 */
189 21
	public function sync(bool $recursive = FolderInterface::SYNC_RECURSIVE): void
190
	{
191 21
		$this->scanner->scan($this->node->getPath(), $recursive);
192 21
	}
193
194
	/**
195
	 * {@inheritDoc}
196
	 */
197 5
	public function isCreatable(): bool
198
	{
199 5
		return $this->node->isCreatable();
200
	}
201
202
	/**
203
	 * {@inheritDoc}
204
	 */
205 29
	protected function getRootFolder(): self
206
	{
207 29
		if ($this->getPath() === '/') {
208 29
			return $this;
209
		}
210
211 12
		if ($this->rootFolder === null) {
212 12
			$ocFolder = $this->node;
213 12
			for ($i = 0; $i < substr_count($this->getPath(), '/'); $i++) {
214
				try {
215 12
					$ocFolder = $ocFolder->getParent();
216
				} catch (NotFoundException $e) {
217
					throw new InvalidPathException();
218
				}
219
			}
220
221 12
			$this->rootFolder = new StorageFolder($ocFolder);
222
		}
223
224 12
		return $this->rootFolder;
225
	}
226
}
227