Passed
Pull Request — master (#99)
by Daniel
26:02
created

WebsitesController   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 48.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 33
eloc 94
c 2
b 0
f 0
dl 0
loc 187
ccs 44
cts 90
cp 0.4889
rs 9.76

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getPersonalWebsites() 0 5 1
B createPersonalWebsite() 0 36 10
A removePersonalWebsite() 0 17 4
C updatePersonalWebsite() 0 57 17
1
<?php
2
/**
3
 * CMS Pico - Create websites using Pico CMS for Nextcloud.
4
 *
5
 * @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>)
6
 * @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>)
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
declare(strict_types=1);
25
26
namespace OCA\CMSPico\Controller;
27
28
use OCA\CMSPico\AppInfo\Application;
29
use OCA\CMSPico\Exceptions\TemplateNotCompatibleException;
30
use OCA\CMSPico\Exceptions\TemplateNotFoundException;
31
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException;
32
use OCA\CMSPico\Exceptions\ThemeNotFoundException;
33
use OCA\CMSPico\Exceptions\WebsiteExistsException;
34
use OCA\CMSPico\Exceptions\WebsiteForeignOwnerException;
35
use OCA\CMSPico\Exceptions\WebsiteInvalidDataException;
36
use OCA\CMSPico\Exceptions\WebsiteInvalidOwnerException;
37
use OCA\CMSPico\Exceptions\WebsiteNotFoundException;
38
use OCA\CMSPico\Model\Website;
39
use OCA\CMSPico\Service\WebsitesService;
40
use OCP\AppFramework\Controller;
41
use OCP\AppFramework\Http;
42
use OCP\AppFramework\Http\DataResponse;
43
use OCP\IL10N;
44
use OCP\ILogger;
45
use OCP\IRequest;
46
use OCP\IUserSession;
47
48
class WebsitesController extends Controller
49
{
50
	use ControllerTrait;
51
52
	/** @var IUserSession */
53
	private $userSession;
54
55
	/** @var IL10N */
56
	private $l10n;
57
58
	/** @var WebsitesService */
59
	private $websitesService;
60
61
	/**
62
	 * WebsitesController constructor.
63
	 *
64
	 * @param IRequest        $request
65
	 * @param IUserSession    $userSession
66
	 * @param IL10N           $l10n
67
	 * @param ILogger         $logger
68
	 * @param WebsitesService $websitesService
69
	 */
70 1
	public function __construct(
71
		IRequest $request,
72
		IUserSession $userSession,
73
		IL10N $l10n,
74
		ILogger $logger,
75
		WebsitesService $websitesService
76
	) {
77 1
		parent::__construct(Application::APP_NAME, $request);
78
79 1
		$this->userSession = $userSession;
80 1
		$this->l10n = $l10n;
81 1
		$this->logger = $logger;
82 1
		$this->websitesService = $websitesService;
83 1
	}
84
85
	/**
86
	 * @NoAdminRequired
87
	 *
88
	 * @return DataResponse
89
	 */
90 6
	public function getPersonalWebsites(): DataResponse
91
	{
92 6
		$userId = $this->userSession->getUser()->getUID();
93 6
		$data = [ 'websites' => $this->websitesService->getWebsitesFromUser($userId) ];
94 6
		return new DataResponse($data, Http::STATUS_OK);
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 *
100
	 * @param array<string,string> $data
101
	 *
102
	 * @return DataResponse
103
	 */
104 3
	public function createPersonalWebsite(array $data): DataResponse
105
	{
106 3
		$userId = $this->userSession->getUser()->getUID();
107
108
		try {
109 3
			$website = (new Website())
110 3
				->setName($data['name'] ?? '')
111 3
				->setUserId($userId)
112 3
				->setSite($data['site'] ?? '')
113 3
				->setTheme($data['theme'] ?? '')
114 3
				->setPath($data['path'] ?? '');
115
116 3
			$website->assertValidOwner();
117
118 3
			$this->websitesService->createWebsite($website, $data['template'] ?? '');
119
120 2
			return $this->getPersonalWebsites();
121 1
		} catch (\Exception $e) {
122 1
			$error = [];
123 1
			if ($e instanceof WebsiteExistsException) {
124 1
				$error['error'] = [ 'field' => 'site', 'message' => $this->l10n->t('Website exists.') ];
125
			} elseif ($e instanceof WebsiteInvalidOwnerException) {
126
				$error['error'] = [ 'field' => 'user', 'message' => $this->l10n->t('No permission.') ];
127
			} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) {
128
				$error['error'] = [ 'field' => $e->getField(), 'message' => $e->getMessage() ];
129
			} elseif ($e instanceof ThemeNotFoundException) {
130
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t('Theme not found.') ];
131
			} elseif ($e instanceof ThemeNotCompatibleException) {
132
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t($e->getReason()) ];
133
			} elseif ($e instanceof TemplateNotFoundException) {
134
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t('Template not found.') ];
135
			} elseif ($e instanceof TemplateNotCompatibleException) {
136
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t($e->getReason()) ];
137
			}
