Passed
Push — master ( e1cb1b...9a76f0 )
by John
15:33 queued 17s
created

UserThemeController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A enableTheme() 0 13 4
A disableTheme() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <[email protected]>
7
 * @copyright Copyright (c) 2019 Janis Köhr <[email protected]>
8
 *
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Daniel Kesselberg <[email protected]>
11
 * @author Janis Köhr <[email protected]>
12
 * @author John Molakvoæ <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license GNU AGPL version 3 or any later version
16
 *
17
 * This program is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License as
19
 * published by the Free Software Foundation, either version 3 of the
20
 * License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
 * GNU Affero General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU Affero General Public License
28
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29
 *
30
 */
31
namespace OCA\Theming\Controller;
32
33
use OCA\Theming\Service\ThemesService;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\AppFramework\OCS\OCSBadRequestException;
36
use OCP\AppFramework\OCSController;
37
use OCP\IConfig;
38
use OCP\IRequest;
39
use OCP\IUserSession;
40
use OCP\PreConditionNotMetException;
41
42
class UserThemeController extends OCSController {
43
44
	protected string $userId;
45
	private IConfig $config;
46
	private IUserSession $userSession;
47
	private ThemesService $themesService;
48
49
	/**
50
	 * Config constructor.
51
	 */
52
	public function __construct(string $appName,
53
								IRequest $request,
54
								IConfig $config,
55
								IUserSession $userSession,
56
								ThemesService $themesService) {
57
		parent::__construct($appName, $request);
58
		$this->config = $config;
59
		$this->userSession = $userSession;
60
		$this->themesService = $themesService;
61
		$this->userId = $userSession->getUser()->getUID();
62
	}
63
64
	/**
65
	 * @NoAdminRequired
66
	 *
67
	 * Enable theme
68
	 *
69
	 * @param string $themeId the theme ID
70
	 * @return DataResponse
71
	 * @throws OCSBadRequestException|PreConditionNotMetException
72
	 */
73
	public function enableTheme(string $themeId): DataResponse {
74
		if ($themeId === '' || !$themeId) {
75
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
76
		}
77
78
		$themes = $this->themesService->getThemes();
79
		if (!isset($themes[$themeId])) {
80
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
81
		}
82
		
83
		// Enable selected theme
84
		$this->themesService->enableTheme($themes[$themeId]);
85
		return new DataResponse();
86
	}
87
88
	/**
89
	 * @NoAdminRequired
90
	 *
91
	 * Disable theme
92
	 *
93
	 * @param string $themeId the theme ID
94
	 * @return DataResponse
95
	 * @throws OCSBadRequestException|PreConditionNotMetException
96
	 */
97
	public function disableTheme(string $themeId): DataResponse {
98
		if ($themeId === '' || !$themeId) {
99
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
100
		}
101
102
		$themes = $this->themesService->getThemes();
103
		if (!isset($themes[$themeId])) {
104
			throw new OCSBadRequestException('Invalid theme id: ' . $themeId);
105
		}
106
		
107
		// Enable selected theme
108
		$this->themesService->disableTheme($themes[$themeId]);
109
		return new DataResponse();
110
	}
111
}
112