Completed
Push — master ( bca2e7...1f8246 )
by Maxence
02:35 queued 11s
created

CirclesQueryHelper::limitToSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles;
33
34
35
use OCA\Circles\Db\CoreQueryBuilder;
36
use OCA\Circles\Db\CoreRequestBuilder;
37
use OCA\Circles\Exceptions\CircleNotFoundException;
38
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
39
use OCA\Circles\Exceptions\RequestBuilderException;
40
use OCA\Circles\Model\Circle;
41
use OCA\Circles\Service\FederatedUserService;
42
use OCP\DB\QueryBuilder\ICompositeExpression;
43
use OCP\DB\QueryBuilder\IQueryBuilder;
44
45
46
/**
47
 * Class CirclesQueryHelper
48
 *
49
 * @package OCA\Circles
50
 */
51
class CirclesQueryHelper {
52
53
54
	/** @var CoreRequestBuilder */
55
	private $coreRequestBuilder;
56
57
	/** @var CoreQueryBuilder */
58
	private $queryBuilder;
59
60
	/** @var FederatedUserService */
61
	private $federatedUserService;
62
63
64
	/**
65
	 * CirclesQueryHelper constructor.
66
	 *
67
	 * @param CoreRequestBuilder $coreRequestBuilder
68
	 * @param FederatedUserService $federatedUserService
69
	 */
70
	public function __construct(
71
		CoreRequestBuilder $coreRequestBuilder,
72
		FederatedUserService $federatedUserService
73
	) {
74
		$this->coreRequestBuilder = $coreRequestBuilder;
75
		$this->federatedUserService = $federatedUserService;
76
	}
77
78
79
	/**
80
	 * @return IQueryBuilder
81
	 */
82
	public function getQueryBuilder(): IQueryBuilder {
83
		$this->queryBuilder = $this->coreRequestBuilder->getQueryBuilder();
84
85
		return $this->queryBuilder;
86
	}
87
88
89
	/**
90
	 * @param string $alias
91
	 * @param string $field
92
	 * @param bool $fullDetails
93
	 *
94
	 * @return ICompositeExpression
95
	 * @throws RequestBuilderException
96
	 * @throws FederatedUserNotFoundException
97
	 */
98
	public function limitToSession(
99
		string $alias,
100
		string $field,
101
		bool $fullDetails = false
102
	): ICompositeExpression {
103
		$session = $this->federatedUserService->getCurrentUser();
104
		if (is_null($session)) {
105
			throw new FederatedUserNotFoundException('session not initiated');
106
		}
107
108
		$this->queryBuilder->setDefaultSelectAlias($alias);
109
		$this->queryBuilder->setOptions(
110
			[CoreQueryBuilder::HELPER],
111
			[
112
				'getData'      => $fullDetails,
113
				'mustBeMember' => true
114
			]
115
		);
116
117
		return $this->queryBuilder->limitToInitiator(
118
			CoreQueryBuilder::HELPER,
119
			$session,
120
			$field,
121
			$alias
122
		);
123
	}
124
125
126
	/**
127
	 * @param string $alias
128
	 * @param string $field
129
	 * @param IFederatedUser $federatedUser
130
	 * @param bool $fullDetails
131
	 *
132
	 * @return ICompositeExpression
133
	 * @throws RequestBuilderException
134
	 */
135 View Code Duplication
	public function limitToInheritedMembers(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
		string $alias,
137
		string $field,
138
		IFederatedUser $federatedUser,
139
		bool $fullDetails = false
140
	): ICompositeExpression {
141
		$this->queryBuilder->setDefaultSelectAlias($alias);
142
		$this->queryBuilder->setOptions(
143
			[CoreQueryBuilder::HELPER],
144
			[
145
				'getData'      => $fullDetails,
146
				'mustBeMember' => true
147
			]
148
		);
149
150
		return $this->queryBuilder->limitToInitiator(
151
			CoreQueryBuilder::HELPER,
152
			$federatedUser,
153
			$field,
154
			$alias
155
		);
156
	}
157
158
159
	/**
160
	 * @param string $field
161
	 * @param string $alias
162
	 *
163
	 * @throws RequestBuilderException
164
	 */
165 View Code Duplication
	public function addCircleDetails(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
166
		string $alias,
167
		string $field
168
	): void {
169
		$this->queryBuilder->setDefaultSelectAlias($alias);
170
		$this->queryBuilder->setOptions(
171
			[CoreQueryBuilder::HELPER],
172
			[
173
				'getData' => true
174
			]
175
		);
176
177
		$this->queryBuilder->leftJoinCircle(CoreQueryBuilder::HELPER, null, $field, $alias);
178
	}
179
180
181
	/**
182
	 * @param array $data
183
	 *
184
	 * @return Circle
185
	 * @throws CircleNotFoundException
186
	 */
187
	public function extractCircle(array $data): Circle {
188
		$circle = new Circle();
189
		$circle->importFromDatabase(
190
			$data,
191
			CoreQueryBuilder::HELPER . '_' . CoreQueryBuilder::CIRCLE . '_'
192
		);
193
194
		return $circle;
195
	}
196
197
}
198
199