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

WebsitesRequest::getWebsiteFromSite()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
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\Db;
28
29
30
use OCA\CMSPico\Exceptions\WebsiteDoesNotExistException;
31
use OCA\CMSPico\Model\Website;
32
33
class WebsitesRequest extends WebsitesRequestBuilder {
34
35
36
	/**
37
	 * @param Website $website
38
	 *
39
	 * @return bool
40
	 * @throws \Exception
41
	 */
42
	public function create(Website $website) {
43
		try {
44
			$qb = $this->getWebsitesInsertSql();
45
			$qb->setValue('name', $qb->createNamedParameter($website->getName()))
46
			   ->setValue('user_id', $qb->createNamedParameter($website->getUserId()))
47
			   ->setValue('site', $qb->createNamedParameter($website->getSite()))
48
			   ->setValue('type', $qb->createNamedParameter($website->getType()))
49
			   ->setValue('options', $qb->createNamedParameter($website->getOptions(true)))
50
			   ->setValue('path', $qb->createNamedParameter($website->getPath()));
51
52
			$qb->execute();
53
54
			return true;
55
		} catch (\Exception $e) {
56
			throw $e;
57
		}
58
	}
59
60
61
	/**
62
	 * @param Website $website
63
	 */
64
	public function update(Website $website) {
65
66
		$qb = $this->getWebsitesUpdateSql();
67
		$qb->set('name', $qb->createNamedParameter($website->getName()));
68
		$qb->set('user_id', $qb->createNamedParameter($website->getUserId()));
69
		$qb->set('site', $qb->createNamedParameter($website->getSite()));
70
		$qb->set('type', $qb->createNamedParameter($website->getType()));
71
		$qb->set('options', $qb->createNamedParameter($website->getOptions(true)));
72
		$qb->set('path', $qb->createNamedParameter($website->getPath()));
73
74
		$this->limitToId($qb, $website->getId());
75
76
		$qb->execute();
77
	}
78
79
80
	/**
81
	 * @param Website $website
82
	 */
83
	public function delete(Website $website) {
84
85
		$qb = $this->getWebsitesDeleteSql();
86
		$this->limitToId($qb, $website->getId());
87
88
		$qb->execute();
89
	}
90
91
92
	/**
93
	 * @param string $userId
94
	 */
95
	public function deleteAllFromUser($userId) {
96
97
		$qb = $this->getWebsitesDeleteSql();
98
		$this->limitToUserId($qb, $userId);
99
100
		$qb->execute();
101
	}
102
103
104
	/**
105
	 * return list of websites from a user.
106
	 *
107
	 * @param string $userId
108
	 *
109
	 * @return Website[]
110
	 */
111
	public function getWebsitesFromUserId($userId) {
112
		$qb = $this->getWebsitesSelectSql();
113
		$this->limitToUserId($qb, $userId);
114
115
		$websites = [];
116
		$cursor = $qb->execute();
117
		while ($data = $cursor->fetch()) {
118
			$websites[] = $this->parseWebsitesSelectSql($data);
119
		}
120
		$cursor->closeCursor();
121
122
		return $websites;
123
	}
124
125
126
	/**
127
	 * return the website corresponding to the Id
128
	 *
129
	 * @param int $siteId
130
	 *
131
	 * @return Website
132
	 * @throws WebsiteDoesNotExistException
133
	 */
134 View Code Duplication
	public function getWebsiteFromId($siteId) {
135
		$qb = $this->getWebsitesSelectSql();
136
		$this->limitToId($qb, $siteId);
137
138
		$cursor = $qb->execute();
139
		$data = $cursor->fetch();
140
		$cursor->closeCursor();
141
142
		if ($data === false) {
143
			throw new WebsiteDoesNotExistException($this->l10n->t('Website not found'));
144
		}
145
146
		return $this->parseWebsitesSelectSql($data);
147
	}
148
149
150
	/**
151
	 * return the website corresponding to the site/url
152
	 *
153
	 * @param string $site
154
	 *
155
	 * @return Website
156
	 * @throws WebsiteDoesNotExistException
157
	 */
158 View Code Duplication
	public function getWebsiteFromSite($site) {
159
		$qb = $this->getWebsitesSelectSql();
160
		$this->limitToSite($qb, $site);
161
162
		$cursor = $qb->execute();
163
		$data = $cursor->fetch();
164
		$cursor->closeCursor();
165
166
		if ($data === false) {
167
			throw new WebsiteDoesNotExistException($this->l10n->t('Website not found'));
168
		}
169
170
		return $this->parseWebsitesSelectSql($data);
171
	}
172
173
}