Test Failed
Push — master ( dc798f...d76e2d )
by Daniel
48:22
created

WebsitesRequest::getWebsiteFromId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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\Db;
27
28
use OCA\CMSPico\Exceptions\WebsiteNotFoundException;
29
use OCA\CMSPico\Model\Website;
30
31
class WebsitesRequest extends WebsitesRequestBuilder
32
{
33
	/**
34
	 * @param Website $website
35
	 */
36
	public function create(Website $website): void
37
	{
38
		$qb = $this->getWebsitesInsertSql();
39
		$qb
40
			->setValue('name', $qb->createNamedParameter($website->getName()))
41
			->setValue('user_id', $qb->createNamedParameter($website->getUserId()))
42
			->setValue('site', $qb->createNamedParameter($website->getSite()))
43
			->setValue('theme', $qb->createNamedParameter($website->getTheme()))
44
			->setValue('type', $qb->createNamedParameter($website->getType()))
45
			->setValue('options', $qb->createNamedParameter($website->getOptionsJSON()))
46
			->setValue('path', $qb->createNamedParameter($website->getPath()));
47
48
		$qb->execute();
49
	}
50
51
	/**
52
	 * @param Website $website
53
	 */
54
	public function update(Website $website): void
55
	{
56
		$qb = $this->getWebsitesUpdateSql();
57
		$qb
58
			->set('name', $qb->createNamedParameter($website->getName()))
59
			->set('user_id', $qb->createNamedParameter($website->getUserId()))
60
			->set('site', $qb->createNamedParameter($website->getSite()))
61
			->set('theme', $qb->createNamedParameter($website->getTheme()))
62
			->set('type', $qb->createNamedParameter($website->getType()))
63
			->set('options', $qb->createNamedParameter($website->getOptionsJSON()))
64
			->set('path', $qb->createNamedParameter($website->getPath()));
65
66
		$this->limitToId($qb, $website->getId());
67
68
		$qb->execute();
69
	}
70
71
	/**
72
	 * @param Website $website
73
	 */
74
	public function delete(Website $website): void
75
	{
76
		$qb = $this->getWebsitesDeleteSql();
77
		$this->limitToId($qb, $website->getId());
78
79
		$qb->execute();
80
	}
81
82
	/**
83
	 * @param string $userId
84
	 */
85
	public function deleteAllFromUser(string $userId): void
86
	{
87
		$qb = $this->getWebsitesDeleteSql();
88
		$this->limitToUserId($qb, $userId);
89
90
		$qb->execute();
91
	}
92
93
	/**
94
	 * @param string $userId
95
	 *
96
	 * @return Website[]
97
	 */
98 4
	public function getWebsitesFromUserId(string $userId): array
99
	{
100 4
		$qb = $this->getWebsitesSelectSql();
101 4
		$this->limitToUserId($qb, $userId);
102
103 4
		$websites = [];
104 4
		$cursor = $qb->execute();
105 4
		while ($data = $cursor->fetch()) {
106
			$websites[] = $this->parseWebsitesSelectSql($data);
107
		}
108 4
		$cursor->closeCursor();
109
110 4
		return $websites;
111
	}
112
113
	/**
114
	 * @param int $siteId
115
	 *
116
	 * @return Website
117
	 * @throws WebsiteNotFoundException
118
	 */
119
	public function getWebsiteFromId(int $siteId): Website
120
	{
121
		$qb = $this->getWebsitesSelectSql();
122
		$this->limitToId($qb, $siteId);
123
124
		$cursor = $qb->execute();
125
		$data = $cursor->fetch();
126
		$cursor->closeCursor();
127
128
		if ($data === false) {
129
			throw new WebsiteNotFoundException();
130
		}
131
132
		return $this->parseWebsitesSelectSql($data);
133
	}
134
135
	/**
136
	 * @param string $site
137
	 *
138
	 * @return Website
139
	 * @throws WebsiteNotFoundException
140
	 */
141 6
	public function getWebsiteFromSite(string $site): Website
142
	{
143 6
		$qb = $this->getWebsitesSelectSql();
144 6
		$this->limitToSite($qb, $site);
145
146 6
		$cursor = $qb->execute();
147 6
		$data = $cursor->fetch();
148 6
		$cursor->closeCursor();
149
150 6
		if ($data === false) {
151 6
			throw new WebsiteNotFoundException();
152
		}
153
154
		return $this->parseWebsitesSelectSql($data);
155
	}
156
}
157