Completed
Pull Request — master (#326)
by Maxence
01:32
created

TokensRequest::getTokenFromMember()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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\TStringTools;
32
use Exception;
33
use OCA\Circles\Exceptions\TokenDoesNotExistException;
34
use OCA\Circles\Model\Member;
35
use OCA\Circles\Model\SharesToken;
36
37
38
/**
39
 * Class TokensRequest
40
 *
41
 * @package OCA\Circles\Db
42
 */
43
class TokensRequest extends TokensRequestBuilder {
44
45
46
	use TStringTools;
47
48
49
	/**
50
	 * @param string $token
51
	 *
52
	 * @return SharesToken
53
	 * @throws TokenDoesNotExistException
54
	 */
55 View Code Duplication
	public function getByToken(string $token) {
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...
56
		$qb = $this->getTokensSelectSql();
57
		$this->limitToToken($qb, $token);
58
59
		$cursor = $qb->execute();
60
		$data = $cursor->fetch();
61
		$cursor->closeCursor();
62
		if ($data === false) {
63
			throw new TokenDoesNotExistException('Unknown share token');
64
		}
65
66
		return $this->parseTokensSelectSql($data);
67
	}
68
69
70
	/**
71
	 * @param string $shareId
72
	 * @param string $circleId
73
	 * @param string $email
74
	 *
75
	 * @return SharesToken
76
	 * @throws TokenDoesNotExistException
77
	 */
78 View Code Duplication
	public function getTokenFromMember(string $shareId, string $circleId, string $email) {
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...
79
		$qb = $this->getTokensSelectSql();
80
		$this->limitToShareId($qb, $shareId);
81
		$this->limitToUserId($qb, $email);
82
		$this->limitToCircleId($qb, $circleId);
83
84
		$cursor = $qb->execute();
85
		$data = $cursor->fetch();
86
		$cursor->closeCursor();
87
		if ($data === false) {
88
			throw new TokenDoesNotExistException('Unknown share token');
89
		}
90
91
		return $this->parseTokensSelectSql($data);
92
	}
93
94
95
	/**
96
	 * @param Member $member
97
	 * @param int $shareId
98
	 *
99
	 * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
100
	 */
101
	public function generateTokenForMember(Member $member, int $shareId) {
102
		$token = $this->uuid(15);
103
		try {
104
			$qb = $this->getTokensInsertSql();
105
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
106
			   ->setValue('user_id', $qb->createNamedParameter($member->getUserId()))
107
			   ->setValue('share_id', $qb->createNamedParameter($shareId))
108
			   ->setValue('token', $qb->createNamedParameter($token));
109
110
			$qb->execute();
111
112
			return $token;
113
		} catch (Exception $e) {
114
			return '';
115
		}
116
	}
117
118
	/**
119
	 * @param Member[] $members
120
	 * @param int $shareId
121
	 */
122
	public function generateTokenForMembers(array $members, int $shareId) {
123
		foreach ($members as $member) {
124
			$this->generateTokenForMember($member, $shareId);
125
		}
126
	}
127
128
129
	/**
130
	 * @param int $shareId
131
	 */
132
	public function removeTokenByShareId(int $shareId) {
133
		$qb = $this->getTokensDeleteSql();
134
		$this->limitToShareId($qb, $shareId);
135
136
		$qb->execute();
137
	}
138
139
140
}
141