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

StorageFolder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 71.1%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
eloc 38
c 4
b 0
f 0
dl 0
loc 150
ccs 32
cts 45
cp 0.711
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A fakeRoot() 0 3 1
A getLocalPath() 0 12 3
A exists() 0 6 1
A getGenerator() 0 5 2
A newFile() 0 4 1
A get() 0 4 1
A listing() 0 3 1
A sync() 0 8 2
A isCreatable() 0 3 1
A newFolder() 0 4 1
A __construct() 0 7 1
A getBasePath() 0 4 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 OC\Files\Utils\Scanner;
28
use OC\ForbiddenException;
29
use OCP\Files\Folder as OCFolder;
30
use OCP\Files\InvalidPathException;
31
use OCP\Files\NotPermittedException;
32
use OCP\IDBConnection;
33
use OCP\ILogger;
34
use OCP\ITempManager;
35
36
class StorageFolder extends AbstractStorageNode implements FolderInterface
37
{
38
	use FolderTrait;
39
40
	/** @var array<string,string> */
41
	private static $localPathCache = [];
42
43
	/** @var OCFolder */
44
	protected $node;
45
46
	/** @var ITempManager */
47
	private $tempManager;
48
49
	/** @var IDBConnection */
50
	private $connection;
51
52
	/** @var ILogger */
53
	private $logger;
54
55
	/**
56
	 * StorageFolder constructor.
57
	 *
58
	 * @param OCFolder    $folder
59
	 * @param string|null $basePath
60
	 *
61
	 * @throws InvalidPathException
62
	 */
63 7
	public function __construct(OCFolder $folder, string $basePath = null)
64
	{
65 7
		$this->tempManager = \OC::$server->getTempManager();
66 7
		$this->connection = \OC::$server->query(IDBConnection::class);
67 7
		$this->logger = \OC::$server->query(ILogger::class);
68
69 7
		parent::__construct($folder, $basePath);
70 7
	}
71
72
	/**
73
	 * {@inheritDoc}
74
	 */
75 2
	public function getLocalPath(): string
76
	{
77 2
		if ($this->isLocal()) {
78 2
			return parent::getLocalPath();
79
		}
80
81
		$cachePath = $this->getOCNode()->getPath();
82
		if (!isset(self::$localPathCache[$cachePath])) {
83
			self::$localPathCache[$cachePath] = $this->tempManager->getTemporaryFolder();
84
		}
85
86
		return self::$localPathCache[$cachePath];
87
	}
88
89
	/**
90
	 * {@inheritDoc}
91
	 */
92
	public function listing(): array
93
	{
94
		return iterator_to_array($this->getGenerator());
95
	}
96
97
	/**
98
	 * {@inheritDoc}
99
	 */
100 2
	protected function getGenerator(): \Generator
101
	{
102 2
		$basePath = $this->getPath();
103 2
		foreach ($this->node->getDirectoryListing() as $node) {
104 2
			yield $this->repackNode($node, $basePath);
105
		}
106 2
	}
107
108
	/**
109
	 * {@inheritDoc}
110
	 */
111
	public function exists(string $path): bool
112
	{
113
		// check for root path breakouts
114
		$this->getBasePath($path);
115
116
		return $this->node->nodeExists($path);
117
	}
118
119
	/**
120
	 * {@inheritDoc}
121
	 */
122 7
	public function get(string $path): NodeInterface
123
	{
124 7
		$basePath = $this->getBasePath($path);
125 7
		return $this->repackNode($this->node->get($path), $basePath);
126
	}
127
128
	/**
129
	 * {@inheritDoc}
130
	 */
131 5
	public function newFolder(string $path): FolderInterface
132
	{
133 5
		$basePath = $this->getBasePath($path);
134 5
		return new StorageFolder($this->node->newFolder($path), $basePath);
135
	}
136
137
	/**
138
	 * {@inheritDoc}
139
	 */
140 4
	public function newFile(string $path): FileInterface
141
	{
142 4
		$basePath = $this->getBasePath($path);
143 4
		return new StorageFile($this->node->newFile($path), $basePath);
144
	}
145
146
	/**
147
	 * {@inheritDoc}
148
	 */
149 3
	public function fakeRoot(): FolderInterface
150
	{
151 3
		return new StorageFolder($this->node);
152
	}
153
154
	/**
155
	 * {@inheritDoc}
156
	 */
157 5
	public function sync(bool $recursive = FolderInterface::SYNC_RECURSIVE)
158
	{
159 5
		$scanner = new Scanner(null, $this->connection, $this->logger);
160
161
		try {
162 5
			$scanner->scan($this->node->getPath(), $recursive);
163
		} catch (ForbiddenException $e) {
164
			throw new NotPermittedException();
165
		}
166 5
	}
167
168
	/**
169
	 * {@inheritDoc}
170
	 */
171
	public function isCreatable(): bool
172
	{
173
		return $this->node->isCreatable();
174
	}
175
176
	/**
177
	 * @param string $path
178
	 *
179
	 * @return string|null
180
	 * @throws InvalidPathException
181
	 */
182 7
	private function getBasePath(string $path)
183
	{
184 7
		$path = $this->normalizePath($this->getPath() . '/' . $path);
185 7
		return ($path !== '/') ? dirname($path) : null;
186
	}
187
}
188