Completed
Push — master ( 90f02e...53fcb4 )
by Maxence
02:59
created

FederatedLinksRequest::getFromUniqueId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
nc 1
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\Model\FederatedLink;
32
33
class FederatedLinksRequest extends FederatedLinksRequestBuilder {
34
35
36
	/**
37
	 * @param FederatedLink $link
38
	 *
39
	 * @return bool
40
	 * @throws \Exception
41
	 */
42
	public function create(FederatedLink $link) {
43
44
		if ($link === null) {
45
			return false;
46
		}
47
48
		try {
49
			$qb = $this->getLinksInsertSql();
50
			$qb->setValue('status', $qb->createNamedParameter($link->getStatus()))
51
			   ->setValue('circle_id', $qb->createNamedParameter($link->getCircleId()))
52
			   ->setValue('unique_id', $qb->createNamedParameter($link->getUniqueId()))
53
			   ->setValue('address', $qb->createNamedParameter($link->getAddress()))
54
			   ->setValue('token', $qb->createNamedParameter($link->getToken()));
55
56
			$qb->execute();
57
58
			return true;
59
		} catch (\Exception $e) {
60
			throw $e;
61
		}
62
	}
63
64
65
	public function update(FederatedLink $link) {
66
67
		if ($link->getStatus() === FederatedLink::STATUS_LINK_REMOVE) {
68
			$this->delete($link);
69
70
			return;
71
		}
72
73
		$qb = $this->getLinksUpdateSql();
74
		$expr = $qb->expr();
75
76
		$qb->set('status', $qb->createNamedParameter($link->getStatus()));
77
		if ($link->getUniqueId() !== '') {
78
			$qb->set('unique_id', $qb->createNamedParameter($link->getUniqueId()));
79
		}
80
81
		$qb->where(
82
			$expr->andX(
83
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId())),
84
				$expr->eq('token', $qb->createNamedParameter($link->getToken()))
85
			)
86
		);
87
88
		$qb->execute();
89
	}
90
91
92
	/**
93
	 * @param int $circleId
94
	 *
95
	 * @return FederatedLink[]
96
	 */
97
	public function getLinked($circleId) {
98
		$qb = $this->getLinksSelectSql();
99
		$expr = $qb->expr();
100
101
		$qb->where(
102
			$expr->andX(
103
				$expr->eq('f.circle_id', $qb->createNamedParameter((int)$circleId)),
104
				$expr->eq('f.status', $qb->createNamedParameter(9))
105
			)
106
		);
107
108
		$result = [];
109
		$cursor = $qb->execute();
110
		while ($data = $cursor->fetch()) {
111
			$entry = $this->getLinkFromEntry($data);
112
			$result[] = $entry;
113
		}
114
		$cursor->closeCursor();
115
116
		return $result;
117
	}
118
119
120
	/**
121
	 * @param int $circleId
122
	 * @param string $uniqueId
123
	 *
124
	 * @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...
125
	 */
126
	public function getFromUniqueId($circleId, $uniqueId) {
127
		$qb = $this->getLinksSelectSql();
128
		$expr = $qb->expr();
129
130
		$qb->where(
131
			$expr->andX(
132
				$expr->eq('f.circle_id', $qb->createNamedParameter((int)$circleId)),
133
				$expr->eq('f.unique_id', $qb->createNamedParameter((string)$uniqueId))
134
			)
135
		);
136
137
		$cursor = $qb->execute();
138
		$data = $cursor->fetch();
139
		$cursor->closeCursor();
140
141
		return $this->getLinkFromEntry($data);
142
	}
143
144
145 View Code Duplication
	public function uniqueness(FederatedLink $link)
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...
146
	{
147
		if ($link === null) {
148
			return;
149
		}
150
151
		$qb = $this->getLinksDeleteSql();
152
		$expr = $qb->expr();
153
154
		$qb->where(
155
			$expr->andX(
156
				$expr->eq('unique_id', $qb->createNamedParameter($link->getUniqueId())),
157
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId()))
158
			)
159
		);
160
161
		$qb->execute();
162
	}
163
164
165 View Code Duplication
	public function delete(FederatedLink $link) {
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
167
		if ($link === null) {
168
			return;
169
		}
170
171
		$qb = $this->getLinksDeleteSql();
172
		$expr = $qb->expr();
173
174
		$qb->where(
175
			$expr->andX(
176
				$expr->eq('token', $qb->createNamedParameter($link->getToken())),
177
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId()))
178
			)
179
		);
180
181
		$qb->execute();
182
	}
183
184
185
	/**
186
	 * @param $arr
187
	 *
188
	 * @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...
189
	 */
190 View Code Duplication
	public function getLinkFromEntry($data) {
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...
191
		if ($data === false || $data === null) {
192
			return null;
193
		}
194
195
		$link = new FederatedLink();
196
		$link->setId($data['id'])
197
			 ->setUniqueId($data['unique_id'])
198
			 ->setStatus($data['status'])
199
			 ->setAddress($data['address'])
200
			 ->setToken($data['token'])
201
			 ->setCircleId($data['circle_id']);
202
203
		return $link;
204
	}
205
}