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

GlobalSync::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 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
		foreach ($knownMembers as $knownItem) {
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
			}
146
		}
147
	}
148
149
150
	/**
151
	 * @param Member $item
152
	 * @param Member[] $members
153
	 * @param string $source
154
	 *
155
	 * @throws MemberDoesNotExistException
156
	 */
157
	private function getMember(Member $item, array $members, string $source) {
158
		foreach ($members as $member) {
159
			if ($member->getInstance() === '') {
160
				$member->setInstance($source);
161
			}
162
163
			if ($this->compareMembers($member, $item)) {
164
				return;
165
			}
166
		}
167
168
		throw new MemberDoesNotExistException();
169
	}
170
171
}
172
173