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

GlobalSync::syncCircle()   C

Complexity

Conditions 10
Paths 261

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 6.013
c 0
b 0
f 0
cc 10
nc 261
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\CircleAlreadyExistsException;
34
use OCA\Circles\Exceptions\CircleDoesNotExistException;
35
use OCA\Circles\Exceptions\MemberAlreadyExistsException;
36
use OCA\Circles\Exceptions\MemberDoesNotExistException;
37
use OCA\Circles\Model\Circle;
38
use OCA\Circles\Model\GlobalScale\GSEvent;
39
use OCA\Circles\Model\Member;
40
41
42
/**
43
 * Class MemberCreate
44
 *
45
 * @package OCA\Circles\GlobalScale
46
 */
47
class GlobalSync extends AGlobalScaleEvent {
48
49
50
	/**
51
	 * @param GSEvent $event
52
	 * @param bool $localCheck
53
	 * @param bool $mustBeChecked
54
	 */
55
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeChecked = false): void {
56
	}
57
58
59
	/**
60
	 * @param GSEvent $event
61
	 */
62
	public function manage(GSEvent $event): void {
63
		$data = $event->getData();
64
		$circles = [];
65
		foreach ($data->gAll() as $circle) {
66
			$circle = Circle::fromArray($circle);
67
			$circles[] = $circle;
68
69
			$this->syncCircle($circle, $event->getSource());
70
			$this->removeDeprecateMembers($circle, $event->getSource());
71
		}
72
73
	}
74
75
76
	/**
77
	 * @param Circle $circle
78
	 * @param string $source
79
	 */
80
	private function syncCircle(Circle $circle, string $source): void {
81
		try {
82
			$knownCircle = $this->circlesRequest->forceGetCircle($circle->getUniqueId());
83
84
			if (!$this->compareCircles($knownCircle, $circle)) {
85
				try {
86
					$this->circlesRequest->updateCircle($circle);
87
				} catch (CircleAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
88
				}
89
			}
90
		} catch (CircleDoesNotExistException $e) {
91
			try {
92
				$this->circlesRequest->createCircle($circle);
93
			} catch (CircleAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94
			}
95
		}
96
97
		foreach ($circle->getMembers() as $member) {
98
			if ($member->getInstance() === '') {
99
				$member->setInstance($source);
100
			}
101
102
			try {
103
				$knownMember = $this->membersRequest->forceGetMember(
104
					$circle->getUniqueId(), $member->getUserId(), $member->getType(), $member->getInstance()
105
				);
106
107
				if ($this->compareMembers($knownMember, $member)) {
108
					continue;
109
				}
110
111
				$this->miscService->log(
112
					'updating member :' . json_encode($member) . ' from ' . json_encode($knownMember), 2
113
				);
114
				$this->membersRequest->updateMember($member);
115
			} catch (MemberDoesNotExistException $e) {
116
				try {
117
					$this->miscService->log(
118
						'creating member :' . json_encode($member), 2
119
					);
120
					$this->membersRequest->createMember($member);
121
				} catch (MemberAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
122
				}
123
			}
124
		}
125
126
	}
127
128
129
	private function removeDeprecateMembers(Circle $circle, string $source): void {
130
		$knownMembers = $this->membersRequest->forceGetMembers($circle->getUniqueId());
131
132
		foreach ($knownMembers as $knownItem) {
133
			try {
134
				$this->getMember($knownItem, $circle->getMembers(), $source);
135
			} catch (MemberDoesNotExistException $e) {
136
				$this->miscService->log('removing deprecated member :' . json_encode($knownItem), 2);
137
				$this->membersRequest->removeMember($knownItem);
138
			}
139
		}
140
	}
141
142
143
	/**
144
	 * @param Member $item
145
	 * @param Member[] $members
146
	 * @param string $source
147
	 *
148
	 * @throws MemberDoesNotExistException
149
	 */
150
	private function getMember(Member $item, array $members, string $source) {
151
		foreach ($members as $member) {
152
			if ($member->getInstance() === '') {
153
				$member->setInstance($source);
154
			}
155
156
			if ($this->compareMembers($member, $item)) {
157
				return;
158
			}
159
		}
160
161
		throw new MemberDoesNotExistException();
162
	}
163
164
}
165
166