Passed
Pull Request — master (#77)
by Daniel
22:33
created

Version010000Beta::postSchemaChange()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 11
cp 0
rs 10
cc 2
nc 2
nop 3
crap 6
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\Files\FolderInterface;
28
use OCA\CMSPico\Service\ConfigService;
29
use OCA\CMSPico\Service\FileService;
30
use OCA\CMSPico\Service\PicoService;
31
use OCA\CMSPico\Service\ThemesService;
32
use OCP\Migration\IOutput;
33
use OCP\Migration\SimpleMigrationStep;
34
35
class Version010000Beta extends SimpleMigrationStep
36
{
37
	use MigrationTrait;
38
39
	/** @var ConfigService */
40
	private $configService;
41
42
	/** @var ThemesService */
43
	private $themesService;
44
45
	/** @var FileService */
46
	private $fileService;
47
48
	/**
49
	 * Version010000 constructor.
50
	 */
51
	public function __construct()
52
	{
53
		$this->setLogger(\OC::$server->getLogger());
54
55
		$this->configService = \OC::$server->query(ConfigService::class);
56
		$this->themesService = \OC::$server->query(ThemesService::class);
57
		$this->fileService = \OC::$server->query(FileService::class);
58
	}
59
60
	/**
61
	 * @param IOutput  $output
62
	 * @param \Closure $schemaClosure
63
	 * @param array    $options
64
	 */
65
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options)
66
	{
67
		$previousAppVersion = $this->configService->getAppValue('installed_version');
68
		if ($previousAppVersion !== '1.0.0') {
69
			// v1.0.0-beta.1 wrongly identifies itself as v1.0.0
70
			// nothing to do if not upgrading from said fake v1.0.0
71
			return;
72
		}
73
74
		$this->setOutput($output);
75
76
		// reset ETags
77
		$this->configService->deleteAppValue(ConfigService::THEMES_ETAG);
78
		$this->configService->deleteAppValue(ConfigService::THEMES_ETAG);
79
80
		// migrate themes
81
		$this->migrateDefaultTheme();
82
		$this->migrateCustomThemes();
83
	}
84
85
	/**
86
	 * @return void
87
	 */
88
	private function migrateDefaultTheme()
89
	{
90
		$customThemesFolder = $this->fileService->getAppDataFolder(PicoService::DIR_THEMES);
91
		$customThemesFolder->sync(FolderInterface::SYNC_SHALLOW);
92
93
		$themeName = 'default';
94
		if ($customThemesFolder->exists($themeName)) {
95
			$themeFolder = $customThemesFolder->getFolder($themeName);
96
97
			$newThemeName = $themeName . '-v0.9';
98
			for ($i = 1; isset($customThemes[$themeName]); $i++) {
99
				$newThemeName = $themeName . '-v0.9-dup' . $i;
100
			}
101
102
			$themeFolder->rename($newThemeName);
103
			$this->themesService->publishCustomTheme($newThemeName);
104
105
			$this->logWarning('Renaming old Pico CMS system theme "%s" to "%s"', $themeName, $newThemeName);
106
		}
107
	}
108
109
	/**
110
	 * @return void
111
	 */
112
	private function migrateCustomThemes()
113
	{
114
		$this->logInfo('Downgrading data structure of Pico CMS themes');
115
116
		$customThemesJson = $this->configService->getAppValue(ConfigService::CUSTOM_THEMES);
117
		$customThemes = $customThemesJson ? json_decode($customThemesJson, true) : [];
118
119
		$newCustomThemes = [];
120
		foreach ($customThemes as $themeData) {
121
			$newCustomThemes[] = $themeData['name'];
122
		}
123
124
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($newCustomThemes));
125
	}
126
}
127