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

GlobalSync   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 126
Duplicated Lines 7.14 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 10
dl 9
loc 126
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 2 1
A manage() 0 12 2
A result() 0 2 1
C syncCircle() 0 47 10
A getMember() 0 13 4
A removeDeprecateMembers() 9 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 GSEvent[] $events
78
	 */
79
	public function result(array $events): void {
80
	}
81
82
83
	/**
84
	 * @param Circle $circle
85
	 * @param string $source
86
	 */
87
	private function syncCircle(Circle $circle, string $source): void {
88
		try {
89
			$knownCircle = $this->circlesRequest->forceGetCircle($circle->getUniqueId());
90
91
			if (!$this->compareCircles($knownCircle, $circle)) {
92
				try {
93
					$this->circlesRequest->updateCircle($circle);
94
				} catch (CircleAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
95
				}
96
			}
97
		} catch (CircleDoesNotExistException $e) {
98
			try {
99
				$this->circlesRequest->createCircle($circle);
100
			} catch (CircleAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
101
			}
102
		}
103
104
		foreach ($circle->getMembers() as $member) {
105
			if ($member->getInstance() === '') {
106
				$member->setInstance($source);
107
			}
108
109
			try {
110
				$knownMember = $this->membersRequest->forceGetMember(
111
					$circle->getUniqueId(), $member->getUserId(), $member->getType(), $member->getInstance()
112
				);
113
114
				if ($this->compareMembers($knownMember, $member)) {
115
					continue;
116
				}
117
118
				$this->miscService->log(
119
					'updating member :' . json_encode($member) . ' from ' . json_encode($knownMember), 2
120
				);
121
				$this->membersRequest->updateMember($member);
122
			} catch (MemberDoesNotExistException $e) {
123
				try {
124
					$this->miscService->log(
125
						'creating member :' . json_encode($member), 2
126
					);
127
					$this->membersRequest->createMember($member);
128
				} catch (MemberAlreadyExistsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
129
				}
130
			}
131
		}
132
133
	}
134
135
136
	private function removeDeprecateMembers(Circle $circle, string $source): void {
137
		$knownMembers = $this->membersRequest->forceGetMembers($circle->getUniqueId());
138
139 View Code Duplication
		foreach ($knownMembers as $knownItem) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
			try {
141
				$this->getMember($knownItem, $circle->getMembers(), $source);
142
			} catch (MemberDoesNotExistException $e) {
143
				$this->miscService->log('removing deprecated member :' . json_encode($knownItem), 2);
144
				$this->membersRequest->removeMember($knownItem);
145
				$this->gsSharesRequest->removeGSSharesFromMember($knownItem);
146
			}
147
		}
148
	}
149
150
151
	/**
152
	 * @param Member $item
153
	 * @param Member[] $members
154
	 * @param string $source
155
	 *
156
	 * @throws MemberDoesNotExistException
157
	 */
158
	private function getMember(Member $item, array $members, string $source) {
159
		foreach ($members as $member) {
160
			if ($member->getInstance() === '') {
161
				$member->setInstance($source);
162
			}
163
164
			if ($this->compareMembers($member, $item)) {
165
				return;
166
			}
167
		}
168
169
		throw new MemberDoesNotExistException();
170
	}
171
172
}
173
174