Passed
Push — master ( 235c52...927bab )
by Daniel
30:30 queued 10s
created

AppDataRepairStep::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 16
cp 0
rs 10
cc 1
nc 1
nop 7
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\Migration;
26
27
use OCA\CMSPico\Service\ConfigService;
28
use OCA\CMSPico\Service\FileService;
29
use OCA\CMSPico\Service\MiscService;
30
use OCA\CMSPico\Service\PicoService;
31
use OCA\CMSPico\Service\PluginsService;
32
use OCA\CMSPico\Service\TemplatesService;
33
use OCA\CMSPico\Service\ThemesService;
34
use OCP\Files\NotFoundException;
35
use OCP\ILogger;
36
use OCP\Migration\IOutput;
37
use OCP\Migration\IRepairStep;
38
39
class AppDataRepairStep implements IRepairStep
40
{
41
	use MigrationTrait;
42
43
	/** @var ConfigService */
44
	private $configService;
45
46
	/** @var TemplatesService */
47
	private $templatesService;
48
49
	/** @var ThemesService */
50
	private $themesService;
51
52
	/** @var PluginsService */
53
	private $pluginsService;
54
55
	/** @var FileService */
56
	private $fileService;
57
58
	/** @var MiscService */
59
	private $miscService;
60
61
	/**
62
	 * AppDataRepairStep constructor.
63
	 *
64
	 * @param ILogger          $logger
65
	 * @param ConfigService    $configService
66
	 * @param TemplatesService $templatesService
67
	 * @param ThemesService    $themesService
68
	 * @param PluginsService   $pluginsService
69
	 * @param FileService      $fileService
70
	 * @param MiscService      $miscService
71
	 */
72
	public function __construct(
73
		ILogger $logger,
74
		ConfigService $configService,
75
		TemplatesService $templatesService,
76
		ThemesService $themesService,
77
		PluginsService $pluginsService,
78
		FileService $fileService,
79
		MiscService $miscService
80
	) {
81
		$this->setLogger($logger);
82
83
		$this->configService = $configService;
84
		$this->templatesService = $templatesService;
85
		$this->themesService = $themesService;
86
		$this->pluginsService = $pluginsService;
87
		$this->fileService = $fileService;
88
		$this->miscService = $miscService;
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94
	public function getName(): string
95
	{
96
		return 'Preparing app data of Pico CMS for Nextcloud';
97
	}
98
99
	/**
100
	 * @param IOutput $output
101
	 */
102
	public function run(IOutput $output)
103
	{
104
		$this->setOutput($output);
105
106
		$this->logInfo('Checking Pico CMS requirements …');
107
		$this->miscService->checkComposer();
108
		$this->miscService->checkPublicFolder();
109
110
		$this->logInfo('Syncing Pico CMS app data folder …');
111
		$this->syncAppDataFolder();
112
113
		$this->logInfo('Copying Pico CMS config …');
114
		$this->copyConfig();
115
116
		$this->logInfo('Registering Pico CMS templates …');
117
		$this->registerTemplates();
118
119
		$this->logInfo('Publishing Pico CMS themes …');
120
		$this->publishThemes();
121
122
		$this->logInfo('Publishing Pico CMS plugins …');
123
		$this->publishPlugins();
124
	}
125
126
	/**
127
	 * @return void
128
	 */
129
	private function syncAppDataFolder()
130
	{
131
		$this->fileService->syncAppDataFolder();
132
	}
133
134
	/**
135
	 * @return void
136
	 */
137
	private function copyConfig()
138
	{
139
		$appDataConfigFolder = $this->fileService->getAppDataFolder(PicoService::DIR_CONFIG);
140
		$systemConfigFolder = $this->fileService->getSystemFolder(PicoService::DIR_CONFIG);
141
142
		foreach ($systemConfigFolder as $configFile) {
143
			$configFileName = $configFile->getName();
144
145
			if (!$configFile->isFile()) {
146
				continue;
147
			}
148
149
			try {
150
				$appDataConfigFolder->getFile($configFileName)->delete();
151
				$this->logWarning('Replacing Pico CMS config file "%s"', $configFileName);
152
			} catch (NotFoundException $e) {
153
				$this->logInfo('Adding Pico CMS config file "%s"', $configFileName);
154
			}
155
156
			$configFile->copy($appDataConfigFolder);
157
		}
158
	}
159
160
	/**
161
	 * @return void
162
	 */
163
	private function registerTemplates()
164
	{
165
		$this->registerSystemTemplates();
166
		$this->registerCustomTemplates();
167
	}
168
169
	/**
170
	 * @return void
171
	 */
172
	private function registerSystemTemplates()
173
	{
174
		$systemTemplatesFolder = $this->fileService->getSystemFolder(PicoService::DIR_TEMPLATES);
175
176
		$oldSystemTemplates = $this->templatesService->getSystemTemplates();
177
		$this->configService->deleteAppValue(ConfigService::SYSTEM_TEMPLATES);
178
179
		foreach ($systemTemplatesFolder as $templateFolder) {
180
			$templateName = $templateFolder->getName();
181
			if ($templateFolder->isFolder()) {
182
				$this->templatesService->registerSystemTemplate($templateName);
183
			}
184
		}
185
186
		$oldSystemTemplates = array_keys($oldSystemTemplates);
187
		$newSystemTemplates = array_keys($this->templatesService->getSystemTemplates());
188
		$this->logChanges('Pico CMS system template', $newSystemTemplates, $oldSystemTemplates, true);
189
	}
190
191
	/**
192
	 * @return void
193
	 */
194
	private function registerCustomTemplates()
195
	{
196
		$appDataTemplatesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_TEMPLATES);
197
198
		$oldCustomTemplates = $this->templatesService->getCustomTemplates();
199
		$this->configService->deleteAppValue(ConfigService::CUSTOM_TEMPLATES);
200
201
		$systemTemplates = $this->templatesService->getSystemTemplates();
202
		foreach ($appDataTemplatesFolder as $templateFolder) {
203
			$templateName = $templateFolder->getName();
204
			if ($templateFolder->isFolder()) {
205
				if (isset($oldCustomTemplates[$templateName]) && !isset($systemTemplates[$templateName])) {
206
					$this->templatesService->registerCustomTemplate($templateName);
207
				}
208
			}
209
		}
210
211
		$oldCustomTemplates = array_keys($oldCustomTemplates);
212
		$newCustomTemplates = array_keys($this->templatesService->getCustomTemplates());
213
		$this->logChanges('Pico CMS custom template', $newCustomTemplates, $oldCustomTemplates);
214
	}
215
216
	/**
217
	 * @return void
218
	 */
219
	private function publishThemes()
220
	{
221
		$publicThemesFolder = $this->fileService->getPublicFolder(PicoService::DIR_THEMES);
222
		$publicThemesFolder->truncate();
223
224
		$this->configService->deleteAppValue(ConfigService::THEMES_ETAG);
225
226
		$this->publishSystemThemes();
227
		$this->publishCustomThemes();
228
	}
229
230
	/**
231
	 * @return void
232
	 */
233
	private function publishSystemThemes()
234
	{
235
		$systemThemesFolder = $this->fileService->getSystemFolder(PicoService::DIR_THEMES);
236
237
		$oldSystemThemes = $this->themesService->getSystemThemes();
238
		$this->configService->deleteAppValue(ConfigService::SYSTEM_THEMES);
239
240
		foreach ($systemThemesFolder as $themeFolder) {
241
			$themeName = $themeFolder->getName();
242
			if ($themeFolder->isFolder()) {
243
				$this->themesService->publishSystemTheme($themeName);
244
			}
245
		}
246
247
		$oldSystemThemes = array_keys($oldSystemThemes);
248
		$newSystemThemes = array_keys($this->themesService->getSystemThemes());
249
		$this->logChanges('Pico CMS system theme', $newSystemThemes, $oldSystemThemes, true);
250
	}
251
252
	/**
253
	 * @return void
254
	 */
255
	private function publishCustomThemes()
256
	{
257
		$appDataThemesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_THEMES);
258
259
		$oldCustomThemes = $this->themesService->getCustomThemes();
260
		$this->configService->deleteAppValue(ConfigService::CUSTOM_THEMES);
261
262
		$systemThemes = $this->themesService->getSystemThemes();
263
		foreach ($appDataThemesFolder as $themeFolder) {
264
			$themeName = $themeFolder->getName();
265
			if ($themeFolder->isFolder()) {
266
				if (isset($oldCustomThemes[$themeName]) && !isset($systemThemes[$themeName])) {
267
					$this->themesService->publishCustomTheme($themeName);
268
				}
269
			}
270
		}
271
272
		$oldCustomThemes = array_keys($oldCustomThemes);
273
		$newCustomThemes = array_keys($this->themesService->getCustomThemes());
274
		$this->logChanges('Pico CMS custom theme', $newCustomThemes, $oldCustomThemes);
275
	}