138
139 1
			return $this->createErrorResponse($e, $error);
140
		}
141
	}
142
143
	/**
144
	 * @NoAdminRequired
145
	 *
146
	 * @param int     $siteId
147
	 * @param mixed[] $data
148
	 *
149
	 * @return DataResponse
150
	 */
151 1
	public function updatePersonalWebsite(int $siteId, array $data): DataResponse
152
	{
153
		try {
154 1
			$website = $this->websitesService->getWebsiteFromId($siteId);
155
156 1
			$userId = $this->userSession->getUser()->getUID();
157 1
			$website->assertOwnedBy($userId);
158
159 1
			foreach ($data as $key => $value) {
160 1
				switch ($key) {
161 1
					case 'type':
162 1
						$website->setType((int) $value);
163 1
						break;
164
165
					case 'theme':
166
						$website->setTheme($value);
167
						break;
168
169
					case 'options':
170
						foreach ($value as $optionKey => $optionValue) {
171
							switch ($optionKey) {
172
								case 'group_access':
173
									$groupAccess = $optionValue ? explode('|', $optionValue) : [];
174
									$website->setOption($optionKey, $groupAccess ?: null);
175
									break;
176
177
								default:
178
									throw new WebsiteInvalidDataException();
179
							}
180
						}
181
						break;
182
183
					default:
184
						throw new WebsiteInvalidDataException();
185
				}
186
			}
187
188 1
			$this->websitesService->updateWebsite($website);
189
190 1
			return $this->getPersonalWebsites();
191
		} catch (\Exception $e) {
192
			$error = [];
193
			if (($e instanceof WebsiteNotFoundException) || ($e instanceof WebsiteForeignOwnerException)) {
194
				$error['error'] = [ 'field' => 'identifier', 'message' => $this->l10n->t('Website not found.') ];
195
			} elseif ($e instanceof WebsiteInvalidDataException) {
196
				$error['error'] = [ 'field' => $e->getField(), 'message' => $e->getMessage() ];
197
			} elseif ($e instanceof ThemeNotFoundException) {
198
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t('Theme not found.') ];
199
			} elseif ($e instanceof ThemeNotCompatibleException) {
200
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t($e->getReason()) ];
201
			} elseif ($e instanceof TemplateNotFoundException) {
202
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t('Template not found.') ];
203
			} elseif ($e instanceof TemplateNotCompatibleException) {
204
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t($e->getReason()) ];
205
			}
206
207
			return $this->createErrorResponse($e, $error);
208
		}
209
	}
210
211
	/**
212
	 * @NoAdminRequired
213
	 *
214
	 * @param int $siteId
215
	 *
216
	 * @return DataResponse
217
	 */
218 1
	public function removePersonalWebsite(int $siteId): DataResponse
219
	{
220
		try {
221 1
			$website = $this->websitesService->getWebsiteFromId($siteId);
222
223 1
			$userId = $this->userSession->getUser()->getUID();
224 1
			$website->assertOwnedBy($userId);
225
226 1
			$this->websitesService->deleteWebsite($website);
227
228 1
			return $this->getPersonalWebsites();
229
		} catch (WebsiteNotFoundException $e) {
230
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]);
231
		} catch (WebsiteForeignOwnerException $e) {
232
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]);
233
		} catch (\Exception $e) {
234
			return $this->createErrorResponse($e);
235
		}
236
	}
237
}
238