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

AGlobalScaleEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
	public function __construct(
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 $event
149
	 * @param bool $mustBeChecked
150
	 *
151
	 * @throws CircleDoesNotExistException
152
	 * @throws ConfigNoCircleAvailableException
153
	 * @throws GlobalScaleDSyncException
154
	 * @throws GlobalScaleEventException
155
	 */
156
	private function checkViewer(GSEvent $event, bool $mustBeChecked) {
157
		if (!$event->hasCircle()
158
			|| !$event->getCircle()
159
					  ->hasViewer()) {
160
			if ($mustBeChecked) {
161
				throw new GlobalScaleEventException('GSEvent cannot be checked');
162
			} else {
163
				return;
164
			}
165
		}
166
167
		$circle = $event->getCircle();
168
		$viewer = $circle->getViewer();
169
		$this->cleanMember($viewer);
170
171
		$localCircle = $this->circlesRequest->getCircle(
172
			$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance()
173
		);
174
175
		if (!$this->compareMembers($viewer, $localCircle->getViewer())) {
176
			throw new GlobalScaleDSyncException('Viewer seems DSync');
177
		}
178
179
		$event->setCircle($localCircle);
180
	}
181
182
183
	/**
184
	 * @param Member $member1
185
	 * @param Member $member2
186
	 *
187
	 * @return bool
188
	 */
189
	protected function compareMembers(Member $member1, Member $member2) {
190
		if ($member1->getInstance() === '') {
191
			$member1->setInstance($this->configService->getLocalCloudId());
192
		}
193
194
		if ($member2->getInstance() === '') {
195
			$member2->setInstance($this->configService->getLocalCloudId());
196
		}
197
198
199
		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...
200
			|| $member1->getUserId() !== $member2->getUserId()
201
			|| $member1->getType() <> $member2->getType()
202
			|| $member1->getLevel() <> $member2->getLevel()
203
			|| $member1->getStatus() !== $member2->getStatus()
204
			|| $member1->getInstance() !== $member2->getInstance()) {
205
			return false;
206
		}
207
208
		return true;
209
	}
210
211
212
	/**
213
	 * @param Circle $circle1
214
	 * @param Circle $circle2
215
	 *
216
	 * @return bool
217
	 */
218
	protected function compareCircles(Circle $circle1, Circle $circle2): bool {
219
		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...
220
			|| $circle1->getDescription() !== $circle2->getDescription()
221
			|| $circle1->getSettings(true) !== $circle2->getSettings(true)
222
			|| $circle1->getType() !== $circle2->getType()
223
			|| $circle1->getUniqueId() !== $circle2->getUniqueId()) {
224
			return false;
225
		}
226
227
		return true;
228
	}
229
230
231
	protected function cleanMember(Member $member) {
232
		if ($member->getInstance() === $this->configService->getLocalCloudId()) {
233
			$member->setInstance('');
234
		}
235
	}
236
237
}
238
239