Completed
Pull Request — master (#362)
by Maxence
02:42
created

GSSharesRequest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 1
A getForUser() 0 15 2
A joinMembership() 0 14 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\TStringTools;
32
use OCA\Circles\Model\GlobalScale\GSShare;
33
use OCA\Circles\Model\Member;
34
use OCP\DB\QueryBuilder\IQueryBuilder;
35
36
37
/**
38
 * Class GSSharesRequest
39
 *
40
 * @package OCA\Circles\Db
41
 */
42
class GSSharesRequest extends GSSharesRequestBuilder {
43
44
45
	use TStringTools;
46
47
48
	/**
49
	 * @param GSShare $gsShare
50
	 */
51
	public function create(GSShare $gsShare): void {
52
		$hash = $this->token();
53
		$qb = $this->getGSSharesInsertSql();
54
		$qb->setValue('circle_unique_id', $qb->createNamedParameter($gsShare->getCircleId()))
55
		   ->setValue('owner', $qb->createNamedParameter($gsShare->getOwner()))
56
		   ->setValue('instance', $qb->createNamedParameter($gsShare->getInstance()))
57
		   ->setValue('token', $qb->createNamedParameter($gsShare->getToken()))
58
		   ->setValue('parent', $qb->createNamedParameter($gsShare->getParent()))
59
		   ->setValue('mountpoint', $qb->createNamedParameter($gsShare->getMountPoint()))
60
		   ->setValue('mountpoint_hash', $qb->createNamedParameter($hash));
61
		$qb->execute();
62
	}
63
64
65
	/**
66
	 * @param string $userId
67
	 *
68
	 * @return GSShare[]
69
	 */
70
	public function getForUser(string $userId): array {
71
		$qb = $this->getGSSharesSelectSql();
72
73
		$this->joinMembership($qb, $userId);
74
75
76
		$shares = [];
77
		$cursor = $qb->execute();
78
		while ($data = $cursor->fetch()) {
79
			$shares[] = $this->parseGSSharesSelectSql($data);
80
		}
81
		$cursor->closeCursor();
82
83
		return $shares;
84
	}
85
86
87
	/**
88
	 * @param IQueryBuilder $qb
89
	 * @param string $userId
90
	 */
91
	private function joinMembership(IQueryBuilder $qb, string $userId) {
92
		$qb->from(CoreRequestBuilder::TABLE_MEMBERS, 'm');
93
94
		$expr = $qb->expr();
95
		$andX = $expr->andX();
96
97
		$andX->add($expr->eq('m.user_id', $qb->createNamedParameter($userId)));
98
		$andX->add($expr->eq('m.instance', $qb->createNamedParameter('')));
99
		$andX->add($expr->gt('m.level', $qb->createNamedParameter(0)));
100
		$andX->add($expr->eq('m.user_type', $qb->createNamedParameter(Member::TYPE_USER)));
101
		$andX->add($expr->eq('m.circle_id', 'gsh.circle_unique_id'));
102
103
		$qb->andWhere($andX);
104
	}
105
106
}
107
108