Passed
Push — master ( 7972a5...654cd1 )
by Christoph
11:53 queued 12s
created

php$0 ➔ delegateSearchProviderRegistration()   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\Search\SearchComposer;
30
use OC\Support\CrashReport\Registry;
31
use OCP\AppFramework\App;
32
use OCP\AppFramework\Bootstrap\IRegistrationContext;
33
use OCP\EventDispatcher\IEventDispatcher;
34
use OCP\ILogger;
35
use Throwable;
36
37
class RegistrationContext {
38
39
	/** @var array[] */
40
	private $capabilities = [];
41
42
	/** @var array[] */
43
	private $crashReporters = [];
44
45
	/** @var array[] */
46
	private $services = [];
47
48
	/** @var array[] */
49
	private $aliases = [];
50
51
	/** @var array[] */
52
	private $parameters = [];
53
54
	/** @var array[] */
55
	private $eventListeners = [];
56
57
	/** @var array[] */
58
	private $middlewares = [];
59
60
	/** @var array[] */
61
	private $searchProviders = [];
62
63
	/** @var ILogger */
64
	private $logger;
65
66
	public function __construct(ILogger $logger) {
67
		$this->logger = $logger;
68
	}
69
70
	public function for(string $appId): IRegistrationContext {
71
		return new class($appId, $this) implements IRegistrationContext {
72
			/** @var string */
73
			private $appId;
74
75
			/** @var RegistrationContext */
76
			private $context;
77
78
			public function __construct(string $appId, RegistrationContext $context) {
79
				$this->appId = $appId;
80
				$this->context = $context;
81
			}
82
83
			public function registerCapability(string $capability): void {
84
				$this->context->registerCapability(
85
					$this->appId,
86
					$capability
87
				);
88
			}
89
90
			public function registerCrashReporter(string $reporterClass): void {
91
				$this->context->registerCrashReporter(
92
					$this->appId,
93
					$reporterClass
94
				);
95
			}
96
97
			public function registerService(string $name, callable $factory, bool $shared = true): void {
98
				$this->context->registerService(
99
					$this->appId,
100
					$name,
101
					$factory,
102
					$shared
103
				);
104
			}
105
106
			public function registerServiceAlias(string $alias, string $target): void {
107
				$this->context->registerServiceAlias(
108
					$this->appId,
109
					$alias,
110
					$target
111
				);
112
			}
113
114
			public function registerParameter(string $name, $value): void {
115
				$this->context->registerParameter(
116
					$this->appId,
117
					$name,
118
					$value
119
				);
120
			}
121
122
			public function registerEventListener(string $event, string $listener, int $priority = 0): void {
123
				$this->context->registerEventListener(
124
					$this->appId,
125
					$event,
126
					$listener,
127
					$priority
128
				);
129
			}
130
131
			public function registerMiddleware(string $class): void {
132
				$this->context->registerMiddleware(
133
					$this->appId,
134
					$class
135
				);
136
			}
137
138
			public function registerSearchProvider(string $class): void {
139
				$this->context->registerSearchProvider(
140
					$this->appId,
141
					$class
142
				);
143
			}
144
		};
145
	}
146
147
	public function registerCapability(string $appId, string $capability): void {
148
		$this->capabilities[] = [
149
			'appId' => $appId,
150
			'capability' => $capability
151
		];
152
	}
153
154
	public function registerCrashReporter(string $appId, string $reporterClass): void {
155
		$this->crashReporters[] = [
156
			'appId' => $appId,
157
			'class' => $reporterClass,
158
		];
159
	}
160
161
	public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
162
		$this->services[] = [
163
			"appId" => $appId,
164
			"name" => $name,
165
			"factory" => $factory,
166
			"sharred" => $shared,
167
		];
168
	}
169
170
	public function registerServiceAlias(string $appId, string $alias, string $target): void {
171
		$this->aliases[] = [
172
			"appId" => $appId,
173
			"alias" => $alias,
174
			"target" => $target,
175
		];
176
	}
177
178
	public function registerParameter(string $appId, string $name, $value): void {
179
		$this->parameters[] = [
180
			"appId" => $appId,
181
			"name" => $name,
182
			"value" => $value,
183
		];
184
	}
185
186
	public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
187
		$this->eventListeners[] = [
188
			"appId" => $appId,
189
			"event" => $event,
190
			"listener" => $listener,
191
			"priority" => $priority,
192
		];
193
	}
194
195
	public function registerMiddleware(string $appId, string $class): void {
196
		$this->middlewares[] = [
197
			"appId" => $appId,
198
			"class" => $class,
199
		];
200
	}
201
202
	public function registerSearchProvider(string $appId, string $class) {
203
		$this->searchProviders[] = [
204
			'appId' => $appId,
205
			'class' => $class,
206
		];
207
	}
