Completed
Push — master ( fa4c5d...fa4c5d )
by Maxence
05:16 queued 02:40
created

CirclesRequest::updateCircle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\Circles\Db;
29
30
31
use OC\L10N\L10N;
32
use OCA\Circles\Model\Circle;
33
use OCA\Circles\Model\FederatedLink;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Model\SharingFrame;
36
use OCA\Circles\Service\MiscService;
37
use OCP\IDBConnection;
38
39
class CirclesRequest extends CirclesRequestBuilder {
40
41
	/** @var MiscService */
42
	private $miscService;
43
44
	/**
45
	 * CirclesRequest constructor.
46
	 *
47
	 * @param L10N $l10n
48
	 * @param IDBConnection $connection
49
	 * @param MiscService $miscService
50
	 */
51
	public function __construct(L10N $l10n, IDBConnection $connection, MiscService $miscService) {
52
		$this->l10n = $l10n;
53
		$this->dbConnection = $connection;
54
		$this->miscService = $miscService;
55
	}
56
57
58
	/**
59
	 * @param int $circleId
60
	 * @param string $userId
61
	 *
62
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
63
	 */
64
	public function getDetails($circleId, $userId = '') {
65
		$qb = $this->getCirclesSelectSql();
66
67
		$this->limitToId($qb, $circleId);
68
		if ($userId !== '') {
69
			$this->leftJoinUserIdAsMember($qb, $userId);
70
		}
71
72
//		$this->leftjoinOwner($qb);
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
//		$this->buildWithMemberLevel($qb, 'u.level', $level);
74
//		$this->buildWithCircleId($qb, 'c.id', $circleId);
75
//		$this->buildWithOrXTypes($qb, $userId, $type, $name, $circleId);
76
77
		$cursor = $qb->execute();
78
		$data = $cursor->fetch();
79
		$entry = $this->parseCirclesSelectSql($data);
80
81
		return $entry;
82
	}
83
84
85
	/**
86
	 * saveFrame()
87
	 *
88
	 * Insert a new entry in the database to save the SharingFrame.
89
	 *
90
	 * @param SharingFrame $frame
91
	 */
92 View Code Duplication
	public function saveFrame(SharingFrame $frame) {
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...
93
94
		$qb = $this->getSharesInsertSql();
95
		$qb->setValue('circle_id', $qb->createNamedParameter($frame->getCircleId()))
96
		   ->setValue('source', $qb->createNamedParameter($frame->getSource()))
97
		   ->setValue('type', $qb->createNamedParameter($frame->getType()))
98
		   ->setValue('headers', $qb->createNamedParameter($frame->getHeaders(true)))
99
		   ->setValue('author', $qb->createNamedParameter($frame->getAuthor()))
100
		   ->setValue('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
101
		   ->setValue('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
102
		   ->setValue('payload', $qb->createNamedParameter($frame->getPayload(true)));
103
104
		$qb->execute();
105
	}
106
107
108 View Code Duplication
	public function updateFrame(SharingFrame $frame) {
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...
109
		$qb = $this->getSharesUpdateSql($frame->getUniqueId());
110
		$qb->set('circle_id', $qb->createNamedParameter($frame->getCircleId()))
111
		   ->set('source', $qb->createNamedParameter($frame->getSource()))
112
		   ->set('type', $qb->createNamedParameter($frame->getType()))
113
		   ->set('headers', $qb->createNamedParameter($frame->getHeaders(true)))
114
		   ->set('author', $qb->createNamedParameter($frame->getAuthor()))
115
		   ->set('cloud_id', $qb->createNamedParameter($frame->getCloudId()))
116
		   ->set('unique_id', $qb->createNamedParameter($frame->getUniqueId()))
117
		   ->set('payload', $qb->createNamedParameter($frame->getPayload(true)));
118
119
		$qb->execute();
120
	}
121
122
123
	public function updateCircle(Circle $circle) {
124
		$qb = $this->getCirclesUpdateSql($circle->getId());
125
		$qb->set('name', $qb->createNamedParameter($circle->getName()))
126
		   ->set('description', $qb->createNamedParameter($circle->getDescription()))
127
		   ->set('settings', $qb->createNamedParameter($circle->getSettings(true)));
128
129
		$qb->execute();
130
	}
131
132
133
	/**
134
	 * @param string $uniqueId
135
	 *
136
	 * @return Circle
0 ignored issues
show
Documentation introduced by
Should the return type not be Circle|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
137
	 */
138
	public function getCircle($uniqueId) {
139
		$qb = $this->getCirclesSelectSql();
140
		$this->limitToUniqueId($qb, (string)$uniqueId);
141
142
		$cursor = $qb->execute();
143
		$data = $cursor->fetch();
144
		$entry = $this->parseCirclesSelectSql($data);
145
146
		return $entry;
147
	}
148
149
150
	/**
151
	 * @param int $circleId
152
	 * @param string $uniqueId
153
	 *
154
	 * @return SharingFrame
0 ignored issues
show
Documentation introduced by
Should the return type not be SharingFrame|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
155
	 */
156
	public function getFrame($circleId, $uniqueId) {
157
		$qb = $this->getSharesSelectSql();
158
		$this->limitToUniqueId($qb, (string)$uniqueId);
159
		$this->limitToCircleId($qb, (int)$circleId);
160
161
		$cursor = $qb->execute();
162
		$data = $cursor->fetch();
163
		$entry = $this->parseSharesSelectSql($data);
164
165
		return $entry;
166
	}
167
168
169
	/**
170
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
171
	 *
172
	 * @param string $token
173
	 * @param string $uniqueId
174
	 *
175
	 * @return FederatedLink
0 ignored issues
show
Documentation introduced by
Should the return type not be FederatedLink|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
176
	 */
177
	public function getLinkFromToken($token, $uniqueId) {
178
		$qb = $this->getLinksSelectSql();
179
		$this->limitToUniqueId($qb, (string)$uniqueId);
180
		$this->limitToToken($qb, (string)$token);
181
182
		$cursor = $qb->execute();
183
		$data = $cursor->fetch();
184
		$entry = $this->parseLinksSelectSql($data);
185
186
		return $entry;
187
188
189
	}
190
191
	/**
192
	 * @param integer $circleId
193
	 * @param int $level
194
	 *
195
	 * @return Member[]
196
	 */
197
	public function getMembers($circleId, $level = Member::LEVEL_MEMBER) {
198
		$qb = $this->getMembersSelectSql();
199
		$this->limitToMemberLevel($qb, $level);
200
201
		$this->joinCircles($qb, 'm.circle_id');
202
		$this->limitToCircleId($qb, $circleId);
203
204
		$qb->selectAlias('c.name', 'circle_name');
205
206
		$users = [];
207
		$cursor = $qb->execute();
208
		while ($data = $cursor->fetch()) {
209
			$member = $this->parseMembersSelectSql($data);
210
			if ($member !== null) {
211
				$users[] = $member;
212
			}
213
		}
214
		$cursor->closeCursor();
215
216
		return $users;
217
	}
218
219
220
}