Test Failed
Push — master ( 0beeeb...19c15a )
by Daniel
23:53
created

WebsitesRequestBuilder::parseWebsitesSelectSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
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 OCP\DB\QueryBuilder\IQueryBuilder;
29
use OCP\IDBConnection;
30
31
class WebsitesRequestBuilder
32
{
33
	/** @var string */
34
	public const TABLE_WEBSITES = 'cms_pico_websites';
35
36
	/** @var IDBConnection */
37
	protected $dbConnection;
38
39
	/**
40
	 * CoreRequestBuilder constructor.
41
	 *
42
	 * @param IDBConnection $connection
43
	 */
44
	public function __construct(IDBConnection $connection)
45
	{
46
		$this->dbConnection = $connection;
47
	}
48
49
	/**
50
	 * @return IQueryBuilder
51
	 */
52
	protected function getWebsitesSelectSql(): IQueryBuilder
53
	{
54
		return $this->dbConnection->getQueryBuilder()
55
			->select('*')
56
			->from(self::TABLE_WEBSITES);
57
	}
58 10
59
	/**
60 10
	 * @return IQueryBuilder
61 10
	 */
62 10
	protected function getWebsitesInsertSql(): IQueryBuilder
63
	{
64
		$qb = $this->dbConnection->getQueryBuilder();
65
		$qb
66
			->insert(self::TABLE_WEBSITES)
67
			->setValue('creation', $qb->createFunction('NOW()'));
68
69
		return $qb;
70
	}
71
72
	/**
73
	 * @return IQueryBuilder
74
	 */
75
	protected function getWebsitesUpdateSql(): IQueryBuilder
76
	{
77
		return $this->dbConnection->getQueryBuilder()
78
			->update(self::TABLE_WEBSITES);
79
	}
80
81
	/**
82
	 * @return IQueryBuilder
83
	 */
84
	protected function getWebsitesDeleteSql(): IQueryBuilder
85
	{
86
		return $this->dbConnection->getQueryBuilder()
87
			->delete(self::TABLE_WEBSITES);
88
	}
89
90
	/**
91
	 * Limit the request by id.
92
	 *
93
	 * @param IQueryBuilder $qb
94
	 * @param int           $id
95
	 */
96
	protected function limitToId(IQueryBuilder $qb, int $id): void
97
	{
98
		$this->limitToDBField($qb, 'id', $id);
99
	}
100
101
	/**
102
	 * Limit the request to a user.
103
	 *
104
	 * @param IQueryBuilder $qb
105
	 * @param string        $userId
106
	 */
107
	protected function limitToUserId(IQueryBuilder $qb, string $userId): void
108
	{
109
		$this->limitToDBField($qb, 'user_id', $userId);
110
	}
111
112
	/**
113
	 * Limit the request to a site.
114
	 *
115
	 * @param IQueryBuilder $qb
116
	 * @param string        $site
117
	 */
118
	protected function limitToSite(IQueryBuilder $qb, string $site): void
119
	{
120
		$this->limitToDBField($qb, 'site', $site);
121
	}
122
123
	/**
124
	 * @param IQueryBuilder $qb
125
	 * @param string        $field
126
	 * @param mixed         $value
127
	 */
128
	private function limitToDBField(IQueryBuilder $qb, string $field, $value): void
129
	{
130
		$qb->andWhere($qb->expr()->eq($field, $qb->createNamedParameter($value)));
131
	}
132
}
133