Test Setup Failed
Push — master ( 4a93bf...5a5f5f )
by Daniel
23:25
created

WebsitesRequest::getWebsitesFromUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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