Completed
Push — master ( 4f3689...17b456 )
by Maxence
02:12 queued 11s
created

UserDeleted   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 7
dl 0
loc 110
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 24 4
A result() 0 2 1
A manage() 0 9 1
B getOlderAdmin() 0 17 7
A destroyCircles() 0 6 2
A promotedAdmins() 0 10 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 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
86
87
	/**
88
	 * @param GSEvent[] $events
89
	 */
90
	public function result(array $events): void {
91
	}
92
93
94
	/**
95
	 * @param GSEvent $event
96
	 */
97
	public function manage(GSEvent $event): void {
98
		$member = $event->getMember();
99
100
		$this->membersRequest->removeAllMembershipsFromUser($member);
101
102
		$data = $event->getData();
103
		$this->destroyCircles($data->gArray('destroyedCircles'));
104
		$this->promotedAdmins($data->gArray('promotedAdmins'));
105
	}
106
107
108
	/**
109
	 * @param Member[] $members
110
	 *
111
	 * @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...
112
	 */
113
	private function getOlderAdmin(array $members) {
114
		foreach ($members as $member) {
115
			if ($member->getLevel() === Member::LEVEL_ADMIN) {
116
				return $member->getMemberId();
117
			}
118
		}
119
		foreach ($members as $member) {
120
			if ($member->getLevel() === Member::LEVEL_MODERATOR) {
121
				return $member->getMemberId();
122
			}
123
		}
124
		foreach ($members as $member) {
125
			if ($member->getLevel() === Member::LEVEL_MEMBER) {
126
				return $member->getMemberId();
127
			}
128
		}
129
	}
130
131
132
	/**
133
	 * @param array $circleIds
134
	 */
135
	private function destroyCircles(array $circleIds) {
136
		foreach ($circleIds as $circleId) {
137
			$this->circlesRequest->destroyCircle($circleId);
138
			$this->membersRequest->removeAllFromCircle($circleId);
139
		}
140
	}
141
142
143
	/**
144
	 * @param array $memberIds
145
	 */
146
	private function promotedAdmins(array $memberIds) {
147
		foreach ($memberIds as $memberId) {
148
			try {
149
				$member = $this->membersRequest->forceGetMemberById($memberId);
150
				$member->setLevel(Member::LEVEL_OWNER);
151
				$this->membersRequest->updateMember($member);
152
			} catch (MemberDoesNotExistException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
153
			}
154
		}
155
	}
156
157
}
158
159