Completed
Pull Request — master (#551)
by Maxence
02:16
created

GlobalScaleService::getGlobalScaleEvent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 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 2019
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\Service;
31
32
33
use daita\MySmallPhpTools\Exceptions\RequestContentException;
34
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
35
use daita\MySmallPhpTools\Exceptions\RequestResultSizeException;
36
use daita\MySmallPhpTools\Exceptions\RequestServerException;
37
use daita\MySmallPhpTools\Model\Nextcloud\nc21\NC21Request;
38
use daita\MySmallPhpTools\Model\Request;
39
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Request;
40
use daita\MySmallPhpTools\Traits\TStringTools;
41
use OC;
42
use OC\Security\IdentityProof\Signer;
43
use OC\User\NoUserException;
44
use OCA\Circles\Db\RemoteWrapperRequest;
45
use OCA\Circles\Exceptions\GlobalScaleEventException;
46
use OCA\Circles\Exceptions\GSKeyException;
47
use OCA\Circles\Exceptions\GSStatusException;
48
use OCA\Circles\GlobalScale\AGlobalScaleEvent;
49
use OCA\Circles\Model\GlobalScale\GSEvent;
50
use OCA\Circles\Model\GlobalScale\GSWrapper;
51
use OCP\AppFramework\QueryException;
52
use OCP\IURLGenerator;
53
use OCP\IUser;
54
use OCP\IUserManager;
55
use OCP\IUserSession;
56
57
58
/**
59
 * Class GlobalScaleService
60
 *
61
 * @package OCA\Circles\Service
62
 */
63
class GlobalScaleService {
64
65
66
	use TNC21Request;
67
	use TStringTools;
68
69
70
	/** @var IURLGenerator */
71
	private $urlGenerator;
72
73
	/** @var IUserManager */
74
	private $userManager;
75
76
	/** @var IUserSession */
77
	private $userSession;
78
79
	/** @var Signer */
80
	private $signer;
81
82
	/** @var RemoteWrapperRequest */
83
	private $remoteWrapperRequest;
84
85
	/** @var ConfigService */
86
	private $configService;
87
88
	/** @var MiscService */
89
	private $miscService;
90
91
92
	/**
93
	 * GlobalScaleService constructor.
94
	 *
95
	 * @param IURLGenerator $urlGenerator
96
	 * @param IUserManager $userManager
97
	 * @param IUserSession $userSession
98
	 * @param Signer $signer
99
	 * @param RemoteWrapperRequest $remoteWrapperRequest
100
	 * @param ConfigService $configService
101
	 * @param MiscService $miscService
102
	 */
103
	public function __construct(
104
		IURLGenerator $urlGenerator,
105
		IUserManager $userManager,
106
		IUserSession $userSession,
107
		Signer $signer,
108
		RemoteWrapperRequest $remoteWrapperRequest,
109
		ConfigService $configService,
110
		MiscService $miscService
111
	) {
112
		$this->urlGenerator = $urlGenerator;
113
		$this->userManager = $userManager;
114
		$this->userSession = $userSession;
115
		$this->signer = $signer;
116
		$this->remoteWrapperRequest = $remoteWrapperRequest;
117
		$this->configService = $configService;
118
		$this->miscService = $miscService;
119
	}
120
121
122
	/**
123
	 * @param GSEvent $event
124
	 *
125
	 * @return string
126
	 */
127
	public function asyncBroadcast(GSEvent $event): string {
128
		$wrapper = new GSWrapper();
129
		$wrapper->setEvent($event);
130
		$wrapper->setToken($this->uuid());
131
		$wrapper->setCreation(time());
132
		$wrapper->setSeverity($event->getSeverity());
133
134
		foreach ($this->getInstances($event->isAsync()) as $instance) {
0 ignored issues
show
Bug introduced by
The method getInstances() does not seem to exist on object<OCA\Circles\Service\GlobalScaleService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
			$wrapper->setInstance($instance);
136
			$wrapper = $this->remoteWrapperRequest->create($wrapper);
0 ignored issues
show
Documentation introduced by
$wrapper is of type object<OCA\Circles\Model...alScale\GSWrapper>|null, but the function expects a object<OCA\Circles\Model\Remote\RemoteWrapper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
Are you sure the assignment to $wrapper is correct as $this->remoteWrapperRequest->create($wrapper) (which targets OCA\Circles\Db\RemoteWrapperRequest::create()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
		}
138
139
		$request = new NC21Request('', Request::TYPE_POST);
140
		$this->configService->configureRequest(
141
			$request, 'circles.RemoteWrapper.asyncBroadcast', ['token' => $wrapper->getToken()]
142
		);
143
144
		try {
145
			$this->doRequest($request);
146
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) {
147
			$this->miscService->e($e);
148
		}
149
150
		return $wrapper->getToken();
151
	}
152
153
154
	/**
155
	 * @param GSEvent $event
156
	 *
157
	 * @return AGlobalScaleEvent
158
	 * @throws GlobalScaleEventException
159
	 */
160
	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
161
		$class = $this->getClassNameFromEvent($event);
162
		try {
163
			$gs = OC::$server->query($class);
164
			if (!$gs instanceof AGlobalScaleEvent) {
165
				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
166
			}
167
168
			return $gs;
169
		} catch (QueryException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\AppFramework\QueryException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
170
			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
171
		}
172
	}
173
174
175
	/**
176
	 * @return string
177
	 */
178
	public function getKey(): string {
179
		try {
180
			$key = $this->configService->getGSStatus(ConfigService::GS_KEY);
181
		} catch (GSStatusException $e) {
182
			$key = $this->configService->getAppValue(ConfigService::CIRCLES_LOCAL_GSKEY);
183
			if ($key === '') {
184
				$key = $this->token(31);
185
				$this->configService->setAppValue(ConfigService::CIRCLES_LOCAL_GSKEY, $key);
186
			}
187
		}
188
189
		return md5('gskey:' . $key);
190
	}
191
192
	/**
193
	 * @param string $key
194
	 *
195
	 * @throws GSKeyException
196
	 */
197
	public function checkKey(string $key) {
198
		if ($key !== $this->getKey()) {
199
			throw new GSKeyException('invalid key');
200
		}
201
	}
202
203
204
	/**
205
	 * @param GSEvent $event
206
	 *
207
	 * @throws GSKeyException
208
	 */
209
	public function checkEvent(GSEvent $event): void {
210
		$this->checkKey($event->getKey());
211
	}
212
213
214
	/**
215
	 * @param GSEvent $event
216
	 *
217
	 * @return string
218
	 * @throws GlobalScaleEventException
219
	 */
220
	private function getClassNameFromEvent(GSEvent $event): string {
221
		$className = $event->getType();
222
		if (substr($className, 0, 25) !== '\OCA\Circles\GlobalScale\\' || strpos($className, '.')) {
223
			throw new GlobalScaleEventException(
224
				$className . ' does not seems to be a secured AGlobalScaleEvent'
225
			);
226
		}
227
228
		return $className;
229
	}
230
231
232
	/**
233
	 * @return IUser
234
	 * @throws NoUserException
235
	 */
236
	private function getRandomUser(): IUser {
237
		$user = $this->userSession->getUser();
238
		if ($user !== null) {
239
			return $user;
240
		}
241
242
		$random = $this->userManager->search('', 1);
243
		if (sizeof($random) > 0) {
244
			return array_shift($random);
245
		}
246
247
		throw new NoUserException();
248
	}
249
250
}
251
252