Passed
Push — master ( dd0b96...52dd1e )
by Roeland
11:37 queued 11s
created

php$0 ➔ delegateCrashReporterRegistrations()   A

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
dl 0
loc 9
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\AppFramework\Bootstrap;
27
28
use Closure;
29
use OC\Support\CrashReport\Registry;
30
use OCP\AppFramework\App;
31
use OCP\AppFramework\Bootstrap\IRegistrationContext;
32
use OCP\EventDispatcher\IEventDispatcher;
33
use OCP\ILogger;
34
use Throwable;
35
36
class RegistrationContext {
37
38
	/** @var array[] */
39
	private $capabilities = [];
40
41
	/** @var array[] */
42
	private $crashReporters = [];
43
44
	/** @var array[] */
45
	private $services = [];
46
47
	/** @var array[] */
48
	private $aliases = [];
49
50
	/** @var array[] */
51
	private $parameters = [];
52
53
	/** @var array[] */
54
	private $eventListeners = [];
55
56
	/** @var array[] */
57
	private $middlewares = [];
58
59
	/** @var ILogger */
60
	private $logger;
61
62
	public function __construct(ILogger $logger) {
63
		$this->logger = $logger;
64
	}
65
66
	public function for(string $appId): IRegistrationContext {
67
		return new class($appId, $this) implements IRegistrationContext {
68
			/** @var string */
69
			private $appId;
70
71
			/** @var RegistrationContext */
72
			private $context;
73
74
			public function __construct(string $appId, RegistrationContext $context) {
75
				$this->appId = $appId;
76
				$this->context = $context;
77
			}
78
79
			public function registerCapability(string $capability): void {
80
				$this->context->registerCapability(
81
					$this->appId,
82
					$capability
83
				);
84
			}
85
86
			public function registerCrashReporter(string $reporterClass): void {
87
				$this->context->registerCrashReporter(
88
					$this->appId,
89
					$reporterClass
90
				);
91
			}
92
93
			public function registerService(string $name, callable $factory, bool $shared = true): void {
94
				$this->context->registerService(
95
					$this->appId,
96
					$name,
97
					$factory,
98
					$shared
99
				);
100
			}
101
102
			public function registerServiceAlias(string $alias, string $target): void {
103
				$this->context->registerServiceAlias(
104
					$this->appId,
105
					$alias,
106
					$target
107
				);
108
			}
109
110
			public function registerParameter(string $name, $value): void {
111
				$this->context->registerParameter(
112
					$this->appId,
113
					$name,
114
					$value
115
				);
116
			}
117
118
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
119
				$this->context->registerEventListener(
120
					$this->appId,
121
					$event,
122
					$listener,
123
					$priority
124
				);
125
			}
126
127
			public function registerMiddleware(string $class): void {
128
				$this->context->registerMiddleware(
129
					$this->appId,
130
					$class
131
				);
132
			}
133
		};
134
	}
135
136
	public function registerCapability(string $appId, string $capability): void {
137
		$this->capabilities[] = [
138
			'appId' => $appId,
139
			'capability' => $capability
140
		];
141
	}
142
143
	public function registerCrashReporter(string $appId, string $reporterClass): void {
144
		$this->crashReporters[] = [
145
			'appId' => $appId,
146
			'class' => $reporterClass,
147
		];
148
	}
149
150
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
151
		$this->services[] = [
152
			"appId" => $appId,
153
			"name" => $name,
154
			"factory" => $factory,
155
			"sharred" => $shared,
156
		];
157
	}
158
159
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
160
		$this->aliases[] = [
161
			"appId" => $appId,
162
			"alias" => $alias,
163
			"target" => $target,
164
		];
165
	}
166
167
	public function registerParameter(string $appId, string $name, $value): void {
168
		$this->parameters[] = [
169
			"appId" => $appId,
170
			"name" => $name,
171
			"value" => $value,
172
		];
173
	}
174
175
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
176
		$this->eventListeners[] = [
177
			"appId" => $appId,
178
			"event" => $event,
179
			"listener" => $listener,
180
			"priority" => $priority,
181
		];
182
	}
