Passed
Push — master ( 6b84e0...00c2c4 )
by Daniel
16:34
created

WebsitesRequest::getWebsites()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.9666
cc 2
nc 2
nop 0
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
use OCP\DB\QueryBuilder\IQueryBuilder;
31
use OCP\IDBConnection;
32
33
class WebsitesRequest
34
{
35
	/** @var string */
36
	public const TABLE_NAME = 'cms_pico_websites';
37
38
	/** @var IDBConnection */
39
	protected $dbConnection;
40
41
	/**
42
	 * CoreRequestBuilder constructor.
43
	 *
44
	 * @param IDBConnection $connection
45
	 */
46 1
	public function __construct(IDBConnection $connection)
47
	{
48 1
		$this->dbConnection = $connection;
49 1
	}
50
51
	/**
52
	 * @param Website $website
53
	 */
54 4
	public function create(Website $website): void
55
	{
56 4
		$qb = $this->dbConnection->getQueryBuilder()
57 4
			->insert(WebsitesRequest::TABLE_NAME);
58
59
		$qb
60 4
			->setValue('name', $qb->createNamedParameter($website->getName()))
61 4
			->setValue('user_id', $qb->createNamedParameter($website->getUserId()))
62 4
			->setValue('site', $qb->createNamedParameter($website->getSite()))
63 4
			->setValue('theme', $qb->createNamedParameter($website->getTheme()))
64 4
			->setValue('type', $qb->createNamedParameter($website->getType()))
65 4
			->setValue('options', $qb->createNamedParameter($website->getOptionsJSON()))
66 4
			->setValue('path', $qb->createNamedParameter($website->getPath()))
67 4
			->setValue('creation', $qb->createFunction('NOW()'));
68
69 4
		$qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

69
		/** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
70
71 4
		$website->setId($qb->getLastInsertId());
72 4
	}
73
74
	/**
75
	 * @param Website $website
76
	 */
77 1
	public function update(Website $website): void
78
	{
79 1
		$qb = $this->dbConnection->getQueryBuilder()
80 1
			->update(WebsitesRequest::TABLE_NAME);
81
82
		$qb
83 1
			->set('name', $qb->createNamedParameter($website->getName()))
84 1
			->set('user_id', $qb->createNamedParameter($website->getUserId()))
85 1
			->set('site', $qb->createNamedParameter($website->getSite()))
86 1
			->set('theme', $qb->createNamedParameter($website->getTheme()))
87 1
			->set('type', $qb->createNamedParameter($website->getType()))
88 1
			->set('options', $qb->createNamedParameter($website->getOptionsJSON()))
89 1
			->set('path', $qb->createNamedParameter($website->getPath()));
90
91 1
		$this->limitToField($qb, 'id', $website->getId());
92
93 1
		$qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

93
		/** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
94 1
	}
95
96
	/**
97
	 * @param Website $website
98
	 */
99 1
	public function delete(Website $website): void
100
	{
101 1
		$qb = $this->dbConnection->getQueryBuilder()
102 1
			->delete(WebsitesRequest::TABLE_NAME);
103
104 1
		$this->limitToField($qb, 'id', $website->getId());
105
106 1
		$qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

106
		/** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107 1
	}
108
109
	/**
110
	 * @param string $userId
111
	 */
112
	public function deleteAllFromUserId(string $userId): void
113
	{
114
		$qb = $this->dbConnection->getQueryBuilder()
115
			->delete(WebsitesRequest::TABLE_NAME);
116
117
		$this->limitToField($qb, 'user_id', $userId);
118
119
		$qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

119
		/** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
	}
121
122
	/**
123
	 * @return Website[]
124
	 */
125
	public function getWebsites(): array
126
	{
127
		$qb = $this->dbConnection->getQueryBuilder()
128
			->select('*')
129
			->from(WebsitesRequest::TABLE_NAME);
130
131
		$websites = [];
132
		$cursor = $qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

132
		$cursor = /** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
133
		while ($data = $cursor->fetch()) {
134
			$websites[] = $this->createInstance($data);
135
		}
136
		$cursor->closeCursor();
137
138
		return $websites;
139
	}
140
141
	/**
142
	 * @param string $userId
143
	 *
144
	 * @return Website[]
145
	 */
146 6
	public function getWebsitesFromUserId(string $userId): array
147
	{
148 6
		$qb = $this->dbConnection->getQueryBuilder()
149 6
			->select('*')
150 6
			->from(WebsitesRequest::TABLE_NAME);
151
152 6
		$this->limitToField($qb, 'user_id', $userId);
153
154 6
		$websites = [];
155 6
		$cursor = $qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

155
		$cursor = /** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
156 6
		while ($data = $cursor->fetch()) {
157 5
			$websites[] = $this->createInstance($data);
158
		}
159 6
		$cursor->closeCursor();
160
161 6
		return $websites;
162
	}
163
164
	/**
165
	 * @param int $siteId
166
	 *
167
	 * @return Website
168
	 * @throws WebsiteNotFoundException
169
	 */
170 2
	public function getWebsiteFromId(int $id): Website
171
	{
172 2
		$qb = $this->dbConnection->getQueryBuilder()
173 2
			->select('*')
174 2
			->from(WebsitesRequest::TABLE_NAME);
175
176 2
		$this->limitToField($qb, 'id', $id);
177
178 2
		$cursor = $qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

178
		$cursor = /** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
179 2
		$data = $cursor->fetch();
180 2
		$cursor->closeCursor();
181
182 2
		if ($data === false) {
183
			throw new WebsiteNotFoundException();
184
		}
185
186 2
		return $this->createInstance($data);
187
	}
188
189
	/**
190
	 * @param string $site
191
	 *
192
	 * @return Website
193
	 * @throws WebsiteNotFoundException
194
	 */
195 12
	public function getWebsiteFromSite(string $site): Website
196
	{
197 12
		$qb = $this->dbConnection->getQueryBuilder()
198 12
			->select('*')
199 12
			->from(WebsitesRequest::TABLE_NAME);
200
201 12
		$this->limitToField($qb, 'site', $site);
202
203 12
		$cursor = $qb->execute();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\QueryBuilder\IQueryBuilder::execute() has been deprecated: 22.0.0 Use executeQuery or executeStatement ( Ignorable by Annotation )

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

203
		$cursor = /** @scrutinizer ignore-deprecated */ $qb->execute();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
204 12
		$data = $cursor->fetch();
205 12
		$cursor->closeCursor();
206
207 12
		if ($data === false) {
208 4
			throw new WebsiteNotFoundException();
209
		}
210
211 10
		return $this->createInstance($data);
212
	}
213
214
	/**
215
	 * @param array $data
216
	 *
217
	 * @return Website
218
	 */
219 15
	private function createInstance(array $data): Website
220
	{
221 15
		return new Website($data);
222
	}
223
224
	/**
225
	 * @param IQueryBuilder $qb
226
	 * @param string        $field
227
	 * @param mixed         $value
228
	 */
229 16
	private function limitToField(IQueryBuilder $qb, string $field, $value): void
230
	{
231 16
		$qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value)));
232 16
	}
233
}
234