Completed
Pull Request — master (#362)
by Maxence
01:50
created

MemberLevel::verifyMemberLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\GlobalScale;
31
32
33
use Exception;
34
use OCA\Circles\Exceptions\CircleDoesNotExistException;
35
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
36
use OCA\Circles\Exceptions\GlobalScaleDSyncException;
37
use OCA\Circles\Exceptions\GlobalScaleEventException;
38
use OCA\Circles\Exceptions\MemberDoesNotExistException;
39
use OCA\Circles\Exceptions\MemberIsNotModeratorException;
40
use OCA\Circles\Exceptions\MemberIsNotOwnerException;
41
use OCA\Circles\Exceptions\MemberIsOwnerException;
42
use OCA\Circles\Exceptions\MemberTypeCantEditLevelException;
43
use OCA\Circles\Exceptions\ModeratorIsNotHighEnoughException;
44
use OCA\Circles\Model\Circle;
45
use OCA\Circles\Model\GlobalScale\GSEvent;
46
use OCA\Circles\Model\Member;
47
48
49
/**
50
 * Class MemberLevel
51
 *
52
 * @package OCA\Circles\GlobalScale
53
 */
54
class MemberLevel extends AGlobalScaleEvent {
55
56
57
	/**
58
	 * @param GSEvent $event
59
	 * @param bool $localCheck
60
	 *
61
	 * @param bool $mustBeChecked
62
	 *
63
	 * @throws CircleDoesNotExistException
64
	 * @throws ConfigNoCircleAvailableException
65
	 * @throws GlobalScaleDSyncException
66
	 * @throws GlobalScaleEventException
67
	 * @throws MemberTypeCantEditLevelException
68
	 * @throws Exception
69
	 */
70
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = true): void {
71
		parent::verify($event, $localCheck, true);
72
73
		$member = $event->getMember();
74
		$level = $event->getData()
75
					   ->gInt('level');
76
		if ($member->getLevel() === $level) {
77
			throw new GlobalScaleDSyncException('level is not changed during the process');
78
		}
79
80
		$member->levelHasToBeEditable();
81
		$circle = $event->getCircle();
82
83
		if ($level === Member::LEVEL_OWNER) {
84
			$this->verifySwitchOwner($event, $circle, $member);
85
		} else {
86
			$this->verifyMemberLevel($event, $circle, $member, $level);
87
		}
88
89
	}
90
91
92
	/**
93
	 * @param GSEvent $event
94
	 *
95
	 * @throws Exception
96
	 */
97
	public function manage(GSEvent $event): void {
98
		$level = $event->getData()
99
					   ->gInt('level');
100
101
		$member = $event->getMember();
102
		$this->cleanMember($member);
103
104
		$member->setLevel($level);
105
		$this->membersRequest->updateMember($member);
106
107
		if ($level === Member::LEVEL_OWNER) {
108
			$circle = $event->getCircle();
109
			$isMod = $circle->getOwner();
110
			if ($isMod->getInstance() === '') {
111
				$isMod->setInstance($event->getSource());
112
			}
113
114
			$isMod->setLevel(Member::LEVEL_ADMIN);
115
			$this->membersRequest->updateMember($isMod);
116
		}
117
	}
118
119
120
	/**
121
	 * @param GSEvent[] $events
122
	 */
123
	public function result(array $events): void {
124
	}
125
126
127
	/**
128
	 * @param GSEvent $event
129
	 * @param Circle $circle
130
	 * @param Member $member
131
	 * @param int $level
132
	 *
133
	 * @throws MemberDoesNotExistException
134
	 * @throws MemberIsOwnerException
135
	 * @throws MemberIsNotModeratorException
136
	 * @throws ModeratorIsNotHighEnoughException
137
	 */
138
	private function verifyMemberLevel(GSEvent $event, Circle $circle, Member &$member, int $level) {
139
		$member->hasToBeMember();
140
		$member->cantBeOwner();
141
142
		if (!$event->isForced()) {
143
			$isMod = $circle->getHigherViewer();
144
			$isMod->hasToBeModerator();
145
			$isMod->hasToBeHigherLevel($level);
146
			$isMod->hasToBeHigherLevel($member->getLevel());
147
		}
148
	}
149
150
	/**
151
	 * @param GSEvent $event
152
	 * @param Circle $circle
153
	 * @param Member $member
154
	 *
155
	 * @throws MemberDoesNotExistException
156
	 * @throws MemberIsNotOwnerException
157
	 * @throws MemberIsOwnerException
158
	 */
159
	private function verifySwitchOwner(GSEvent $event, Circle $circle, Member &$member) {
160
		if (!$event->isForced()) {
161
			$isMod = $circle->getHigherViewer();
162
			$this->circlesService->hasToBeOwner($isMod);
163
		}
164
165
		$member->hasToBeMember();
166
		$member->cantBeOwner();
167
	}
168
169
}
170
171