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

UserDeleted::manage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 OCA\Circles\Exceptions\CircleDoesNotExistException;
34
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
35
use OCA\Circles\Exceptions\GlobalScaleDSyncException;
36
use OCA\Circles\Exceptions\GlobalScaleEventException;
37
use OCA\Circles\Exceptions\MemberDoesNotExistException;
38
use OCA\Circles\Model\Circle;
39
use OCA\Circles\Model\GlobalScale\GSEvent;
40
use OCA\Circles\Model\Member;
41
42
43
/**
44
 * Class MemberDelete
45
 *
46
 * @package OCA\Circles\GlobalScale
47
 */
48
class UserDeleted extends AGlobalScaleEvent {
49
50
51
	/**
52
	 * @param GSEvent $event
53
	 * @param bool $localCheck
54
	 * @param bool $mustBeChecked
55
	 *
56
	 * @throws CircleDoesNotExistException
57
	 * @throws ConfigNoCircleAvailableException
58
	 * @throws GlobalScaleDSyncException
59
	 * @throws GlobalScaleEventException
60
	 */
61
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = false): void {
62
		parent::verify($event, $localCheck, true);
63
64
		$member = $event->getMember();
65
		$circles = $this->circlesRequest->getCircles($member->getUserId(), 0, '', Member::LEVEL_OWNER);
66
67
		$destroyedCircles = [];
68
		$promotedAdmins = [];
69
		foreach ($circles as $circle) {
70
			$members =
71
				$this->membersRequest->forceGetMembers($circle->getUniqueId(), Member::LEVEL_MEMBER);
72
73
			if ($circle->getType() === Circle::CIRCLES_PERSONAL || sizeof($members) === 1) {
74
				$destroyedCircles[] = $circle->getUniqueId();
75
				continue;
76
			}
77
78
			$promotedAdmins[] = $this->getOlderAdmin($members);
79
		}
80
81
		$event->getData()
82
			  ->sArray('destroyedCircles', $destroyedCircles)
83
			  ->sArray('promotedAdmins', $promotedAdmins);
84
85
		$this->miscService->log(json_encode($event->getData()));
86
	}
87
88
89
	/**
90
	 * @param GSEvent[] $events
91
	 */
92
	public function result(array $events): void {
93
	}
94
95
96
	/**
97
	 * @param GSEvent $event
98
	 */
99
	public function manage(GSEvent $event): void {
100
		$member = $event->getMember();
101
102
		$this->membersRequest->removeAllMembershipsFromUser($member);
103
104
		$data = $event->getData();
105
		$this->destroyCircles($data->gArray('destroyedCircles'));
106
		$this->promotedAdmins($data->gArray('promotedAdmins'));
107
	}
108
109
110
	/**
111
	 * @param Member[] $members
112
	 *
113
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|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...
114
	 */
115
	private function getOlderAdmin(array $members) {
116
		foreach ($members as $member) {
117
			if ($member->getLevel() === Member::LEVEL_ADMIN) {
118
				return $member->getMemberId();
119
			}
120
		}
121
		foreach ($members as $member) {
122
			if ($member->getLevel() === Member::LEVEL_MODERATOR) {
123
				return $member->getMemberId();
124
			}
125
		}
126
		foreach ($members as $member) {
127
			if ($member->getLevel() === Member::LEVEL_MEMBER) {
128
				return $member->getMemberId();
129
			}
130
		}
131
	}
132
133
134
	/**
135
	 * @param array $circleIds
136
	 */
137
	private function destroyCircles(array $circleIds) {
138
		foreach ($circleIds as $circleId) {
139
			$this->circlesRequest->destroyCircle($circleId);
140
			$this->membersRequest->removeAllFromCircle($circleId);
141
		}
142
	}
143
144
145
	/**
146
	 * @param array $memberIds
147
	 */
148
	private function promotedAdmins(array $memberIds) {
149
		foreach ($memberIds as $memberId) {
150
			try {
151
				$member = $this->membersRequest->forceGetMemberById($memberId);
152
				$member->setLevel(Member::LEVEL_OWNER);
153
				$this->membersRequest->updateMember($member);
154
			} catch (MemberDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
155
			}
156
		}
157
	}
158
159
}
160
161