Completed
Push — master ( 0de50b...9bec62 )
by Maxence
02:45
created

FederatedLinksRequest::getLinkFromCircle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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 OCA\Circles\Exceptions\FederatedLinkDoesNotExistException;
32
use OCA\Circles\Model\FederatedLink;
33
34
class FederatedLinksRequest extends FederatedLinksRequestBuilder {
35
36
37
	/**
38
	 * @param FederatedLink $link
39
	 *
40
	 * @return bool
41
	 * @throws \Exception
42
	 */
43
	public function create(FederatedLink $link) {
44
		try {
45
			$qb = $this->getLinksInsertSql();
46
			$qb->setValue('status', $qb->createNamedParameter($link->getStatus()))
47
			   ->setValue('circle_id', $qb->createNamedParameter($link->getCircleId()))
48
			   ->setValue('unique_id', $qb->createNamedParameter($link->getUniqueId(true)))
49
			   ->setValue('address', $qb->createNamedParameter($link->getAddress()))
50
			   ->setValue('token', $qb->createNamedParameter($link->getToken(true)));
51
52
			$qb->execute();
53
54
			return true;
55
		} catch (\Exception $e) {
56
			throw $e;
57
		}
58
	}
59
60
61
	/**
62
	 * @param FederatedLink $link
63
	 */
64
	public function update(FederatedLink $link) {
65
66
		if ($link->getStatus() === FederatedLink::STATUS_LINK_REMOVE) {
67
			$this->delete($link);
68
69
			return;
70
		}
71
72
		$qb = $this->getLinksUpdateSql();
73
		$qb->set('status', $qb->createNamedParameter($link->getStatus()));
74
		if ($link->getUniqueId() !== '') {
75
			$qb->set('unique_id', $qb->createNamedParameter($link->getUniqueId(true)));
76
		}
77
78
		$this->limitToToken($qb, $link->getToken(true));
79
		$this->limitToCircleId($qb, $link->getCircleId());
80
81
		$qb->execute();
82
	}
83
84
85
	/**
86
	 * @param FederatedLink $link
87
	 */
88
	public function delete(FederatedLink $link) {
89
90
		if ($link === null) {
91
			return;
92
		}
93
94
		$qb = $this->getLinksDeleteSql();
95
		$this->limitToToken($qb, $link->getToken(true));
96
		$this->limitToCircleId($qb, $link->getCircleId());
97
98
		$qb->execute();
99
	}
100
101
102
	/**
103
	 * @param array $data
104
	 *
105
	 * @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...
106
	 */
107
	public function getLinkFromEntry($data) {
108
		if ($data === false || $data === null) {
109
			return null;
110
		}
111
112
		$link = new FederatedLink();
113
		$link->setId($data['id'])
114
			 ->setUniqueId($data['unique_id'])
115
			 ->setStatus($data['status'])
116
			 ->setAddress($data['address'])
117
			 ->setToken($data['token'])
118
			 ->setCircleId($data['circle_id']);
119
120
		return $link;
121
	}
122
123
124
	/**
125
	 * returns all FederatedLink from a circle
126
	 *
127
	 * @param string $circleUniqueId
128
	 * @param int $status
129
	 *
130
	 * @return FederatedLink[]
131
	 */
132 View Code Duplication
	public function getLinksFromCircle($circleUniqueId, $status = 0) {
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...
133
		$qb = $this->getLinksSelectSql();
134
		$this->limitToCircleId($qb, $circleUniqueId);
135
		$this->limitToStatus($qb, $status);
136
137
		$links = [];
138
		$cursor = $qb->execute();
139
		while ($data = $cursor->fetch()) {
140
			$links[] = $this->parseLinksSelectSql($data);
141
		}
142
		$cursor->closeCursor();
143
144
		return $links;
145
	}
146
147
148
	/**
149
	 * returns a FederatedLink from a circle identified by its full unique Id
150
	 *
151
	 * @param string $circleUniqueId
152
	 * @param string $linkUniqueId
153
	 *
154
	 * @return FederatedLink
155
	 * @throws FederatedLinkDoesNotExistException
156
	 */
157 View Code Duplication
	public function getLinkFromCircle($circleUniqueId, $linkUniqueId) {
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...
158
		$qb = $this->getLinksSelectSql();
159
		$this->limitToCircleId($qb, $circleUniqueId);
160
		$this->limitToUniqueId($qb, $linkUniqueId);
161
162
		$cursor = $qb->execute();
163
		$data = $cursor->fetch();
164
		$cursor->closeCursor();
165
166
		if ($data === false) {
167
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
168
		}
169
170
		return $this->parseLinksSelectSql($data);
171
	}
172
173
174
	/**
175
	 * return the FederatedLink identified by a remote Circle UniqueId and the Token of the link
176
	 *
177
	 * @param string $token
178
	 * @param string $uniqueId
179
	 *
180
	 * @return FederatedLink
181
	 * @throws FederatedLinkDoesNotExistException
182
	 */
183 View Code Duplication
	public function getLinkFromToken($token, $uniqueId) {
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...
184
		$qb = $this->getLinksSelectSql();
185
		$this->limitToUniqueId($qb, (string)$uniqueId);
186
		$this->limitToToken($qb, (string)$token);
187
188
		$cursor = $qb->execute();
189
		$data = $cursor->fetch();
190
		$cursor->closeCursor();
191
192
		if ($data === false) {
193
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
194
		}
195
196
		return $this->parseLinksSelectSql($data);
197
	}
198
199
200
	/**
201
	 * return the FederatedLink identified by a its Id
202
	 *
203
	 * @param string $linkUniqueId
204
	 *
205
	 * @return FederatedLink
206
	 * @throws FederatedLinkDoesNotExistException
207
	 */
208 View Code Duplication
	public function getLinkFromId($linkUniqueId) {
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...
209
		$qb = $this->getLinksSelectSql();
210
		$this->limitToShortenUniqueId($qb, $linkUniqueId, FederatedLink::SHORT_UNIQUE_ID_LENGTH);
211
212
		$cursor = $qb->execute();
213
		$data = $cursor->fetch();
214
		$cursor->closeCursor();
215
216
		if ($data === false) {
217
			throw new FederatedLinkDoesNotExistException($this->l10n->t('Federated link not found'));
218
		}
219
220
		$entry = $this->parseLinksSelectSql($data);
221
222
		return $entry;
223
	}
224
225
226
}