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

WebsitesRequestBuilder::getWebsitesInsertSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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