Passed
Push — master ( 11b018...e5557a )
by Maxence
05:01 queued 01:42
created

FileService::getSourceFiles()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 22
rs 8.9197
c 1
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
1
<?php
2
/**
3
 * CMS Pico - Integration of Pico within your files to create websites.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\CMSPico\Service;
28
29
use DirectoryIterator;
30
use Exception;
31
use OCA\CMSPico\AppInfo\Application;
32
use OCA\CMSPico\Model\TemplateFile;
33
use OCP\Files\Folder;
34
use OCP\Files\IAppData;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IAppData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use OCP\Files\IRootFolder;
36
use OCP\Files\Node;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
use OCP\Files\NotFoundException;
38
39
class FileService {
40
41
42
	const INSTALL_DIR = __DIR__ . '/../../Pico/';
43
44
	/** @var IRootFolder */
45
	private $rootFolder;
46
47
	/** @var ConfigService */
48
	private $configService;
49
50
	/** @var MiscService */
51
	private $miscService;
52
53
	/** @var Folder */
54
	private $appDataFolder;
55
56
57
	/**
58
	 * ConfigService constructor.
59
	 *
60
	 * @param IRootFolder $rootFolder
61
	 * @param ConfigService $configService
62
	 * @param MiscService $miscService
63
	 */
64
	public function __construct(
65
		IRootFolder $rootFolder, ConfigService $configService, MiscService $miscService
66
	) {
67
		$this->rootFolder = $rootFolder;
68
		$this->configService = $configService;
69
		$this->miscService = $miscService;
70
	}
71
72
73
//	public function getAppDataFolderContent($dir) {
74
	public function getDirectoriesFromAppDataFolder($dir) {
75
// do we still use DirectoryIterator as files are in DB ?
76
		$all = [];
77
		foreach (new DirectoryIterator($this->getAppDataFolderAbsolutePath($dir)) as $file) {
78
			if (!$file->isDir() || substr($file->getFilename(), 0, 1) === '.') {
79
				continue;
80
			}
81
82
			$all[] = $file->getFilename();
83
		}
84
85
		return $all;
86
	}
87
88
89
	/**
90
	 * @param string $base
91
	 * @param string $dir
92
	 *
93
	 * @return string[]
94
	 */
95 View Code Duplication
	public function getSourceFiles($base, $dir = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
97
		$base = MiscService::endSlash($base);
98
		$dir = MiscService::endSlash($dir);
99
100
		$files = [];
101
		foreach (new DirectoryIterator($base . $dir) as $file) {
102
103
			if (substr($file->getFilename(), 0, 1) === '.') {
104
				continue;
105
			}
106
107
			if ($file->isDir()) {
108
				$files[] = $dir . $file->getFilename() . '/';
109
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
110
				continue;
111
			}
112
113
			$files[] = $dir . $file->getFilename();
114
		}
115
116
		return $files;
117
	}
118
119
120
121
122
/**
123
 * // TODO: this function should use File from nc, not read on the filesystem
124
	 * @param string $base
125
	 * @param string $dir
126
	 *
127
	 * @return string[]
128
	 */
129 View Code Duplication
	public function getAppDataFiles($base, $dir = '') {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
131
		$base = MiscService::endSlash($base);
132
		$dir = MiscService::endSlash($dir);
133
134
		$files = [];
135
		foreach (new DirectoryIterator($base . $dir) as $file) {
136
137
			if (substr($file->getFilename(), 0, 1) === '.') {
138
				continue;
139
			}
140
141
			if ($file->isDir()) {
142
				$files[] = $dir . $file->getFilename() . '/';
143
				$files = array_merge($files, $this->getSourceFiles($base, $dir . $file->getFilename()));
144
				continue;
145
			}
146
147
			$files[] = $dir . $file->getFilename();
148
		}
149
150
		return $files;
151
	}
152
153
154
	/**
155
	 * @param string $dir
156
	 *
157
	 * @return string
158
	 */
159
	public function getAppDataFolderAbsolutePath($dir) {
160
161
		$appNode = $this->getAppDataFolder();
162
163
		try {
164
			$appNode->get($dir);
165
		} catch (NotFoundException $e) {
166
			$this->createAppDataFolder($appNode, $dir);
167
		}
168
169
		$appPath = MiscService::endSlash($this->configService->getSystemValue('datadirectory', null));
170
		$appPath .= MiscService::endSlash($appNode->getInternalPath());
171
172
		$appPath .= MiscService::endSlash($dir);
173
174
		return $appPath;
175
	}
176
177
178
	/**
179
	 * Create Appdata Subfolder and duplicate the content from apps/cms_pico/Pico/
180
	 *
181
	 * @param Folder $appNode
182
	 * @param string $dir
183
	 */
184
	private function createAppDataFolder(Folder $appNode, $dir) {
185
		$appFolder = $appNode->newFolder($dir);
186
187
		$files = $this->getSourceFiles(self::INSTALL_DIR . $dir);
188
		foreach ($files as $file) {
189
			if (substr($file, -1) === '/') {
190
				$appFolder->newFolder($file);
191
			} else {
192
				$newFile = $appFolder->newFile($file);
193
				$newFile->putContent(file_get_contents(self::INSTALL_DIR . $dir . '/' . $file));
194
			}
195
		}
196
	}
197
198
199
	/**
200
	 * Get AppData Folder for this app, create it otherwise.
201
	 *
202
	 * @return Folder
203
	 */
204
	private function getAppDataFolder() {
205
		if ($this->appDataFolder === null) {
206
207
			$instanceId = $this->configService->getSystemValue('instanceid', null);
208
			$name = 'appdata_' . $instanceId;
209
210
			/** @var Folder $globalAppDataFolder */
211
			try {
212
				$globalAppDataFolder = $this->rootFolder->get($name);
213
			} catch (NotFoundException $e) {
214
				$globalAppDataFolder = $this->rootFolder->newFolder($name);
215
			}
216
217
			try {
218
				$this->appDataFolder = $globalAppDataFolder->get(Application::APP_NAME);
219
			} catch (NotFoundException $e) {
220
				$this->appDataFolder = $globalAppDataFolder->newFolder(Application::APP_NAME);
221
			}
222
		}
223
224
		return $this->appDataFolder;
225
	}
226
227
228
}
229