Completed
Push — federated-circles ( 2a3998...f4a386 )
by Maxence
02:39
created

FederatedLinksRequest::getLinked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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