Passed
Push — master ( 4352c0...c7fee6 )
by Maxence
02:05
created

FileService   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 191
Duplicated Lines 24.08 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
dl 46
loc 191
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getSourceFiles() 22 22 4
A getDirectoriesFromAppDataFolder() 0 12 4
A getAppDataFolder() 0 21 4
B getAppDataFiles() 22 22 4
A getAppDataFolderPath() 0 20 3
A createAppDataFolder() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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