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

AGlobalScaleEvent   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 13.46 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 21
loc 156
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 21 21 1
A verify() 0 5 2
manage() 0 1 ?
A checkViewer() 0 25 5
B compareMembers() 0 20 9
A cleanMember() 0 5 2

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\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\GlobalScale\GSEvent;
42
use OCA\Circles\Model\Member;
43
use OCA\Circles\Service\CirclesService;
44
use OCA\Circles\Service\ConfigService;
45
use OCA\Circles\Service\EventsService;
46
use OCA\Circles\Service\MembersService;
47
use OCA\Circles\Service\MiscService;
48
49
50
/**
51
 * Class AGlobalScaleEvent
52
 *
53
 * @package OCA\Circles\GlobalScale
54
 */
55
abstract class AGlobalScaleEvent {
56
57
58
	/** @var SharesRequest */
59
	private $sharesRequest;
60
61
	/** @var TokensRequest */
62
	private $tokensRequest;
63
64
	/** @var CirclesRequest */
65
	protected $circlesRequest;
66
67
	/** @var MembersRequest */
68
	protected $membersRequest;
69
70
	/** @var CirclesService */
71
	protected $circlesService;
72
73
	/** @var MembersService */
74
	protected $membersService;
75
76
	/** @var EventsService */
77
	protected $eventsService;
78
79
	/** @var ConfigService */
80
	protected $configService;
81
82
	/** @var MiscService */
83
	protected $miscService;
84
85
86
	/**
87
	 * AGlobalScaleEvent constructor.
88
	 *
89
	 * @param SharesRequest $sharesRequest
90
	 * @param TokensRequest $tokensRequest
91
	 * @param CirclesRequest $circlesRequest
92
	 * @param MembersRequest $membersRequest
93
	 * @param CirclesService $circlesService
94
	 * @param MembersService $membersService
95
	 * @param EventsService $eventsService
96
	 * @param ConfigService $configService
97
	 * @param MiscService $miscService
98
	 */
99 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...
100
		SharesRequest $sharesRequest,
101
		TokensRequest $tokensRequest,
102
		CirclesRequest $circlesRequest,
103
		MembersRequest $membersRequest,
104
		CirclesService $circlesService,
105
		MembersService $membersService,
106
		EventsService $eventsService,
107
		ConfigService $configService,
108
		MiscService $miscService
109
	) {
110
		$this->sharesRequest = $sharesRequest;
111
		$this->tokensRequest = $tokensRequest;
112
		$this->circlesRequest = $circlesRequest;
113
		$this->membersRequest = $membersRequest;
114
		$this->circlesService = $circlesService;
115
		$this->membersService = $membersService;
116
		$this->eventsService = $eventsService;
117
		$this->configService = $configService;
118
		$this->miscService = $miscService;
119
	}
120
121
122
	/**
123
	 * @param GSEvent $event
124
	 * @param bool $localCheck
125
	 *
126
	 * @param bool $mustBeCheck
127
	 *
128
	 * @throws CircleDoesNotExistException
129
	 * @throws ConfigNoCircleAvailableException
130
	 * @throws GlobalScaleDSyncException
131
	 * @throws GlobalScaleEventException
132
	 */
133
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void {
134
		if ($localCheck) {
135
			$this->checkViewer($event, $mustBeCheck);
136
		}
137
	}
138
139
140
	/**
141
	 * @param GSEvent $event
142
	 */
143
	abstract public function manage(GSEvent $event): void;
144
145
146
	/**
147
	 * @param GSEvent $event
148
	 * @param bool $mustBeChecked
149
	 *
150
	 * @throws CircleDoesNotExistException
151
	 * @throws ConfigNoCircleAvailableException
152
	 * @throws GlobalScaleDSyncException
153
	 * @throws GlobalScaleEventException
154
	 */
155
	private function checkViewer(GSEvent $event, bool $mustBeChecked) {
156
		if (!$event->hasCircle()
157
			|| !$event->getCircle()
158
					  ->hasViewer()) {
159
			if ($mustBeChecked) {
160
				throw new GlobalScaleEventException('GSEvent cannot be checked');
161
			} else {
162
				return;
163
			}
164
		}
165
166
		$circle = $event->getCircle();
167
		$viewer = $circle->getViewer();
168
		$this->cleanMember($viewer);
169
170
		$localCircle = $this->circlesRequest->getCircle(
171
			$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance()
172
		);
173
174
		if (!$this->compareMembers($viewer, $localCircle->getViewer())) {
175
			throw new GlobalScaleDSyncException('Viewer seems DSync');
176
		}
177
178
		$event->setCircle($localCircle);
179
	}
180
181
182
	private function compareMembers(Member $member1, Member $member2) {
183
		if ($member1->getInstance() === '') {
184
			$member1->setInstance($this->configService->getLocalCloudId());
185
		}
186
187
		if ($member2->getInstance() === '') {
188
			$member2->setInstance($this->configService->getLocalCloudId());
189
		}
190
191
		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...
192
			|| $member1->getUserId() !== $member2->getUserId()
193
			|| $member1->getType() <> $member2->getType()
194
			|| $member1->getLevel() <> $member2->getLevel()
195
			|| $member1->getStatus() !== $member2->getStatus()
196
			|| $member1->getInstance() !== $member2->getInstance()) {
197
			return false;
198
		}
199
200
		return true;
201
	}
202
203
204
	protected function cleanMember(Member $member) {
205
		if ($member->getInstance() === $this->configService->getLocalCloudId()) {
206
			$member->setInstance('');
207
		}
208
	}
209
210
}
211
212