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

AGlobalScaleEvent::compareMembers()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 8.8333
c 0
b 0
f 0
cc 7
nc 2
nop 2
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 MiscService $miscService
97
	 */
98 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...
99
		SharesRequest $sharesRequest,
100
		TokensRequest $tokensRequest,
101
		CirclesRequest $circlesRequest,
102
		MembersRequest $membersRequest,
103
		CirclesService $circlesService,
104
		MembersService $membersService,
105
		EventsService $eventsService,
106
		ConfigService $configService,
107
		MiscService $miscService
108
	) {
109
		$this->sharesRequest = $sharesRequest;
110
		$this->tokensRequest = $tokensRequest;
111
		$this->circlesRequest = $circlesRequest;
112
		$this->membersRequest = $membersRequest;
113
		$this->circlesService = $circlesService;
114
		$this->membersService = $membersService;
115
		$this->eventsService = $eventsService;
116
		$this->configService = $configService;
117
		$this->miscService = $miscService;
118
	}
119
120
121
	/**
122
	 * @param GSEvent $event
123
	 * @param bool $localCheck
124
	 *
125
	 * @param bool $mustBeCheck
126
	 *
127
	 * @throws CircleDoesNotExistException
128
	 * @throws ConfigNoCircleAvailableException
129
	 * @throws GlobalScaleDSyncException
130
	 * @throws GlobalScaleEventException
131
	 */
132
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void {
133
		if ($localCheck) {
134
			$this->checkViewer($event, $mustBeCheck);
135
		}
136
	}
137
138
139
	/**
140
	 * @param GSEvent $event
141
	 */
142
	abstract public function manage(GSEvent $event): void;
143
144
145
	/**
146
	 * @param GSEvent $event
147
	 * @param bool $mustBeChecked
148
	 *
149
	 * @throws CircleDoesNotExistException
150
	 * @throws ConfigNoCircleAvailableException
151
	 * @throws GlobalScaleDSyncException
152
	 * @throws GlobalScaleEventException
153
	 */
154
	private function checkViewer(GSEvent $event, bool $mustBeChecked) {
155
		if (!$event->hasViewer() || !$event->hasCircle()) {
156
			if ($mustBeChecked) {
157
				throw new GlobalScaleEventException('GSEvent cannot be checked');
158
			} else {
159
				return;
160
			}
161
		}
162
163
		$circle = $event->getCircle();
164
		$viewer = $event->getViewer();
165
166
		$localCircle = $this->circlesRequest->getCircle(
167
			$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance()
168
		);
169
170
		if (!$this->compareMembers($viewer, $circle->getViewer())) {
171
			throw new GlobalScaleDSyncException('Viewer seems DSync');
172
		}
173
174
		$event->setCircle($localCircle);
175
	}
176
177
178
	private function compareMembers(Member $member1, Member $member2) {
179
		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...
180
			|| $member1->getUserId() !== $member2->getUserId()
181
			|| $member1->getType() <> $member2->getType()
182
			|| $member1->getLevel() <> $member2->getLevel()
183
			|| $member1->getStatus() !== $member2->getStatus()
184
			|| $member1->getInstance() !== $member2->getInstance()) {
185
			return false;
186
		}
187
188
		return true;
189
	}
190
191
192
193
	protected function cleanMember(Member $member) {
194
		if ($member->getInstance() === $this->configService->getLocalCloudId()) {
195
			$member->setInstance('');
196
		}
197
	}
198
199
}
200
201