Passed
Push — master ( d11704...85c314 )
by Maxence
02:03
created

SettingsController::updateWebsiteTheme()   A

Complexity

Conditions 2
Paths 7

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 7
nop 2
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 $theme
143
	 *
144
	 * @return DataResponse
145
	 */
146 View Code Duplication
	public function updateWebsiteTheme($siteId, $theme) {
1 ignored issue
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...
147
148
		try {
149
			$website = $this->websitesService->getWebsiteFromId($siteId);
150
151
			$website->hasToBeOwnedBy($this->userId);
152
			$website->setTheme((string)$theme);
153
154
			$this->themesService->hasToBeAValidTheme($theme);
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
	 * @param int $siteId
170
	 * @param string $option
171
	 * @param string $value
172
	 *
173
	 * @return DataResponse
174
	 */
175 View Code Duplication
	public function editPersonalWebsiteOption($siteId, $option, $value) {
1 ignored issue
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...
176
177
		try {
178
			$website = $this->websitesService->getWebsiteFromId($siteId);
179
180
			$website->hasToBeOwnedBy($this->userId);
181
			$website->setOption((string)$option, (string)$value);
182
183
			$this->websitesService->updateWebsite($website);
184
185
			return $this->miscService->success(
186
				['websites' => $this->websitesService->getWebsitesFromUser($this->userId)]
187
			);
188
		} catch (Exception $e) {
189
			return $this->miscService->fail(['error' => $e->getMessage()]);
190
		}
191
	}
192
193
194
	/**
195
	 * @NoAdminRequired
196
	 *
197
	 * @return DataResponse
198
	 */
199
	public function getPersonalWebsites() {
200
		try {
201
			$websites = $this->websitesService->getWebsitesFromUser($this->userId);
202
203
			return $this->miscService->success(
204
				[
205
					'themes'   => $this->themesService->getThemesList(),
206
					'websites' => $websites
207
				]
208
			);
209
		} catch (Exception $e) {
210
			return $this->miscService->fail(['error' => $e->getMessage()]);
211
		}
212
	}
213
214
215
	/**
216
	 * @return DataResponse
217
	 */
218
	public function getSettingsAdmin() {
219
		$data = [
220
			'templates'     => $this->templatesService->getTemplatesList(true),
221
			'templates_new' => $this->templatesService->getNewTemplatesList(),
222
			'themes'        => $this->themesService->getThemesList(true),
223
			'themes_new'    => $this->themesService->getNewThemesList()
224
		];
225
226
		return new DataResponse($data, Http::STATUS_OK);
227
	}
228
229
230
	/**
231
	 * @return DataResponse
232
	 */
233
	public function setSettingsAdmin() {
234
		return $this->getSettingsAdmin();
235
	}
236
237
238
	/**
239
	 * @param string $template
240
	 *
241
	 * @return DataResponse
242
	 */
243 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...
244
245
		$custom = $this->templatesService->getTemplatesList(true);
246
		array_push($custom, $template);
247
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
248
249
		return $this->getSettingsAdmin();
250
	}
251
252
	/**
253
	 * @param string $template
254
	 *
255
	 * @return DataResponse
256
	 */
257 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...
258
259
		$custom = $this->templatesService->getTemplatesList(true);
260
261
		$k = array_search($template, $custom);
262
		if ($k !== false) {
263
			unset($custom[$k]);
264
		}
265
266
		$this->configService->setAppValue(ConfigService::CUSTOM_TEMPLATES, json_encode($custom));
267
268
		return $this->getSettingsAdmin();
269
	}
270
271
272
	/**
273
	 * @param string $theme
274
	 *
275
	 * @return DataResponse
276
	 */
277 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...
278
279
		$custom = $this->themesService->getThemesList(true);
280
		array_push($custom, $theme);
281
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
282
283
		return $this->getSettingsAdmin();
284
	}
285
286
	/**
287
	 * @param string $theme
288
	 *
289
	 * @return DataResponse
290
	 */
291 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...
292
293
		$custom = $this->themesService->getThemesList(true);
294
295
		$k = array_search($theme, $custom);
296
		if ($k !== false) {
297
			unset($custom[$k]);
298
		}
299
300
		$this->configService->setAppValue(ConfigService::CUSTOM_THEMES, json_encode($custom));
301
302
		return $this->getSettingsAdmin();
303
	}
304
305
}