Completed
Push — master ( 5a6198...a64e14 )
by Maxence
17s queued 10s
created

SharesRequest::getTokenByShareId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
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 daita\MySmallPhpTools\Traits\TArrayTools;
32
use daita\MySmallPhpTools\Traits\TStringTools;
33
use OCA\Circles\Exceptions\TokenDoesNotExistException;
34
use OCA\Circles\Model\Member;
35
36
37
/**
38
 * Class SharesRequest
39
 *
40
 * @package OCA\Circles\Db
41
 */
42
class SharesRequest extends SharesRequestBuilder {
43
44
45
	use TArrayTools;
46
	use TStringTools;
47
48
49
	/**
50
	 * remove shares from a member to a circle
51
	 *
52
	 * @param Member $member
53
	 */
54
	public function removeSharesFromMember(Member $member) {
55
		$qb = $this->getSharesDeleteSql();
56
		$expr = $qb->expr();
57
58
		$andX = $expr->andX();
59
		$andX->add($expr->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE)));
60
		$andX->add($expr->eq('share_with', $qb->createNamedParameter($member->getCircleId())));
61
		$andX->add($expr->eq('uid_initiator', $qb->createNamedParameter($member->getUserId())));
62
		$qb->andWhere($andX);
63
64
		$qb->execute();
65
	}
66
67
68
	/**
69
	 * @param int $shareId
70
	 *
71
	 * @return string
72
	 * @throws TokenDoesNotExistException
73
	 */
74 View Code Duplication
	public function getTokenByShareId(int $shareId) {
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...
75
		$qb = $this->getSharesSelectSql();
76
		$this->limitToId($qb, $shareId);
77
		$this->limitToShareType($qb, self::SHARE_TYPE);
78
79
		$cursor = $qb->execute();
80
		$data = $cursor->fetch();
81
		$cursor->closeCursor();
82
		if ($data === false) {
83
			throw new TokenDoesNotExistException('Unknown share token');
84
		}
85
86
		return $this->get('token', $data, 'notfound');
87
	}
88
89
90
	/**
91
	 * @param string $circleUniqueId
92
	 */
93
	public function shuffleTokensFromCircle(string $circleUniqueId) {
94
		$shares = $this->getSharesForCircle($circleUniqueId);
95
		foreach ($shares as $share) {
96
			$this->shuffleTokenByShareId($this->getInt('id', $share, 0));
97
		}
98
	}
99
100
101
	/**
102
	 * @param int $shareId
103
	 */
104
	public function shuffleTokenByShareId(int $shareId) {
105
		$qb = $this->getSharesUpdateSql();
106
		$qb->set('token', $qb->createNamedParameter($this->uuid(15)));
107
108
		$this->limitToShareType($qb, self::SHARE_TYPE);
109
		$this->limitToId($qb, $shareId);
110
111
		$qb->execute();
112
	}
113
114
115
	/**
116
	 * @param string $circleId
117
	 *
118
	 * @return array
119
	 */
120
	public function getSharesForCircle(string $circleId) {
121
		$qb = $this->getSharesSelectSql();
122
123
		$this->limitToShareWith($qb, $circleId);
124
		$this->limitToShareType($qb, self::SHARE_TYPE);
125
126
		$shares = [];
127
		$cursor = $qb->execute();
128
		while ($data = $cursor->fetch()) {
129
			$shares[] = $data;
130
		}
131
		$cursor->closeCursor();
132
133
		return $shares;
134
	}
135
136
137
}
138