Completed
Push — master ( 5b722b...aea56e )
by Maxence
02:59
created

MembersRequest::getMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
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 Doctrine\DBAL\Exception\UniqueConstraintViolationException;
32
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
33
use OCA\Circles\Exceptions\MemberDoesNotExistException;
34
use OCA\Circles\Model\Member;
35
36
class MembersRequest extends MembersRequestBuilder {
37
38
39
	public function getMember($circleId, $userId) {
0 ignored issues
show
Unused Code introduced by
The parameter $circleId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $userId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
41
	}
42
43
44
45
	/**
46
	 * forceGetGroup();
47
	 *
48
	 * returns group information as a member within a Circle.
49
	 *
50
	 * WARNING: This function does not filters data regarding the current user/viewer.
51
	 *          In case of interaction with users, Please use getGroup() instead.
52
	 *
53
	 * @param int $circleId
54
	 * @param string $groupId
55
	 *
56
	 * @return Member
0 ignored issues
show
Documentation introduced by
Should the return type not be Member|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...
57
	 * @throws MemberDoesNotExistException
58
	 */
59 View Code Duplication
	public function forceGetGroup($circleId, $groupId) {
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...
60
		$qb = $this->getGroupsSelectSql();
61
62
		$this->limitToGroupId($qb, $groupId);
63
		$this->limitToCircleId($qb, $circleId);
64
65
		$cursor = $qb->execute();
66
		$data = $cursor->fetch();
67
		if ($data === false) {
68
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
69
		}
70
71
		$group = Member::fromArray($this->l10n, $data);
72
		$cursor->closeCursor();
73
74
		return $group;
75
	}
76
77
78
	// TODO - returns data of a group from a Viewer POV
79
	public function getGroup($circleId, $groupId, $viewer) {
0 ignored issues
show
Unused Code introduced by
The parameter $circleId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $groupId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $viewer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
81
	}
82
83
	/**
84
	 * @param int $circleId
85
	 * @param Member $viewer
86
	 *
87
	 * @return Member[]
88
	 * @throws MemberDoesNotExistException
89
	 */
90
	public function getGroups($circleId, Member $viewer) {
91
92
		if ($viewer->getLevel() < Member::LEVEL_MEMBER) {
93
			return [];
94
		}
95
96
		$qb = $this->getGroupsSelectSql();
97
		$this->limitToCircleId($qb, $circleId);
98
		$this->limitToLevel($qb, Member::LEVEL_MEMBER);
99
100
		$cursor = $qb->execute();
101
		$groups = [];
102 View Code Duplication
		while ($data = $cursor->fetch()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
103
			if ($viewer->getLevel() < Member::LEVEL_MODERATOR) {
104
				$data['note'] = '';
105
			}
106
107
			$groups[] = Member::fromArray($this->l10n, $data);
108
		}
109
		$cursor->closeCursor();
110
111
		return $groups;
112
	}
113
114
115
	/**
116
	 * Insert Member into database.
117
	 *
118
	 * @param Member $member
119
	 *
120
	 * @throws MemberAlreadyExistsException
121
	 */
122
	public function insertGroup(Member $member) {
123
		try {
124
			$qb = $this->getGroupsInsertSql();
125
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
126
			   ->setValue('group_id', $qb->createNamedParameter($member->getGroupId()))
127
			   ->setValue('level', $qb->createNamedParameter($member->getLevel()))
128
			   ->setValue('note', $qb->createNamedParameter($member->getNote()));
129
130
			$qb->execute();
131
		} catch (UniqueConstraintViolationException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Exception\...raintViolationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
132
			throw new MemberAlreadyExistsException(
133
				$this->l10n->t('This user is already a member of the circle')
134
			);
135
		}
136
	}
137
138
139
	/**
140
	 * update database entry for a specific Group.
141
	 *
142
	 * @param Member $member
143
	 *
144
	 * @return bool
145
	 */
146
	public function editGroup(Member $member) {
147
148
		$qb = $this->getGroupsUpdateSql($member->getCircleId(), $member->getGroupId());
149
		$qb->set('level', $qb->createNamedParameter($member->getLevel()));
150
		$qb->execute();
151
152
		return true;
153
	}
154
155
156
	public function unlinkAllFromGroupId($groupId) {
157
		$qb = $this->getGroupsDeleteSql($groupId);
158
		$qb->execute();
159
	}
160
161
}