Passed
Push — master ( 42c369...11b018 )
by Maxence
02:16
created

FileService::getSourceFiles()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 8.9197
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 View Code Duplication
	public function getDirectoriesFromAppDataFolder($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...
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
	public function getSourceFiles($base, $dir = '') {
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
	 * @param string $dir
122
	 *
123
	 * @return string
124
	 */
125
	public function getAppDataFolderAbsolutePath($dir) {
126
127
		$appNode = $this->getAppDataFolder();
128
129
		try {
130
			$appNode->get($dir);
131
		} catch (NotFoundException $e) {
132
			$this->createAppDataFolder($appNode, $dir);
133
		}
134
135
		$appPath = MiscService::endSlash($this->configService->getSystemValue('datadirectory', null));
136
		$appPath .= MiscService::endSlash($appNode->getInternalPath());
137
138
		$appPath .= MiscService::endSlash($dir);
139
140
		return $appPath;
141
	}
142
143
144
	/**
145
	 * Create Appdata Subfolder and duplicate the content from apps/cms_pico/Pico/
146
	 *
147
	 * @param Folder $appNode
148
	 * @param string $dir
149
	 */
150
	private function createAppDataFolder(Folder $appNode, $dir) {
151
		$appFolder = $appNode->newFolder($dir);
152
153
		$files = $this->getSourceFiles(self::INSTALL_DIR . $dir);
154
		foreach ($files as $file) {
155
			if (substr($file, -1) === '/') {
156
				$appFolder->newFolder($file);
157
			} else {
158
				$newFile = $appFolder->newFile($file);
159
				$newFile->putContent(file_get_contents(self::INSTALL_DIR . $dir . '/' . $file));
160
			}
161
		}
162
	}
163
164
165
	/**
166
	 * Get AppData Folder for this app, create it otherwise.
167
	 *
168
	 * @return Folder
169
	 */
170
	private function getAppDataFolder() {
171
		if ($this->appDataFolder === null) {
172
173
			$instanceId = $this->configService->getSystemValue('instanceid', null);
174
			$name = 'appdata_' . $instanceId;
175
176
			/** @var Folder $globalAppDataFolder */
177
			try {
178
				$globalAppDataFolder = $this->rootFolder->get($name);
179
			} catch (NotFoundException $e) {
180
				$globalAppDataFolder = $this->rootFolder->newFolder($name);
181
			}
182
183
			try {
184
				$this->appDataFolder = $globalAppDataFolder->get(Application::APP_NAME);
185
			} catch (NotFoundException $e) {
186
				$this->appDataFolder = $globalAppDataFolder->newFolder(Application::APP_NAME);
187
			}
188
		}
189
190
		return $this->appDataFolder;
191
	}
192
193
194
}
195