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

AGlobalScaleEvent::verify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\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\EventsService;
45
use OCA\Circles\Service\MiscService;
46
47
48
/**
49
 * Class AGlobalScaleEvent
50
 *
51
 * @package OCA\Circles\GlobalScale
52
 */
53
abstract class AGlobalScaleEvent {
54
55
56
	/** @var SharesRequest */
57
	private $sharesRequest;
58
59
	/** @var TokensRequest */
60
	private $tokensRequest;
61
62
	/** @var CirclesRequest */
63
	protected $circlesRequest;
64
65
	/** @var MembersRequest */
66
	protected $membersRequest;
67
68
	/** @var CirclesService */
69
	protected $circlesService;
70
71
	/** @var EventsService */
72
	protected $eventsService;
73
74
	/** @var MiscService */
75
	protected $miscService;
76
77
78
	/**
79
	 * AGlobalScaleEvent constructor.
80
	 *
81
	 * @param SharesRequest $sharesRequest
82
	 * @param TokensRequest $tokensRequest
83
	 * @param CirclesRequest $circlesRequest
84
	 * @param MembersRequest $membersRequest
85
	 * @param CirclesService $circlesService
86
	 * @param EventsService $eventsService
87
	 * @param MiscService $miscService
88
	 */
89 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...
90
		SharesRequest $sharesRequest,
91
		TokensRequest $tokensRequest,
92
		CirclesRequest $circlesRequest,
93
		MembersRequest $membersRequest,
94
		CirclesService $circlesService,
95
		EventsService $eventsService,
96
		MiscService $miscService
97
	) {
98
		$this->sharesRequest = $sharesRequest;
99
		$this->tokensRequest = $tokensRequest;
100
		$this->circlesRequest = $circlesRequest;
101
		$this->membersRequest = $membersRequest;
102
		$this->circlesService = $circlesService;
103
		$this->eventsService = $eventsService;
104
		$this->miscService = $miscService;
105
	}
106
107
108
	/**
109
	 * @param GSEvent $event
110
	 * @param bool $localCheck
111
	 *
112
	 * @param bool $mustBeCheck
113
	 *
114
	 * @throws CircleDoesNotExistException
115
	 * @throws ConfigNoCircleAvailableException
116
	 * @throws GlobalScaleDSyncException
117
	 * @throws GlobalScaleEventException
118
	 */
119
	public function verify(GSEvent $event, bool $localCheck = false, bool $mustBeCheck = false): void {
120
		if ($localCheck) {
121
			$this->checkViewer($event, $mustBeCheck);
122
		}
123
	}
124
125
126
	/**
127
	 * @param GSEvent $event
128
	 */
129
	abstract public function manage(GSEvent $event): void;
130
131
132
	/**
133
	 * @param GSEvent $event
134
	 * @param bool $mustBeChecked
135
	 *
136
	 * @throws CircleDoesNotExistException
137
	 * @throws ConfigNoCircleAvailableException
138
	 * @throws GlobalScaleDSyncException
139
	 * @throws GlobalScaleEventException
140
	 */
141
	private function checkViewer(GSEvent $event, bool $mustBeChecked) {
142
		$this->miscService->log('######## ' . json_encode($event) . '    -- ' . $mustBeChecked);
143
		if (!$event->hasViewer() || !$event->hasCircle()) {
144
			if ($mustBeChecked) {
145
				throw new GlobalScaleEventException('GSEvent cannot be checked');
146
			} else {
147
				return;
148
			}
149
		}
150
151
		$circle = $event->getCircle();
152
		$viewer = $event->getViewer();
153
154
		$localCircle = $this->circlesRequest->getCircle(
155
			$circle->getUniqueId(), $viewer->getUserId(), $viewer->getInstance()
156
		);
157
158
		if (!$this->compareMembers($viewer, $circle->getViewer())) {
159
			throw new GlobalScaleDSyncException('Viewer seems DSync');
160
		}
161
162
		$event->setCircle($localCircle);
163
	}
164
165
166
	private function compareMembers(Member $member1, Member $member2) {
167
		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...
168
			|| $member1->getUserId() !== $member2->getUserId()
169
			|| $member1->getType() <> $member2->getType()
170
			|| $member1->getLevel() <> $member2->getLevel()
171
			|| $member1->getStatus() !== $member2->getStatus()
172
			|| $member1->getInstance() !== $member2->getInstance()) {
173
			return false;
174
		}
175
176
		return true;
177
	}
178
179
}
180
181