Passed
Push — master ( 4164c5...f58e59 )
by Maxence
02:19
created

WebsitesService::forceDeleteWebsite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
eloc 1
nc 1
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\Service;
28
29
use OCA\CMSPico\Db\WebsitesRequest;
30
use OCA\CMSPico\Exceptions\WebsiteAlreadyExistException;
31
use OCA\CMSPico\Exceptions\WebsiteDoesNotExistException;
32
use OCA\CMSPico\Model\Webpage;
33
use OCA\CMSPico\Model\Website;
34
use OCP\IL10N;
35
36
class WebsitesService {
37
38
	/** @var IL10N */
39
	private $l10n;
40
41
	/** @var WebsitesRequest */
42
	private $websiteRequest;
43
44
	/** @var TemplatesService */
45
	private $templatesService;
46
47
	/** @var PicoService */
48
	private $picoService;
49
50
	/** @var MiscService */
51
	private $miscService;
52
53
	/**
54
	 * WebsitesService constructor.
55
	 *
56
	 * @param IL10N $l10n
57
	 * @param WebsitesRequest $websiteRequest
58
	 * @param TemplatesService $templatesService
59
	 * @param PicoService $picoService
60
	 * @param MiscService $miscService
61
	 */
62
	function __construct(
63
		IL10N $l10n, WebsitesRequest $websiteRequest, TemplatesService $templatesService,
64
		PicoService $picoService, MiscService $miscService
65
	) {
66
		$this->l10n = $l10n;
67
		$this->websiteRequest = $websiteRequest;
68
		$this->templatesService = $templatesService;
69
		$this->picoService = $picoService;
70
		$this->miscService = $miscService;
71
	}
72
73
74
	/**
75
	 * @param string $name
76
	 * @param string $userId
77
	 * @param string $site
78
	 * @param string $path
79
	 * @param int $template
80
	 *
81
	 * @throws WebsiteAlreadyExistException
82
	 */
83
	public function createWebsite($name, $userId, $site, $path, $template) {
84
		$this->templatesService->templateHasToExist($template);
85
86
		$website = new Website();
87
		$website->setName($name)
88
				->setUserId($userId)
89
				->setSite($site)
90
				->setPath($path)
91
				->setTemplateSource(TemplatesService::TEMPLATES[$template]);
92
93
		try {
94
			$website->hasToBeFilledWithValidEntries();
95
			$website = $this->websiteRequest->getWebsiteFromSite($website->getSite());
96
			throw new WebsiteAlreadyExistException($this->l10n->t('Website already exist.'));
97
		} catch (WebsiteDoesNotExistException $e) {
98
			// In fact we want the website to not exist (yet).
99
		}
100
101
		$this->templatesService->installTemplates($website);
102
		$this->websiteRequest->create($website);
103
	}
104
105
106
	/**
107
	 * @param int $siteId
108
	 * @param string $userId
109
	 */
110
	public function deleteWebsite($siteId, $userId) {
111
112
		$website = $this->getWebsiteFromId($siteId);
113
		$website->hasToBeOwnedBy($userId);
114
115
		$this->forceDeleteWebsite($website);
116
	}
117
118
119
	/**
120
	 * @param Website $website
121
	 */
122
	public function forceDeleteWebsite(Website $website) {
123
		$this->websiteRequest->delete($website);
124
	}
125
126
127
	/**
128
	 * @param string $userId
129
	 */
130
	public function onUserRemoved($userId) {
131
		$this->websiteRequest->deleteAllFromUser($userId);
132
	}
133
134
135
	/**
136
	 * @param int $siteId
137
	 *
138
	 * @return Website
139
	 */
140
	public function getWebsiteFromId($siteId) {
141
		return $this->websiteRequest->getWebsiteFromId($siteId);
142
	}
143
144
145
	/**
146
	 * @param Website $website
147
	 */
148
	public function updateWebsite(Website $website) {
149
		$this->websiteRequest->update($website);
150
151
	}
152
153
	/**
154
	 * @param string $userId
155
	 *
156
	 * @return Website[]
157
	 */
158
	public function getWebsitesFromUser($userId) {
159
		$websites = $this->websiteRequest->getWebsitesFromUserId($userId);
160
161
		return $websites;
162
	}
163
164
165
	/**
166
	 * @param string $site
167
	 * @param string $viewer
168
	 *
169
	 * @return string
170
	 */
171
	public function getWebpageFromSite($site, $viewer) {
172
173
		$website = $this->websiteRequest->getWebsiteFromSite($site);
174
		$website->setViewer($viewer);
175
176
		return $this->picoService->getContent($website);
177
	}
178
179
180
}