Completed
Push — federated-circles ( c0d7e5...47921f )
by Maxence
02:40
created

FederatedLinksRequest::getLinkFromEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
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 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
	 * @param string $uniqueId
77
	 *
78
	 * @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...
79
	 */
80
	public function get(int $circleId, string $uniqueId) {
81
		$qb = $this->getLinksSelectSql();
82
		$expr = $qb->expr();
83
84
		$qb->where(
85
			$expr->andX(
86
				$expr->eq('f.circle_id', $qb->createNamedParameter($circleId)),
87
				$expr->eq('f.unique_id', $qb->createNamedParameter($uniqueId))
88
			)
89
		);
90
91
		$cursor = $qb->execute();
92
		$data = $cursor->fetch();
93
94
		if ($data === false) {
95
			return null;
96
		}
97
98
		$cursor->closeCursor();
99
100
		return $this->getLinkFromEntry($data);
101
	}
102
103
104
	public function delete(FederatedLink $link) {
105
106
		if ($link === null) {
107
			return;
108
		}
109
110
		$qb = $this->getLinksDeleteSql();
111
		$expr = $qb->expr();
112
113
		$qb->where(
114
			$expr->andX(
115
				$expr->eq('token', $qb->createNamedParameter($link->getToken())),
116
				$expr->eq('circle_id', $qb->createNamedParameter($link->getCircleId()))
117
			)
118
		);
119
120
		$qb->execute();
121
	}
122
123
124
	/**
125
	 * @param $arr
126
	 *
127
	 * @return FederatedLink
128
	 */
129
	public function getLinkFromEntry($arr) {
130
131
		$link = new FederatedLink();
132
		$link->setId($arr['id'])
133
			 ->setUniqueId($arr['unique_id'])
134
			 ->setStatus($arr['status'])
135
			 ->setAddress($arr['address'])
136
			 ->setToken($arr['token'])
137
			 ->setCircleId($arr['circle_id']);
138
139
		return $link;
140
	}
141
}