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

MemberLevel::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 3
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
		$this->miscService->log('$$$$ 3 ' . json_encode($event));
90
91
	}
92
93
94
	/**
95
	 * @param GSEvent $event
96
	 *
97
	 * @throws Exception
98
	 */
99
	public function manage(GSEvent $event): void {
100
		$level = $event->getData()
101
					   ->gInt('level');
102
103
		$this->miscService->log('$$$$ 4 ' . json_encode($event));
104
105
		$member = $event->getMember();
106
		$this->cleanMember($member);
107
		$this->miscService->log('$$$$ 5 ' . json_encode($event));
108
109
		$member->setLevel($level);
110
		$this->membersRequest->updateMember($member);
111
112
		$this->miscService->log('#### ' . json_encode($event));
113
		if ($level === Member::LEVEL_OWNER) {
114
			$circle = $event->getCircle();
115
			$isMod = $circle->getOwner();
116
			if ($isMod->getInstance() === '') {
117
				$isMod->setInstance($event->getSource());
118
			}
119
120
			$this->miscService->log('???? ' . $isMod->getInstance());
121
122
			$isMod->setLevel(Member::LEVEL_ADMIN);
123
			$this->miscService->log('#### 001 ' . json_encode($event));
124
125
			$this->membersRequest->updateMember($isMod);
126
			$this->miscService->log('#### 002 ' . json_encode($event));
127
128
		}
129
	}
130
131
132
	/**
133
	 * @param GSEvent[] $events
134
	 */
135
	public function result(array $events): void {
136
	}
137
138
139
	/**
140
	 * @param GSEvent $event
141
	 * @param Circle $circle
142
	 * @param Member $member
143
	 * @param int $level
144
	 *
145
	 * @throws MemberDoesNotExistException
146
	 * @throws MemberIsOwnerException
147
	 * @throws MemberIsNotModeratorException
148
	 * @throws ModeratorIsNotHighEnoughException
149
	 */
150
	private function verifyMemberLevel(GSEvent $event, Circle $circle, Member &$member, int $level) {
151
		$member->hasToBeMember();
152
		$member->cantBeOwner();
153
154
		if (!$event->isForced()) {
155
			$isMod = $circle->getHigherViewer();
156
			$isMod->hasToBeModerator();
157
			$isMod->hasToBeHigherLevel($level);
158
			$isMod->hasToBeHigherLevel($member->getLevel());
159
		}
160
	}
161
162
	/**
163
	 * @param GSEvent $event
164
	 * @param Circle $circle
165
	 * @param Member $member
166
	 *
167
	 * @throws MemberDoesNotExistException
168
	 * @throws MemberIsNotOwnerException
169
	 * @throws MemberIsOwnerException
170
	 */
171
	private function verifySwitchOwner(GSEvent $event, Circle $circle, Member &$member) {
172
		if (!$event->isForced()) {
173
			$isMod = $circle->getHigherViewer();
174
			$this->circlesService->hasToBeOwner($isMod);
175
		}
176
177
		$member->hasToBeMember();
178
		$member->cantBeOwner();
179
	}
180
181
}
182
183