183
184
	public function registerMiddleware(string $appId, string $class): void {
185
		$this->middlewares[] = [
186
			"appId" => $appId,
187
			"class" => $class,
188
		];
189
	}
190
191
	/**
192
	 * @param App[] $apps
193
	 */
194
	public function delegateCapabilityRegistrations(array $apps): void {
195
		foreach ($this->capabilities as $registration) {
196
			try {
197
				$apps[$registration['appId']]
198
					->getContainer()
199
					->registerCapability($registration['capability']);
200
			} catch (Throwable $e) {
201
				$appId = $registration['appId'];
202
				$this->logger->logException($e, [
203
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
204
					'level' => ILogger::ERROR,
205
				]);
206
			}
207
		}
208
	}
209
210
	/**
211
	 * @param App[] $apps
212
	 */
213
	public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void {
0 ignored issues
show
Unused Code introduced by
The parameter $apps is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

213
	public function delegateCrashReporterRegistrations(/** @scrutinizer ignore-unused */ array $apps, Registry $registry): void {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
		foreach ($this->crashReporters as $registration) {
215
			try {
216
				$registry->registerLazy($registration['class']);
217
			} catch (Throwable $e) {
218
				$appId = $registration['appId'];
219
				$this->logger->logException($e, [
220
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
221
					'level' => ILogger::ERROR,
222
				]);
223
			}
224
		}
225
	}
226
227
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
228
		foreach ($this->eventListeners as $registration) {
229
			try {
230
				if (isset($registration['priority'])) {
231
					$eventDispatcher->addServiceListener(
232
						$registration['event'],
233
						$registration['listener'],
234
						$registration['priority']
235
					);
236
				} else {
237
					$eventDispatcher->addListener(
238
						$registration['event'],
239
						$registration['listener']
240
					);
241
				}
242
			} catch (Throwable $e) {
243
				$appId = $registration['appId'];
244
				$this->logger->logException($e, [
245
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
246
					'level' => ILogger::ERROR,
247
				]);
248
			}
249
		}
250
	}
251
252
	/**
253
	 * @param App[] $apps
254
	 */
255
	public function delegateContainerRegistrations(array $apps): void {
256
		foreach ($this->services as $registration) {
257
			try {
258
				/**
259
				 * Register the service and convert the callable into a \Closure if necessary
260
				 */
261
				$apps[$registration['appId']]
262
					->getContainer()
263
					->registerService(
264
						$registration['name'],
265
						Closure::fromCallable($registration['factory']),
266
						$registration['shared'] ?? true
267
					);
268
			} catch (Throwable $e) {
269
				$appId = $registration['appId'];
270
				$this->logger->logException($e, [
271
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
272
					'level' => ILogger::ERROR,
273
				]);
274
			}
275
		}
276
277
		foreach ($this->aliases as $registration) {
278
			try {
279
				$apps[$registration['appId']]
280
					->getContainer()
281
					->registerAlias(
282
						$registration['alias'],
283
						$registration['target']
284
					);
285
			} catch (Throwable $e) {
286
				$appId = $registration['appId'];
287
				$this->logger->logException($e, [
288
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
289
					'level' => ILogger::ERROR,
290
				]);
291
			}
292
		}
293
294
		foreach ($this->parameters as $registration) {
295
			try {
296
				$apps[$registration['appId']]
297
					->getContainer()
298
					->registerParameter(
299
						$registration['name'],
300
						$registration['value']
301
					);
302
			} catch (Throwable $e) {
303
				$appId = $registration['appId'];
304
				$this->logger->logException($e, [
305
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
306
					'level' => ILogger::ERROR,
307
				]);
308
			}
309
		}
310
	}
311
312
	/**
313
	 * @param App[] $apps
314
	 */
315
	public function delegateMiddlewareRegistrations(array $apps): void {
316
		foreach ($this->middlewares as $middleware) {
317
			try {
318
				$apps[$middleware['appId']]
319
					->getContainer()
320
					->registerMiddleWare($middleware['class']);
321
			} catch (Throwable $e) {
322
				$appId = $middleware['appId'];
323
				$this->logger->logException($e, [
324
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
325
					'level' => ILogger::ERROR,
326
				]);
327
			}
328
		}
329
	}
330
}
331