208
209
	/**
210
	 * @param App[] $apps
211
	 */
212
	public function delegateCapabilityRegistrations(array $apps): void {
213
		foreach ($this->capabilities as $registration) {
214
			try {
215
				$apps[$registration['appId']]
216
					->getContainer()
217
					->registerCapability($registration['capability']);
218
			} catch (Throwable $e) {
219
				$appId = $registration['appId'];
220
				$this->logger->logException($e, [
221
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
222
					'level' => ILogger::ERROR,
223
				]);
224
			}
225
		}
226
	}
227
228
	/**
229
	 * @param App[] $apps
230
	 */
231
	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

231
	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...
232
		foreach ($this->crashReporters as $registration) {
233
			try {
234
				$registry->registerLazy($registration['class']);
235
			} catch (Throwable $e) {
236
				$appId = $registration['appId'];
237
				$this->logger->logException($e, [
238
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
239
					'level' => ILogger::ERROR,
240
				]);
241
			}
242
		}
243
	}
244
245
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
246
		foreach ($this->eventListeners as $registration) {
247
			try {
248
				if (isset($registration['priority'])) {
249
					$eventDispatcher->addServiceListener(
250
						$registration['event'],
251
						$registration['listener'],
252
						$registration['priority']
253
					);
254
				} else {
255
					$eventDispatcher->addServiceListener(
256
						$registration['event'],
257
						$registration['listener']
258
					);
259
				}
260
			} catch (Throwable $e) {
261
				$appId = $registration['appId'];
262
				$this->logger->logException($e, [
263
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
264
					'level' => ILogger::ERROR,
265
				]);
266
			}
267
		}
268
	}
269
270
	/**
271
	 * @param App[] $apps
272
	 */
273
	public function delegateContainerRegistrations(array $apps): void {
274
		foreach ($this->services as $registration) {
275
			try {
276
				/**
277
				 * Register the service and convert the callable into a \Closure if necessary
278
				 */
279
				$apps[$registration['appId']]
280
					->getContainer()
281
					->registerService(
282
						$registration['name'],
283
						Closure::fromCallable($registration['factory']),
284
						$registration['shared'] ?? true
285
					);
286
			} catch (Throwable $e) {
287
				$appId = $registration['appId'];
288
				$this->logger->logException($e, [
289
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
290
					'level' => ILogger::ERROR,
291
				]);
292
			}
293
		}
294
295
		foreach ($this->aliases as $registration) {
296
			try {
297
				$apps[$registration['appId']]
298
					->getContainer()
299
					->registerAlias(
300
						$registration['alias'],
301
						$registration['target']
302
					);
303
			} catch (Throwable $e) {
304
				$appId = $registration['appId'];
305
				$this->logger->logException($e, [
306
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
307
					'level' => ILogger::ERROR,
308
				]);
309
			}
310
		}
311
312
		foreach ($this->parameters as $registration) {
313
			try {
314
				$apps[$registration['appId']]
315
					->getContainer()
316
					->registerParameter(
317
						$registration['name'],
318
						$registration['value']
319
					);
320
			} catch (Throwable $e) {
321
				$appId = $registration['appId'];
322
				$this->logger->logException($e, [
323
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
324
					'level' => ILogger::ERROR,
325
				]);
326
			}
327
		}
328
	}
329
330
	/**
331
	 * @param App[] $apps
332
	 */
333
	public function delegateMiddlewareRegistrations(array $apps): void {
334
		foreach ($this->middlewares as $middleware) {
335
			try {
336
				$apps[$middleware['appId']]
337
					->getContainer()
338
					->registerMiddleWare($middleware['class']);
339
			} catch (Throwable $e) {
340
				$appId = $middleware['appId'];
341
				$this->logger->logException($e, [
342
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
343
					'level' => ILogger::ERROR,
344
				]);
345
			}
346
		}
347
	}
348
349
	/**
350
	 * @param App[] $apps
351
	 */
352
	public function delegateSearchProviderRegistration(array $apps, SearchComposer $searchComposer): 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

352
	public function delegateSearchProviderRegistration(/** @scrutinizer ignore-unused */ array $apps, SearchComposer $searchComposer): 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...
353
		foreach ($this->searchProviders as $registration) {
354
			try {
355
				$searchComposer->registerProvider($registration['class']);
356
			} catch (Throwable $e) {
357
				$appId = $registration['appId'];
358
				$this->logger->logException($e, [
359
					'message' => "Error during search provider registration of $appId: " . $e->getMessage(),
360
					'level' => ILogger::ERROR,
361
				]);
362
			}
363
		}
364
	}
365
}
366