Passed
Push — master ( cb6173...c886d0 )
by Daniel
53:00
created

WebsitesController::removePersonalWebsite()   A

Complexity

Conditions 3
Paths 11

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.576

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
ccs 6
cts 10
cp 0.6
rs 9.9332
cc 3
nc 11
nop 1
crap 3.576
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
		try {
93 6
			$userId = $this->userSession->getUser()->getUID();
94 6
			$data = [ 'websites' => $this->websitesService->getWebsitesFromUser($userId) ];
95 6
			return new DataResponse($data);
96
		} catch (\Throwable $e) {
97
			return $this->createErrorResponse($e);
98
		}
99
	}
100
101
	/**
102
	 * @NoAdminRequired
103
	 *
104
	 * @param array<string,string> $data
105
	 *
106
	 * @return DataResponse
107
	 */
108 3
	public function createPersonalWebsite(array $data): DataResponse
109
	{
110
		try {
111 3
			$userId = $this->userSession->getUser()->getUID();
112
113 3
			$website = (new Website())
114 3
				->setName($data['name'] ?? '')
115 3
				->setUserId($userId)
116 3
				->setSite($data['site'] ?? '')
117 3
				->setTheme($data['theme'] ?? '')
118 3
				->setPath($data['path'] ?? '');
119
120 3
			$website->assertValidOwner();
121
122 3
			$this->websitesService->createWebsite($website, $data['template'] ?? '');
123
124 2
			return $this->getPersonalWebsites();
125 1
		} catch (\Throwable $e) {
126 1
			$error = [];
127 1
			if ($e instanceof WebsiteExistsException) {
128 1
				$error += [ 'errorField' => 'site', 'error' => $this->l10n->t('Website exists.') ];
129
			} elseif ($e instanceof WebsiteInvalidOwnerException) {
130
				$error += [ 'errorField' => 'user', 'error' => $this->l10n->t('No permission.') ];
131
			} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) {
132
				$error += [ 'errorField' => $e->getField(), 'error' => $e->getMessage() ];
133
			} elseif ($e instanceof ThemeNotFoundException) {
134
				$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ];
135
			} elseif ($e instanceof ThemeNotCompatibleException) {
136
				$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ];
137
			} elseif ($e instanceof TemplateNotFoundException) {
138
				$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ];
139
			} elseif ($e instanceof TemplateNotCompatibleException) {
140
				$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ];
141
			}
142
143 1
			return $this->createErrorResponse($e, $error);
144
		}
145
	}
146
147
	/**
148
	 * @NoAdminRequired
149
	 *
150
	 * @param int     $siteId
151
	 * @param mixed[] $data
152
	 *
153
	 * @return DataResponse
154
	 */
155 1
	public function updatePersonalWebsite(int $siteId, array $data): DataResponse
156
	{
157
		try {
158 1
			$website = $this->websitesService->getWebsiteFromId($siteId);
159
160 1
			$userId = $this->userSession->getUser()->getUID();
161 1
			$website->assertOwnedBy($userId);
162
163 1
			foreach ($data as $key => $value) {
164 1
				switch ($key) {
165 1
					case 'type':
166 1
						$website->setType((int) $value);
167 1
						break;
168
169
					case 'theme':
170
						$website->setTheme($value);
171
						break;
172
173
					default:
174
						throw new WebsiteInvalidDataException();
175
				}
176
			}
177
178 1
			$this->websitesService->updateWebsite($website);
179
180 1
			return $this->getPersonalWebsites();
181
		} catch (\Throwable $e) {
182
			$error = [];
183
			if (($e instanceof WebsiteNotFoundException) || ($e instanceof WebsiteForeignOwnerException)) {
184
				$error += [ 'errorField' => 'identifier', 'error' => $this->l10n->t('Website not found.') ];
185
			} elseif ($e instanceof WebsiteInvalidDataException) {
186
				$error += [ 'errorField' => $e->getField(), 'error' => $e->getMessage() ];
187
			} elseif ($e instanceof ThemeNotFoundException) {
188
				$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t('Theme not found.') ];
189
			} elseif ($e instanceof ThemeNotCompatibleException) {
190
				$error += [ 'errorField' => 'theme', 'error' => $this->l10n->t($e->getReason()) ];
191
			} elseif ($e instanceof TemplateNotFoundException) {
192
				$error += [ 'errorField' => 'template', 'error' => $this->l10n->t('Template not found.') ];
193
			} elseif ($e instanceof TemplateNotCompatibleException) {
194
				$error += [ 'errorField' => 'template', 'error' => $this->l10n->t($e->getReason()) ];
195
			}
196
197
			return $this->createErrorResponse($e, $error);
198
		}
199
	}
200
201
	/**
202
	 * @NoAdminRequired
203
	 *
204
	 * @param int $siteId
205
	 *
206
	 * @return DataResponse
207
	 */
208 1
	public function removePersonalWebsite(int $siteId): DataResponse
209
	{
210
		try {
211 1
			$website = $this->websitesService->getWebsiteFromId($siteId);
212
213 1
			$userId = $this->userSession->getUser()->getUID();
214 1
			$website->assertOwnedBy($userId);
215
216 1
			$this->websitesService->deleteWebsite($website);
217
218 1
			return $this->getPersonalWebsites();
219
		} catch (WebsiteNotFoundException | WebsiteForeignOwnerException $e) {
220
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]);
221
		} catch (\Throwable $e) {
222
			return $this->createErrorResponse($e);
223
		}
224
	}
225
}
226