Completed
Push — master ( 94366e...dc9b8a )
by Maxence
04:37 queued 01:50
created

MembersRequest::getGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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 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
40
	/**
41
	 * forceGetGroup();
42
	 *
43
	 * returns group information as a member within a Circle.
44
	 *
45
	 * WARNING: This function does not filters data regarding the current user/viewer.
46
	 *          In case of interaction with users, Please use getGroup() instead.
47
	 *
48
	 * @param int $circleId
49
	 * @param string $groupId
50
	 *
51
	 * @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...
52
	 * @throws MemberDoesNotExistException
53
	 */
54 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...
55
		$qb = $this->getGroupsSelectSql();
56
57
		$this->limitToGroupId($qb, $groupId);
58
		$this->limitToCircleId($qb, $circleId);
59
60
		$cursor = $qb->execute();
61
		$data = $cursor->fetch();
62
		if ($data === false) {
63
			throw new MemberDoesNotExistException($this->l10n->t('This member does not exist'));
64
		}
65
66
		$group = Member::fromArray($this->l10n, $data);
67
		$cursor->closeCursor();
68
69
		return $group;
70
	}
71
72
73
74
	// TODO - returns data of a group from a Viewer POV
75
	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...
76
	{
77
78
	}
79
80
	/**
81
	 * @param int $circleId
82
	 * @param Member $viewer
83
	 *
84
	 * @return Member[]
85
	 * @throws MemberDoesNotExistException
86
	 */
87
	public function getGroups($circleId, Member $viewer) {
88
89
		if ($viewer->getLevel() < Member::LEVEL_MEMBER) {
90
			return [];
91
		}
92
93
		$qb = $this->getGroupsSelectSql();
94
		$this->limitToCircleId($qb, $circleId);
95
		$this->limitToMemberLevel($qb, Member::LEVEL_MEMBER);
96
97
		$cursor = $qb->execute();
98
		$groups = [];
99 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...
100
			if ($viewer->getLevel() < Member::LEVEL_MODERATOR) {
101
				$data['note'] = '';
102
			}
103
104
			$groups[] = Member::fromArray($this->l10n, $data);
105
		}
106
		$cursor->closeCursor();
107
108
		return $groups;
109
	}
110
111
112
	/**
113
	 * Insert Member into database.
114
	 *
115
	 * @param Member $member
116
	 *
117
	 * @throws MemberAlreadyExistsException
118
	 */
119
	public function insertGroup(Member $member) {
120
		try {
121
			$qb = $this->getGroupsInsertSql();
122
			$qb->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
123
			   ->setValue('group_id', $qb->createNamedParameter($member->getGroupId()))
124
			   ->setValue('level', $qb->createNamedParameter($member->getLevel()))
125
			   ->setValue('note', $qb->createNamedParameter($member->getNote()));
126
127
			$qb->execute();
128
		} 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...
129
			throw new MemberAlreadyExistsException(
130
				$this->l10n->t('This user is already a member of the circle')
131
			);
132
		}
133
	}
134
135
136
	/**
137
	 * update database entry for a specific Group.
138
	 *
139
	 * @param Member $member
140
	 *
141
	 * @return bool
142
	 */
143
	public function editGroup(Member $member) {
144
145
		$qb = $this->getGroupsUpdateSql($member->getCircleId(), $member->getGroupId());
146
		$qb->set('level', $qb->createNamedParameter($member->getLevel()));
147
		$qb->execute();
148
149
		return true;
150
	}
151
152
153
	public function unlinkAllFromGroupId($groupId)
154
	{
155
		$qb = $this->getGroupsDeleteSql($groupId);
156
		$qb->execute();
157
	}
158
159
}