Completed
Push — master ( 397c2e...5232e6 )
by
unknown
02:26
created

MembersMapper::editMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
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
namespace OCA\Circles\Db;
28
29
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
30
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
31
use OCA\Circles\Exceptions\MemberDoesNotExistException;
32
use OCA\Circles\Model\Circle;
33
use OCA\Circles\Model\Member;
34
35
use OCP\IDBConnection;
36
use OCP\AppFramework\Db\Mapper;
37
38
class MembersMapper extends Mapper {
39
40
	const TABLENAME = 'circles_members';
41
42
	private $miscService;
43
44
	public function __construct(IDBConnection $db, $miscService) {
45
		parent::__construct($db, self::TABLENAME, 'OCA\Circles\Db\Members');
46
		$this->miscService = $miscService;
47
	}
48
49
50
	/**
51
	 * @param $circleId
52
	 * @param $userId
53
	 * @param bool $moderator
54
	 *
55
	 * @return null|Member
56
	 * @throws MemberDoesNotExistException
57
	 */
58
	public function getMemberFromCircle($circleId, $userId, $moderator = false) {
59
60
		$circleId = (int)$circleId;
61
62
		$qb = $this->db->getQueryBuilder();
63
		$qb->select(
64
			'circle_id', 'user_id', 'level', 'status', 'note', 'joined'
65
		)
66
		   ->from(self::TABLENAME)
67
		   ->where(
68
			   $qb->expr()
69
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
70
		   );
71
72
		$qb->andWhere(
73
			$qb->expr()
74
			   ->eq('user_id', $qb->createNamedParameter($userId))
75
		);
76
77
		$cursor = $qb->setMaxResults(1)
78
					 ->execute();
79
80
		$data = $cursor->fetch();
81
82
		if ($moderator !== true) {
83
			unset($data['note']);
84
		}
85
86
		$member = Member::fromArray($data);
87
		$cursor->closeCursor();
88
89
		if ($member === null) {
90
			throw new MemberDoesNotExistException();
91
		}
92
93
		return $member;
94
	}
95
96
97
	/**
98
	 * get members list from a circle. If moderator, returns also notes about each member.
99
	 *
100
	 * @param $circleId
101
	 * @param bool $moderator
102
	 *
103
	 * @return array
104
	 */
105
	public function getMembersFromCircle($circleId, $moderator = false) {
106
107
		$circleId = (int)$circleId;
108
109
		$qb = $this->db->getQueryBuilder();
110
		$qb->select(
111
			'circle_id', 'user_id', 'level', 'status', 'note', 'joined'
112
		)
113
		   ->from(self::TABLENAME)
114
		   ->where(
115
			   $qb->expr()
116
				  ->eq('circle_id', $qb->createNamedParameter($circleId))
117
		   )
118
		   ->andwhere(
119
			   $qb->expr()
120
				  ->neq('status', $qb->createNamedParameter(Member::STATUS_NONMEMBER))
121
		   );
122
123
		$cursor = $qb->execute();
124
125
		$result = [];
126
		while ($data = $cursor->fetch()) {
127
			if ($moderator !== true) {
128
				unset($data['note']);
129
			}
130
131
			$result[] = Member::fromArray($data);
132
		}
133
		$cursor->closeCursor();
134
135
		return $result;
136
	}
137
138
139
	public function editMember(Member $member) {
140
141
		$qb = $this->db->getQueryBuilder();
142
		$qb->update(self::TABLENAME);
143
		$qb->set('level', $qb->createNamedParameter($member->getLevel()));
144
		$qb->set('status', $qb->createNamedParameter($member->getStatus()));
145
		$qb->where(
146
			$qb->expr()
147
			   ->andX(
148
				   $qb->expr()
149
					  ->eq('circle_id', $qb->createNamedParameter($member->getCircleId())),
150
				   $qb->expr()
151
					  ->eq('user_id', $qb->createNamedParameter($member->getUserId()))
152
			   )
153
		);
154
155
		$qb->execute();
156
157
		return true;
158
	}
159
160
161
	public function add(Member $member) {
162
163
		try {
164
			$qb = $this->db->getQueryBuilder();
165
			$qb->insert(self::TABLENAME)
166
			   ->setValue('circle_id', $qb->createNamedParameter($member->getCircleId()))
167
			   ->setValue('user_id', $qb->createNamedParameter($member->getUserId()))
168
			   ->setValue('level', $qb->createNamedParameter($member->getLevel()))
169
			   ->setValue('status', $qb->createNamedParameter($member->getStatus()))
170
			   ->setValue('note', $qb->createNamedParameter($member->getNote()))
171
			   ->setValue('joined', 'CURRENT_TIMESTAMP()');
172
			$qb->execute();
173
		} catch (UniqueConstraintViolationException $e) {
1 ignored issue
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...
174
			throw new MemberAlreadyExistsException();
175
		}
176
177
		return true;
178
	}
179
180
181
	/**
182
	 * Remove a member from a circle.
183
	 *
184
	 * @param Member $member
185
	 *
186
	 * @return bool
187
	 */
188
	public function remove(Member $member) {
189
190
		$qb = $this->db->getQueryBuilder();
191
		$qb->delete(self::TABLENAME)
192
		   ->where(
193
			   $qb->expr()
194
				  ->eq('circle_id', $qb->createNamedParameter($member->getCircleId())),
195
			   $qb->expr()
196
				  ->eq('user_id', $qb->createNamedParameter($member->getUserId()))
197
		   );
198
199
		$qb->execute();
200
201
		return true;
202
	}
203
204
	/**
205
	 * remove all members/owner from a circle
206
	 *
207
	 * @param Circle $circle
208
	 *
209
	 * @return bool
210
	 */
211 View Code Duplication
	public function removeAllFromCircle(Circle $circle) {
1 ignored issue
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...
212
		$qb = $this->db->getQueryBuilder();
213
		$qb->delete(self::TABLENAME)
214
		   ->where(
215
			   $qb->expr()
216
				  ->eq('circle_id', $qb->createNamedParameter($circle->getId()))
217
		   );
218
219
		$qb->execute();
220
221
		return true;
222
223
	}
224
}
225
226