Passed
Push — master ( c59208...8079ea )
by Maxence
02:12
created

SettingsController::removeCustomTheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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\Controller;
28
29
use Exception;
30
use OCA\CMSPico\AppInfo\Application;
31
use OCA\CMSPico\Service\ConfigService;
32
use OCA\CMSPico\Service\MiscService;
33
use OCA\CMSPico\Service\TemplatesService;
34
use OCA\CMSPico\Service\ThemesService;
35
use OCA\CMSPico\Service\WebsitesService;
36
use OCP\AppFramework\Controller;
37
use OCP\AppFramework\Http;
38
use OCP\AppFramework\Http\DataResponse;
39
use OCP\IRequest;
40
41
class SettingsController extends Controller {
42
43
	/** @var string */
44
	private $userId;
45
46
	/** @var ConfigService */
47
	private $configService;
48
49
	/** @var TemplatesService */
50
	private $templatesService;
51
52
	/** @var ThemesService */
53
	private $themesService;
54
55
	/** @var WebsitesService */
56
	private $websitesService;
57
58
	/** @var MiscService */
59
	private $miscService;
60
61
62
	/**
63
	 * SettingsController constructor.
64
	 *
65
	 * @param IRequest $request
66
	 * @param string $userId
67
	 * @param ConfigService $configService
68
	 * @param TemplatesService $templatesService
69
	 * @param ThemesService $themesService
70
	 * @param WebsitesService $websitesService
71
	 * @param MiscService $miscService
72
	 */
73
	function __construct(
74
		IRequest $request, $userId, ConfigService $configService, TemplatesService $templatesService,
75
		ThemesService $themesService, WebsitesService $websitesService,
76
		MiscService $miscService
77
	) {
78
		parent::__construct(Application::APP_NAME, $request);
79
		$this->userId = $userId;
80
		$this->configService = $configService;
81
		$this->templatesService = $templatesService;
82
		$this->themesService = $themesService;
83
		$this->websitesService = $websitesService;
84
		$this->miscService = $miscService;
85
	}
86
87
88
	/**
89
	 * @NoAdminRequired
90
	 *
91
	 * @param array $data
92
	 *
93
	 * @return DataResponse
94
	 */
95
	public function createPersonalWebsite($data) {
96
97
		try {
98
			$this->websitesService->createWebsite(
99
				$data['name'], $this->userId, $data['website'], $data['path'], $data['template']
100
			);
101
102
			return $this->miscService->success(
103
				[
104
					'name'     => $data['name'],
105
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
106
				]
107
			);
108
		} catch (Exception $e) {
109
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
110
		}
111
	}
112
113
114
	/**
115
	 * @NoAdminRequired
116
	 *
117
	 * @param $data
118
	 *
119
	 * @return DataResponse
120
	 */
121
	public function removePersonalWebsite($data) {
122
123
		try {
124
			$this->websitesService->deleteWebsite($data['id'], $this->userId);
125
126
			return $this->miscService->success(
127
				[
128
					'name'     => $data['name'],
129
					'websites' => $this->websitesService->getWebsitesFromUser($this->userId)
130
				]
131
			);
132
		} catch (Exception $e) {
133
			return $this->miscService->fail(['name' => $data['name'], 'error' => $e->getMessage()]);
134
		}
135
	}
136
137
138
	/**
139
	 * @NoAdminRequired
140
	 *
141
	 * @param int $siteId
142
	 * @param string $option
143
	 * @param string $value
144
	 *
145
	 * @return DataResponse
146
	 */
147
	public function editPersonalWebsiteOption($siteId, $option, $value) {
148
149
		try {
150
			$website = $this->websitesService->getWebsiteFromId($siteId);
151
152
			$website->hasToBeOwnedBy($this->userId);
153
			$website->setOption((string)$option, (string)$value);
154
155
			$this->websitesService->updateWebsite($website);
156
157
			return $this->miscService->success(
158
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
159
			);
160
		} catch (Exception $e) {
161
			return $this->miscService->fail(['error' => $e->getMessage()]);
162
		}
163
	}
164
165
166
	/**
167
	 * @NoAdminRequired
168
	 *
169
	 * @return DataResponse
170
	 */
171
	public function getPersonalWebsites() {
172
		try {
173
			$websites = $this->websitesService->getWebsitesFromUser($this->userId);
174
175
			return $this->miscService->success(['websites' => $websites]);
176
		} catch (Exception $e) {
177
			return $this->miscService->fail(['error' => $e->getMessage()]);
178
		}
179
	}
180
181
182
	/**
183
	 * @return DataResponse
184
	 */
185
	public function getSettingsAdmin() {
186
		$data = [
187
			'templates'     => $this->templatesService->getTemplatesList(true),
188
			'templates_new' => $this->templatesService->getNewTemplatesList(),
189
			'themes'        => $this->themesService->getThemesList(true),
190
			'themes_new'    => $this->themesService->getNewThemesList()
191
		];
192
193
		return new DataResponse($data, Http::STATUS_OK);
194
	}
195
196
197
	/**
198
	 * @return DataResponse
199
	 */
200
	public function setSettingsAdmin() {
201
		return $this->getSettingsAdmin();
202
	}
203
204
205
	/**
206
	 * @param string $template
207
	 *
208
	 * @return DataResponse
209
	 */
210 View Code Duplication
	public function addCustomTemplate($template) {
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...
211
212
		$custom = $this->templatesService->getTemplatesList(true);
213
		array_push($custom, $template);
214
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
215
216
		return $this->getSettingsAdmin();
217
	}
218
219
	/**
220
	 * @param string $template
221
	 *
222
	 * @return DataResponse
223
	 */
224 View Code Duplication
	public function removeCustomTemplate($template) {
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...
225
226
		$custom = $this->templatesService->getTemplatesList(true);
227
228
		$k = array_search($template, $custom);
229
		if ($k !== false) {
230
			unset($custom[$k]);
231
		}
232
233
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
234
235
		return $this->getSettingsAdmin();
236
	}
237
238
239
	/**
240
	 * @param string $theme
241
	 *
242
	 * @return DataResponse
243
	 */
244 View Code Duplication
	public function addCustomTheme($theme) {
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...
245
246
		$custom = $this->themesService->getThemesList(true);
247
		array_push($custom, $theme);
248
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
249
250
		return $this->getSettingsAdmin();
251
	}
252
253
	/**
254
	 * @param string $theme
255
	 *
256
	 * @return DataResponse
257
	 */
258 View Code Duplication
	public function removeCustomTheme($theme) {
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...
259
		$custom = $this->themesService->getThemesList(true);
260
261
		$k = array_search($theme, $custom);
262
		if ($k !== false) {
263
			unset($custom[$k]);
264
		}
265
266
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
267
268
		return $this->getSettingsAdmin();
269
	}
270
271
}