Passed
Push — master ( 9ca045...4164c5 )
by Maxence
02:09
created

WebsitesService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
dl 0
loc 133
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A createWebsite() 0 20 2
A updateWebsite() 0 2 1
A getWebsiteFromId() 0 2 1
A forceDeleteWebsite() 0 2 1
A getWebsitesFromUser() 0 4 1
A deleteWebsite() 0 6 1
A getWebpageFromSite() 0 6 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 int $siteId
129
	 *
130
	 * @return Website
131
	 */
132
	public function getWebsiteFromId($siteId) {
133
		return $this->websiteRequest->getWebsiteFromId($siteId);
134
	}
135
136
137
	/**
138
	 * @param string $website
139
	 */
140
	public function updateWebsite($website) {
141
		$this->websiteRequest->update($website);
0 ignored issues
show
Bug introduced by
$website of type string is incompatible with the type OCA\CMSPico\Model\Website expected by parameter $website of OCA\CMSPico\Db\WebsitesRequest::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
		$this->websiteRequest->update(/** @scrutinizer ignore-type */ $website);
Loading history...
142
143
	}
144
145
	/**
146
	 * @param string $userId
147
	 *
148
	 * @return Website[]
149
	 */
150
	public function getWebsitesFromUser($userId) {
151
		$websites = $this->websiteRequest->getWebsitesFromUserId($userId);
152
153
		return $websites;
154
	}
155
156
157
	/**
158
	 * @param string $site
159
	 * @param string $viewer
160
	 *
161
	 * @return string
162
	 */
163
	public function getWebpageFromSite($site, $viewer) {
164
165
		$website = $this->websiteRequest->getWebsiteFromSite($site);
166
		$website->setViewer($viewer);
167
168
		return $this->picoService->getContent($website);
169
	}
170
171
172
}