Completed
Push — federated-circles ( 1e2c73...673aab )
by Maxence
02:33
created

CirclesRequest::updateFrame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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
	/**
124
	 * @param string $uniqueId
125
	 *
126
	 * @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...
127
	 */
128
	public function getCircle(string $uniqueId) {
129
		$qb = $this->getCirclesSelectSql();
130
		$this->limitToUniqueId($qb, $uniqueId);
131
132
		$cursor = $qb->execute();
133
		$data = $cursor->fetch();
134
		$entry = $this->parseCirclesSelectSql($data);
135
136
		return $entry;
137
	}
138
139
140
	/**
141
	 * @param int $circleId
142
	 * @param string $uniqueId
143
	 *
144
	 * @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...
145
	 */
146
	public function getFrame(int $circleId, string $uniqueId) {
147
		$qb = $this->getSharesSelectSql();
148
		$this->limitToUniqueId($qb, $uniqueId);
149
		$this->limitToCircleId($qb, $circleId);
150
151
		$cursor = $qb->execute();
152
		$data = $cursor->fetch();
153
		$entry = $this->parseSharesSelectSql($data);
154
155
		return $entry;
156
	}
157
158
159
	/**
160
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
161
	 * @param string $token
162
	 * @param string $uniqueId
163
	 *
164
	 * @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...
165
	 */
166
	public function getLinkFromToken(string $token, string $uniqueId) {
167
		$qb = $this->getLinksSelectSql();
168
		$this->limitToUniqueId($qb, $uniqueId);
169
		$this->limitToToken($qb, $token);
170
171
		$cursor = $qb->execute();
172
		$data = $cursor->fetch();
173
		$entry = $this->parseLinksSelectSql($data);
174
175
		return $entry;
176
177
178
	}
179
180
	/**
181
	 * @param $circleId
182
	 * @param int $level
183
	 *
184
	 * @return Member[]
185
	 */
186
	public function getMembers($circleId, $level = Member::LEVEL_MEMBER) {
187
		$qb = $this->getMembersSelectSql();
188
		$this->limitToMemberLevel($qb, $level);
189
190
		$this->joinCircles($qb, 'm.circle_id');
191
		$this->limitToCircleId($qb, $circleId);
192
193
		$qb->selectAlias('c.name', 'circle_name');
194
195
		$users = [];
196
		$cursor = $qb->execute();
197
		while ($data = $cursor->fetch()) {
198
			$member = $this->parseMembersSelectSql($data);
199
			if ($member !== null) {
200
				$users[] = $member;
201
			}
202
		}
203
		$cursor->closeCursor();
204
205
		return $users;
206
	}
207
208
209
}