Test Setup Failed
Push — master ( 924e3b...3be9b3 )
by Daniel
30:53
created

WebsitesController::removePersonalWebsite()   A

Complexity

Conditions 4
Paths 13

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.9
cc 4
nc 13
nop 1
crap 20
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
47
class WebsitesController extends Controller
48
{
49
	use ControllerTrait;
50
	/** @var string */
51
	private $userId;
52
53
	/** @var IL10N */
54
	private $l10n;
55
56
	/** @var WebsitesService */
57
	private $websitesService;
58
59
	/**
60
	 * WebsitesController constructor.
61
	 *
62
	 * @param IRequest        $request
63
	 * @param string          $userId
64
	 * @param IL10N           $l10n
65
	 * @param ILogger         $logger
66
	 * @param WebsitesService $websitesService
67
	 */
68
	public function __construct(
69
		IRequest $request,
70
		$userId,
71
		IL10N $l10n,
72
		ILogger $logger,
73
		WebsitesService $websitesService
74
	) {
75
		parent::__construct(Application::APP_NAME, $request);
76
77
		$this->userId = $userId;
78
		$this->l10n = $l10n;
79
		$this->logger = $logger;
80
		$this->websitesService = $websitesService;
81
	}
82
83
	/**
84
	 * @NoAdminRequired
85
	 *
86
	 * @return DataResponse
87
	 */
88
	public function getPersonalWebsites(): DataResponse
89
	{
90
		$data = [ 'websites' => $this->websitesService->getWebsitesFromUser($this->userId) ];
91
		return new DataResponse($data, Http::STATUS_OK);
92
	}
93
94
	/**
95
	 * @NoAdminRequired
96
	 *
97
	 * @param array<string,string> $data
98
	 *
99
	 * @return DataResponse
100
	 */
101
	public function createPersonalWebsite(array $data): DataResponse
102
	{
103
		try {
104
			$website = (new Website())
105
				->setName($data['name'])
106
				->setUserId($this->userId)
107
				->setSite($data['site'])
108
				->setTheme($data['theme'])
109
				->setPath($data['path'])
110
				->setTemplateSource($data['template']);
111
112
			$this->websitesService->createWebsite($website);
113
114
			return $this->getPersonalWebsites();
115
		} catch (\Exception $e) {
116
			$error = [];
117
			if ($e instanceof WebsiteExistsException) {
118
				$error['error'] = [ 'field' => 'site', 'message' => $this->l10n->t('Website exists.') ];
119
			} elseif ($e instanceof WebsiteInvalidOwnerException) {
120
				$error['error'] = [ 'field' => 'user', 'message' => $this->l10n->t('No permission.') ];
121
			} elseif (($e instanceof WebsiteInvalidDataException) && $e->getField()) {
122
				$error['error'] = [ 'field' => $e->getField(), 'message' => $e->getMessage() ];
123
			} elseif ($e instanceof ThemeNotFoundException) {
124
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t('Theme not found.') ];
125
			} elseif ($e instanceof ThemeNotCompatibleException) {
126
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t($e->getReason()) ];
127
			} elseif ($e instanceof TemplateNotFoundException) {
128
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t('Template not found.') ];
129
			} elseif ($e instanceof TemplateNotCompatibleException) {
130
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t($e->getReason()) ];
131
			}
132
133
			return $this->createErrorResponse($e, $error);
134
		}
135
	}
136
137
	/**
138
	 * @NoAdminRequired
139
	 *
140
	 * @param int     $siteId
141
	 * @param mixed[] $data
142
	 *
143
	 * @return DataResponse
144
	 */
145
	public function updatePersonalWebsite(int $siteId, array $data): DataResponse
146
	{
147
		try {
148
			$website = $this->websitesService->getWebsiteFromId($siteId);
149
150
			$website->assertOwnedBy($this->userId);
151
152
			foreach ($data as $key => $value) {
153
				switch ($key) {
154
					case 'type':
155
						$website->setType((int) $value);
156
						break;
157
158
					case 'theme':
159
						$website->setTheme($value);
160
						break;
161
162
					default:
163
						throw new WebsiteInvalidDataException();
164
				}
165
			}
166
167
			$this->websitesService->updateWebsite($website);
168
169
			return $this->getPersonalWebsites();
170
		} catch (\Exception $e) {
171
			$error = [];
172
			if (($e instanceof WebsiteNotFoundException) || ($e instanceof WebsiteForeignOwnerException)) {
173
				$error['error'] = [ 'field' => 'identifier', 'message' => $this->l10n->t('Website not found.') ];
174
			} elseif ($e instanceof WebsiteInvalidDataException) {
175
				$error['error'] = [ 'field' => $e->getField(), 'message' => $e->getMessage() ];
176
			} elseif ($e instanceof ThemeNotFoundException) {
177
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t('Theme not found.') ];
178
			} elseif ($e instanceof ThemeNotCompatibleException) {
179
				$error['error'] = [ 'field' => 'theme', 'message' => $this->l10n->t($e->getReason()) ];
180
			} elseif ($e instanceof TemplateNotFoundException) {
181
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t('Template not found.') ];
182
			} elseif ($e instanceof TemplateNotCompatibleException) {
183
				$error['error'] = [ 'field' => 'template', 'message' => $this->l10n->t($e->getReason()) ];
184
			}
185
186
			return $this->createErrorResponse($e, $error);
187
		}
188
	}
189
190
	/**
191
	 * @NoAdminRequired
192
	 *
193
	 * @param int $siteId
194
	 *
195
	 * @return DataResponse
196
	 */
197
	public function removePersonalWebsite(int $siteId): DataResponse
198
	{
199
		try {
200
			$website = $this->websitesService->getWebsiteFromId($siteId);
201
202
			$website->assertOwnedBy($this->userId);
203
204
			$this->websitesService->deleteWebsite($website);
205
206
			return $this->getPersonalWebsites();
207
		} catch (WebsiteNotFoundException $e) {
208
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]);
209
		} catch (WebsiteForeignOwnerException $e) {
210
			return $this->createErrorResponse($e, [ 'error' => $this->l10n->t('Website not found.') ]);
211
		} catch (\Exception $e) {
212
			return $this->createErrorResponse($e);
213
		}
214
	}
215
}
216