Completed
Push — master ( e8f96e...301a71 )
by Maxence
17s
created

FederatedLinksRequest::create()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 4
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($circleId) {
80
		$qb = $this->getLinksSelectSql();
81
		$expr = $qb->expr();
82
83
		$qb->where(
84
			$expr->andX(
85
				$expr->eq('f.circle_id', $qb->createNamedParameter((int) $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($circleId, $uniqueId) {
109
		$qb = $this->getLinksSelectSql();
110
		$expr = $qb->expr();
111
112
		$qb->where(
113
			$expr->andX(
114
				$expr->eq('f.circle_id', $qb->createNamedParameter((int) $circleId)),
115
				$expr->eq('f.unique_id', $qb->createNamedParameter((string) $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 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...
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
}