Passed
Push — master ( 24ec4a...f04f93 )
by Roeland
13:26 queued 11s
created

delegateContainerRegistrations()

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
nc 27
nop 1
dl 0
loc 52

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

248
	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...
249
		foreach ($this->crashReporters as $registration) {
250
			try {
251
				$registry->registerLazy($registration['class']);
252
			} catch (Throwable $e) {
253
				$appId = $registration['appId'];
254
				$this->logger->logException($e, [
255
					'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
256
					'level' => ILogger::ERROR,
257
				]);
258
			}
259
		}
260
	}
261
262
	/**
263
	 * @param App[] $apps
264
	 */
265
	public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): 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

265
	public function delegateDashboardPanelRegistrations(/** @scrutinizer ignore-unused */ array $apps, IManager $dashboardManager): 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...
266
		foreach ($this->dashboardPanels as $panel) {
267
			try {
268
				$dashboardManager->lazyRegisterPanel($panel['class']);
269
			} catch (Throwable $e) {
270
				$appId = $panel['appId'];
271
				$this->logger->logException($e, [
272
					'message' => "Error during dashboard registration of $appId: " . $e->getMessage(),
273
					'level' => ILogger::ERROR,
274
				]);
275
			}
276
		}
277
	}
278
279
	public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
280
		foreach ($this->eventListeners as $registration) {
281
			try {
282
				if (isset($registration['priority'])) {
283
					$eventDispatcher->addServiceListener(
284
						$registration['event'],
285
						$registration['listener'],
286
						$registration['priority']
287
					);
288
				} else {
289
					$eventDispatcher->addServiceListener(
290
						$registration['event'],
291
						$registration['listener']
292
					);
293
				}
294
			} catch (Throwable $e) {
295
				$appId = $registration['appId'];
296
				$this->logger->logException($e, [
297
					'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
298
					'level' => ILogger::ERROR,
299
				]);
300
			}
301
		}
302
	}
303
304
	/**
305
	 * @param App[] $apps
306
	 */
307
	public function delegateContainerRegistrations(array $apps): void {
308
		foreach ($this->services as $registration) {
309
			try {
310
				/**
311
				 * Register the service and convert the callable into a \Closure if necessary
312
				 */
313
				$apps[$registration['appId']]
314
					->getContainer()
315
					->registerService(
316
						$registration['name'],
317
						Closure::fromCallable($registration['factory']),
318
						$registration['shared'] ?? true
319
					);
320
			} catch (Throwable $e) {
321
				$appId = $registration['appId'];
322
				$this->logger->logException($e, [
323
					'message' => "Error during service registration of $appId: " . $e->getMessage(),
324
					'level' => ILogger::ERROR,
325
				]);
326
			}
327
		}
328
329
		foreach ($this->aliases as $registration) {
330
			try {
331
				$apps[$registration['appId']]
332
					->getContainer()
333
					->registerAlias(
334
						$registration['alias'],
335
						$registration['target']
336
					);
337
			} catch (Throwable $e) {
338
				$appId = $registration['appId'];
339
				$this->logger->logException($e, [
340
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
341
					'level' => ILogger::ERROR,
342
				]);
343
			}
344
		}
345
346
		foreach ($this->parameters as $registration) {
347
			try {
348
				$apps[$registration['appId']]
349
					->getContainer()
350
					->registerParameter(
351
						$registration['name'],
352
						$registration['value']
353
					);
354
			} catch (Throwable $e) {
355
				$appId = $registration['appId'];
356
				$this->logger->logException($e, [
357
					'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
358
					'level' => ILogger::ERROR,
359
				]);
360
			}
361
		}
362
	}
363
364
	/**
365
	 * @param App[] $apps
366
	 */
367
	public function delegateMiddlewareRegistrations(array $apps): void {
368
		foreach ($this->middlewares as $middleware) {
369
			try {
370
				$apps[$middleware['appId']]
371
					->getContainer()
372
					->registerMiddleWare($middleware['class']);
373
			} catch (Throwable $e) {
374
				$appId = $middleware['appId'];
375
				$this->logger->logException($e, [
376
					'message' => "Error during capability registration of $appId: " . $e->getMessage(),
377
					'level' => ILogger::ERROR,
378
				]);
379
			}
380
		}
381
	}
382
383
	/**
384
	 * @return array[]
385
	 */
386
	public function getSearchProviders(): array {
387
		return $this->searchProviders;
388
	}
389
}
390