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

AGlobalScaleEvent::manage()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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\Db\CirclesRequest;
34
use OCA\Circles\Db\MembersRequest;
35
use OCA\Circles\Db\SharesRequest;
36
use OCA\Circles\Db\TokensRequest;
37
use OCA\Circles\Exceptions\CircleDoesNotExistException;
38
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
39
use OCA\Circles\Exceptions\GlobalScaleDSyncException;
40
use OCA\Circles\Exceptions\GlobalScaleEventException;
41
use OCA\Circles\Model\Circle;
42
use OCA\Circles\Model\GlobalScale\GSEvent;
43
use OCA\Circles\Model\Member;
44
use OCA\Circles\Service\CirclesService;
45
use OCA\Circles\Service\ConfigService;
46
use OCA\Circles\Service\EventsService;
47
use OCA\Circles\Service\MembersService;
48
use OCA\Circles\Service\MiscService;
49
50
51
/**
52
 * Class AGlobalScaleEvent
53
 *
54
 * @package OCA\Circles\GlobalScale
55
 */
56
abstract class AGlobalScaleEvent {
57
58
59
	/** @var SharesRequest */
60
	private $sharesRequest;
61
62
	/** @var TokensRequest */
63
	private $tokensRequest;
64
65
	/** @var CirclesRequest */
66
	protected $circlesRequest;
67
68
	/** @var MembersRequest */
69
	protected $membersRequest;
70
71
	/** @var CirclesService */
72
	protected $circlesService;
73
74
	/** @var MembersService */
75
	protected $membersService;
76
77
	/** @var EventsService */
78
	protected $eventsService;
79
80
	/** @var ConfigService */
81
	protected $configService;
82
83
	/** @var MiscService */
84
	protected $miscService;
85
86
87
	/**
88
	 * AGlobalScaleEvent constructor.
89
	 *
90
	 * @param SharesRequest $sharesRequest
91
	 * @param TokensRequest $tokensRequest
92
	 * @param CirclesRequest $circlesRequest
93
	 * @param MembersRequest $membersRequest
94
	 * @param CirclesService $circlesService
95
	 * @param MembersService $membersService
96
	 * @param EventsService $eventsService
97
	 * @param ConfigService $configService
98
	 * @param MiscService $miscService
99
	 */
100 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
101
		SharesRequest $sharesRequest,
102
		TokensRequest $tokensRequest,
103
		CirclesRequest $circlesRequest,
104
		MembersRequest $membersRequest,
105
		CirclesService $circlesService,
106
		MembersService $membersService,
107
		EventsService $eventsService,
108
		ConfigService $configService,
109
		MiscService $miscService
110
	) {
111
		$this->sharesRequest = $sharesRequest;
112
		$this->tokensRequest = $tokensRequest;
113
		$this->circlesRequest = $circlesRequest;
114
		$this->membersRequest = $membersRequest;
115
		$this->circlesService = $circlesService;
116
		$this->membersService = $membersService;
117
		$this->eventsService = $eventsService;
118
		$this->configService = $configService;
119
		$this->miscService = $miscService;
120
	}
121
122
123
	/**
124
	 * @param GSEvent $event
125
	 * @param bool $localCheck
126
	 *
127
	 * @param bool $mustBeCheck
128
	 *
129
	 * @throws CircleDoesNotExistException
130
	 * @throws ConfigNoCircleAvailableException
131
	 * @throws GlobalScaleDSyncException
132
	 * @throws GlobalScaleEventException
133
	 */
134
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void {
135
		if ($localCheck) {
136
			$this->checkViewer($event, $mustBeCheck);
137
		}
138
	}
139
140
141
	/**
142
	 * @param GSEvent $event
143
	 */
144
	abstract public function manage(GSEvent $event): void;
145
146
147
	/**
148
	 * @param GSEvent[] $events
149
	 */
150
	abstract public function result(array $events): void;
151
152
153
	/**
154
	 * @param GSEvent $event
155
	 * @param bool $mustBeChecked
156
	 *
157
	 * @throws CircleDoesNotExistException
158
	 * @throws ConfigNoCircleAvailableException
159
	 * @throws GlobalScaleDSyncException
160
	 * @throws GlobalScaleEventException
161
	 */
162
	private function checkViewer(GSEvent $event, bool $mustBeChecked) {
163
		if (!$event->hasCircle()
164
			|| !$event->getCircle()
165
					  ->hasViewer()) {
166
			if ($mustBeChecked) {
167
				throw new GlobalScaleEventException('GSEvent cannot be checked');
168
			} else {
169
				return;
170
			}
171
		}
172
173
		$circle = $event->getCircle();
174
		$viewer = $circle->getViewer();
175
		$this->cleanMember($viewer);
176
177
		$localCircle = $this->circlesRequest->getCircle(
178
			$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance()
179
		);
180
181
		if (!$this->compareMembers($viewer, $localCircle->getViewer())) {
182
			throw new GlobalScaleDSyncException('Viewer seems DSync');
183
		}
184
185
		$event->setCircle($localCircle);
186
	}
187
188
189
	/**
190
	 * @param Member $member1
191
	 * @param Member $member2
192
	 *
193
	 * @return bool
194
	 */
195
	protected function compareMembers(Member $member1, Member $member2) {
196
		if ($member1->getInstance() === '') {
197
			$member1->setInstance($this->configService->getLocalCloudId());
198
		}
199
200
		if ($member2->getInstance() === '') {
201
			$member2->setInstance($this->configService->getLocalCloudId());
202
		}
203
204
205
		if ($member1->getCircleId() !== $member2->getCircleId()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($member1->getCi...ember2->getInstance());.
Loading history...
206
			|| $member1->getUserId() !== $member2->getUserId()
207
			|| $member1->getType() <> $member2->getType()
208
			|| $member1->getLevel() <> $member2->getLevel()
209
			|| $member1->getStatus() !== $member2->getStatus()
210
			|| $member1->getInstance() !== $member2->getInstance()) {
211
			return false;
212
		}
213
214
		return true;
215
	}
216
217
218
	/**
219
	 * @param Circle $circle1
220
	 * @param Circle $circle2
221
	 *
222
	 * @return bool
223
	 */
224
	protected function compareCircles(Circle $circle1, Circle $circle2): bool {
225
		if ($circle1->getName() !== $circle2->getName()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($circle1->getNa...ircle2->getUniqueId());.
Loading history...
226
			|| $circle1->getDescription() !== $circle2->getDescription()
227
			|| $circle1->getSettings(true) !== $circle2->getSettings(true)
228
			|| $circle1->getType() !== $circle2->getType()
229
			|| $circle1->getUniqueId() !== $circle2->getUniqueId()) {
230
			return false;
231
		}
232
233
		return true;
234
	}
235
236
237
	protected function cleanMember(Member $member) {
238
		if ($member->getInstance() === $this->configService->getLocalCloudId()) {
239
			$member->setInstance('');
240
		}
241
	}
242
243
}
244
245