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

GlobalScaleService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 117
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A asyncBroadcast() 0 19 3
A getGlobalScaleEvent() 0 13 3
A getKey() 0 3 1
A checkKey() 0 5 2
A checkEvent() 0 3 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\Request;
38
use daita\MySmallPhpTools\Traits\TRequest;
39
use OC;
40
use OCA\Circles\Db\GSEventsRequest;
41
use OCA\Circles\Exceptions\GlobalScaleEventException;
42
use OCA\Circles\Exceptions\GSKeyException;
43
use OCA\Circles\Exceptions\GSStatusException;
44
use OCA\Circles\GlobalScale\AGlobalScaleEvent;
45
use OCA\Circles\Model\GlobalScale\GSEvent;
46
use OCP\AppFramework\QueryException;
47
use OCP\IURLGenerator;
48
49
50
/**
51
 * Class GlobalScaleService
52
 *
53
 * @package OCA\Circles\Service
54
 */
55
class GlobalScaleService {
56
57
58
	use TRequest;
59
60
61
	/** @var IURLGenerator */
62
	private $urlGenerator;
63
64
	/** @var GSEventsRequest */
65
	private $gsEventsRequest;
66
67
	/** @var ConfigService */
68
	private $configService;
69
70
	/** @var MiscService */
71
	private $miscService;
72
73
74
	/**
75
	 * GlobalScaleService constructor.
76
	 *
77
	 * @param IURLGenerator $urlGenerator
78
	 * @param GSEventsRequest $gsEventsRequest
79
	 * @param ConfigService $configService
80
	 * @param MiscService $miscService
81
	 */
82
	public function __construct(
83
		IURLGenerator $urlGenerator,
84
		GSEventsRequest $gsEventsRequest,
85
		ConfigService $configService,
86
		MiscService $miscService
87
	) {
88
		$this->urlGenerator = $urlGenerator;
89
		$this->gsEventsRequest = $gsEventsRequest;
90
		$this->configService = $configService;
91
		$this->miscService = $miscService;
92
	}
93
94
95
	/**
96
	 * @param GSEvent $event
97
	 *
98
	 * @throws GSStatusException
99
	 */
100
	public function asyncBroadcast(GSEvent $event): void {
101
		if (!$this->configService->getGSStatus(ConfigService::GS_ENABLED)) {
102
			return;
103
		}
104
105
		$wrapper = $this->gsEventsRequest->create($event);
106
		$path = $this->urlGenerator->linkToRoute(
107
			'circles.GlobalScale.asyncBroadcast', ['token' => $wrapper->getToken()]
108
		);
109
110
		$request = new Request($path, Request::TYPE_PUT);
111
		$request->setAddressFromUrl($this->urlGenerator->getBaseUrl());
112
		$request->setDataSerialize($event);
113
114
		try {
115
			$this->doRequest($request);
116
		} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
117
		}
118
	}
119
120
121
	/**
122
	 * @param GSEvent $event
123
	 *
124
	 * @return AGlobalScaleEvent
125
	 * @throws GlobalScaleEventException
126
	 */
127
	public function getGlobalScaleEvent(GSEvent $event): AGlobalScaleEvent {
128
		$class = '\OCA\Circles\\' . $event->getType();
129
		try {
130
			$gs = OC::$server->query($class);
131
			if (!$gs instanceof AGlobalScaleEvent) {
132
				throw new GlobalScaleEventException($class . ' not an AGlobalScaleEvent');
133
			}
134
135
			return $gs;
136
		} 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...
137
			throw new GlobalScaleEventException('AGlobalScaleEvent ' . $class . ' not found');
138
		}
139
	}
140
141
142
	/**
143
	 * @return string
144
	 */
145
	public function getKey(): string {
146
		return 'abcd';
147
	}
148
149
150
	/**
151
	 * @param string $key
152
	 *
153
	 * @throws GSKeyException
154
	 */
155
	public function checkKey(string $key) {
156
		if ($key !== $this->getKey()) {
157
			throw new GSKeyException('invalid key');
158
		}
159
	}
160
161
162
	/**
163
	 * @param GSEvent $event
164
	 *
165
	 * @throws GSKeyException
166
	 */
167
	public function checkEvent(GSEvent $event) {
168
		$this->checkKey($event->getKey());
169
	}
170
171
}
172
173