Passed
Push — master ( fa41e0...4bd63f )
by Daniel
07:07 queued 12s
created

WebsitesRequest::createInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
	 * @param string $userId
124
	 *
125
	 * @return Website[]
126
	 */
127 6
	public function getWebsitesFromUserId(string $userId): array
128
	{
129 6
		$qb = $this->dbConnection->getQueryBuilder()
130 6
			->select('*')
131 6
			->from(WebsitesRequest::TABLE_NAME);
132
133 6
		$this->limitToField($qb, 'user_id', $userId);
134
135 6
		$websites = [];
136 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

136
		$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...
137 6
		while ($data = $cursor->fetch()) {
138 5
			$websites[] = $this->createInstance($data);
139
		}
140 6
		$cursor->closeCursor();
141
142 6
		return $websites;
143
	}
144
145
	/**
146
	 * @param int $siteId
147
	 *
148
	 * @return Website
149
	 * @throws WebsiteNotFoundException
150
	 */
151 2
	public function getWebsiteFromId(int $id): Website
152
	{
153 2
		$qb = $this->dbConnection->getQueryBuilder()
154 2
			->select('*')
155 2
			->from(WebsitesRequest::TABLE_NAME);
156
157 2
		$this->limitToField($qb, 'id', $id);
158
159 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

159
		$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...
160 2
		$data = $cursor->fetch();
161 2
		$cursor->closeCursor();
162
163 2
		if ($data === false) {
164
			throw new WebsiteNotFoundException();
165
		}
166
167 2
		return $this->createInstance($data);
168
	}
169
170
	/**
171
	 * @param string $site
172
	 *
173
	 * @return Website
174
	 * @throws WebsiteNotFoundException
175
	 */
176 12
	public function getWebsiteFromSite(string $site): Website
177
	{
178 12
		$qb = $this->dbConnection->getQueryBuilder()
179 12
			->select('*')
180 12
			->from(WebsitesRequest::TABLE_NAME);
181
182 12
		$this->limitToField($qb, 'site', $site);
183
184 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

184
		$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...
185 12
		$data = $cursor->fetch();
186 12
		$cursor->closeCursor();
187
188 12
		if ($data === false) {
189 4
			throw new WebsiteNotFoundException();
190
		}
191
192 10
		return $this->createInstance($data);
193
	}
194
195
	/**
196
	 * @param array $data
197
	 *
198
	 * @return Website
199
	 */
200 15
	private function createInstance(array $data): Website
201
	{
202 15
		return new Website($data);
203
	}
204
205
	/**
206
	 * @param IQueryBuilder $qb
207
	 * @param string        $field
208
	 * @param mixed         $value
209
	 */
210 16
	private function limitToField(IQueryBuilder $qb, string $field, $value): void
211
	{
212 16
		$qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value)));
213 16
	}
214
}
215