276
277
	/**
278
	 * @return void
279
	 */
280
	private function publishPlugins()
281
	{
282
		$publicPluginsFolder = $this->fileService->getPublicFolder(PicoService::DIR_PLUGINS);
283
		$publicPluginsFolder->truncate();
284
285
		$this->configService->deleteAppValue(ConfigService::PLUGINS_ETAG);
286
287
		$this->publishSystemPlugins();
288
		$this->publishCustomPlugins();
289
	}
290
291
	/**
292
	 * @return void
293
	 */
294
	private function publishSystemPlugins()
295
	{
296
		$systemPluginsFolder = $this->fileService->getSystemFolder(PicoService::DIR_PLUGINS);
297
298
		$oldSystemPlugins = $this->pluginsService->getSystemPlugins();
299
		$this->configService->deleteAppValue(ConfigService::SYSTEM_PLUGINS);
300
301
		foreach ($systemPluginsFolder as $pluginFolder) {
302
			$pluginName = $pluginFolder->getName();
303
			if ($pluginFolder->isFolder()) {
304
				$this->pluginsService->publishSystemPlugin($pluginName);
305
			}
306
		}
307
308
		$oldSystemPlugins = array_keys($oldSystemPlugins);
309
		$newSystemPlugins = array_keys($this->pluginsService->getSystemPlugins());
310
		$this->logChanges('Pico CMS system plugin', $newSystemPlugins, $oldSystemPlugins, true);
311
	}
312
313
	/**
314
	 * @return void
315
	 */
316
	private function publishCustomPlugins()
317
	{
318
		$appDataPluginsFolder = $this->fileService->getAppDataFolder(PicoService::DIR_PLUGINS);
319
320
		$oldCustomPlugins = $this->pluginsService->getCustomPlugins();
321
		$this->configService->deleteAppValue(ConfigService::CUSTOM_PLUGINS);
322
323
		$systemPlugins = $this->pluginsService->getSystemPlugins();
324
		foreach ($appDataPluginsFolder as $pluginFolder) {
325
			$pluginName = $pluginFolder->getName();
326
			if ($pluginFolder->isFolder()) {
327
				if (isset($oldCustomPlugins[$pluginName]) && !isset($systemPlugins[$pluginName])) {
328
					$this->pluginsService->publishCustomPlugin($pluginName);
329
				}
330
			}
331
		}
332
333
		$oldCustomPlugins = array_keys($oldCustomPlugins);
334
		$newCustomPlugins = array_keys($this->pluginsService->getCustomPlugins());
335
		$this->logChanges('Pico CMS custom plugin', $newCustomPlugins, $oldCustomPlugins);
336
	}
337
}
338