Completed
Push — master ( e8cbe7...f3c40f )
by Victor
21:02 queued 11:32
created

DisableExtraThemes::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Repair;
23
24
25
use OCP\App\IAppManager;
26
use OCP\IAppConfig;
27
use OCP\IConfig;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
31
/**
32
 * Leave only one app-theme active to prevent the mess
33
 */
34
class DisableExtraThemes implements IRepairStep {
35
36
	/** @var IAppManager */
37
	protected $appManager;
38
39
	/** @var IConfig */
40
	protected $config;
41
42
	/** @var IAppConfig */
43
	protected $appConfig;
44
45
	/**
46
	 * @param IAppManager $appManager
47
	 * @param IConfig $config
48
	 * @param IAppConfig $appConfig
49
	 */
50
	public function __construct($appManager, $config, $appConfig) {
51
		$this->appManager = $appManager;
52
		$this->config = $config;
53
		$this->appConfig = $appConfig;
54
	}
55
56
	public function getName() {
57
		return 'Disable extra themes';
58
	}
59
60
	/**
61
	 * @param IOutput $out
62
	 */
63
	public function run(IOutput $out) {
64
		$enabledThemes = $this->getEnabledAppThemes();
65
66
		$activeTheme = '';
67
		if (count($enabledThemes) >= 2) {
68
			$activeTheme = array_pop($enabledThemes);
69
			foreach ($enabledThemes as $appId) {
70
				$this->appManager->disableApp($appId);
71
				$out->info("Theme $appId disabled");
72
			}
73
		}
74
75
		$activeLegacyTheme = $this->config->getSystemValue('theme', '');
76
		if ($activeLegacyTheme !== '' && count($enabledThemes) > 0) {
77
			$this->config->setSystemValue('theme', '');
78
			$out->info("Legacy theme $activeLegacyTheme disabled");
79
		}
80
81
		if ($activeTheme !== '') {
82
			$out->info("Theme $activeTheme left active");
83
		}
84
	}
85
86
	/**
87
	 * @return string[]
88
	 */
89
	protected function getEnabledAppThemes() {
90
		// get enabled apps as index => appId
91
		$enabledApps = $this->appManager->getInstalledApps();
92
93
		// get themes as appId => appType
94
		$appTypes = $this->appConfig->getValues(false, 'types');
95
		$allThemes = array_filter(
96
			$appTypes,
97
			function ($appTypes){
98
				return in_array('theme', explode(',', $appTypes));
99
			}
100
		);
101
102
		// calculate an intersection to get enabled themes
103
		$enabledThemes = array_intersect(array_keys($allThemes), $enabledApps);
104
105
		return $enabledThemes;
106
	}
107
}
108