Completed
Pull Request — master (#72)
by Maxence
06:31
created

Application::registerServices()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 41

Duplication

Lines 21
Ratio 27.63 %

Importance

Changes 0
Metric Value
dl 21
loc 76
rs 8.9667
c 0
b 0
f 0
cc 1
eloc 41
nc 1
nop 1

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
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
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
27
namespace OCA\Circles\AppInfo;
28
29
use OCA\Circles\Controller\FederatedController;
30
use \OCA\Circles\Controller\NavigationController;
31
use \OCA\Circles\Controller\CirclesController;
32
use \OCA\Circles\Controller\MembersController;
33
34
35
use OCA\Circles\Controller\SharesController;
36
use \OCA\Circles\Db\CirclesMapper;
37
use OCA\Circles\Db\CirclesRequest;
38
use OCA\Circles\Db\FederatedLinksRequest;
39
use \OCA\Circles\Db\MembersMapper;
40
use OCA\Circles\Events\UserEvents;
41
use OCA\Circles\Service\BroadcastService;
42
use \OCA\Circles\Service\DatabaseService;
43
use \OCA\Circles\Service\CirclesService;
44
use OCA\Circles\Service\FederatedService;
45
use \OCA\Circles\Service\MembersService;
46
use \OCA\Circles\Service\ConfigService;
47
use \OCA\Circles\Service\MiscService;
48
use OCA\Circles\Service\SharesService;
49
use OCP\AppFramework\App;
50
use OCP\AppFramework\IAppContainer;
51
use OCP\Util;
52
53
class Application extends App {
54
55
	/** @var string */
56
	private $appName;
57
58
59
	/**
60
	 * @param array $params
61
	 */
62
	public function __construct(array $params = array()) {
63
		parent::__construct('circles', $params);
64
65
		$container = $this->getContainer();
66
		$this->appName = $container->query('AppName');
67
68
		self::registerServices($container);
69
		self::registerControllers($container);
70
		self::registerMappers($container);
71
		self::registerDatabaseRequesters($container);
72
		self::registerCores($container);
73
		self::registerEvents($container);
74
		self::registerHooks();
75
76
		// Translates
77
		$container->registerService(
78
			'L10N', function(IAppContainer $c) {
79
			return $c->query('ServerContainer')
80
					 ->getL10N($c->query('AppName'));
81
		}
82
		);
83
	}
84
85
86
	/**
87
	 * Register Containers
88
	 *
89
	 * @param $container
90
	 */
91
	private function registerServices(IAppContainer &$container) {
92
93
		$container->registerService(
94
			'MiscService', function(IAppContainer $c) {
95
			return new MiscService($c->query('Logger'), $c->query('AppName'));
96
		}
97
		);
98
99
100
		$container->registerService(
101
			'ConfigService', function(IAppContainer $c) {
102
			return new ConfigService(
103
				$c->query('AppName'), $c->query('CoreConfig'), $c->query('UserId'),
104
				$c->query('MiscService')
105
			);
106
		}
107
		);
108
109
		$container->registerService(
110
			'DatabaseService', function(IAppContainer $c) {
111
			return new DatabaseService(
112
				$c->query('CirclesMapper'), $c->query('MembersMapper')
113
			);
114
		}
115
		);
116
117
		$container->registerService(
118 View Code Duplication
			'CirclesService', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
			return new CirclesService(
120
				$c->query('UserId'), $c->query('L10N'), $c->query('ConfigService'),
121
				$c->query('DatabaseService'), $c->query('MiscService')
122
			);
123
		}
124
		);
125
126
		$container->registerService(
127 View Code Duplication
			'MembersService', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
			return new MembersService(
129
				$c->query('UserId'), $c->query('L10N'), $c->query('UserManager'),
130
				$c->query('ConfigService'), $c->query('DatabaseService'), $c->query('MiscService')
131
			);
132
		}
133
		);
134
135
136
		$container->registerService(
137
			'BroadcastService', function(IAppContainer $c) {
138
			return new BroadcastService(
139
				$c->query('UserId'), $c->query('ConfigService'), $c->query('CirclesRequest'),
140
				$c->query('MiscService')
141
			);
142
		}
143
		);
144
145
146
		$container->registerService(
147
			'SharesService', function(IAppContainer $c) {
148
			return new SharesService(
149
				$c->query('UserId'), $c->query('ConfigService'), $c->query('CirclesRequest'),
150
				$c->query('BroadcastService'), $c->query('FederatedService'), $c->query('MiscService')
151
			);
152
		}
153
		);
154
155
		$container->registerService(
156 View Code Duplication
			'FederatedService', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
			return new FederatedService(
158
				$c->query('UserId'), $c->query('L10N'), $c->query('CirclesRequest'),
159
				$c->query('ConfigService'), $c->query('DatabaseService'),
160
				$c->query('CirclesService'), $c->query('BroadcastService'),
161
				$c->query('FederatedLinksRequest'),
162
				$c->query('ServerHost'), $c->query('HTTPClientService'), $c->query('MiscService')
163
			);
164
		}
165
		);
166
	}
167
168
169
	/**
170
	 * Register Controllers
171
	 *
172
	 * @param $container
173
	 */
174
	private static function registerControllers(IAppContainer &$container) {
175
176
		$container->registerService(
177 View Code Duplication
			'NavigationController', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
			return new NavigationController(
179
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
180
				$c->query('ConfigService'), $c->query('CirclesService'),
181
				$c->query('MembersService'), $c->query('SharesService'),
182
				$c->query('FederatedService'), $c->query('MiscService')
183
			);
184
		}
185
		);
186
187
188
		$container->registerService(
189 View Code Duplication
			'CirclesController', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
			return new CirclesController(
191
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
192
				$c->query('ConfigService'), $c->query('CirclesService'),
193
				$c->query('MembersService'), $c->query('SharesService'),
194
				$c->query('FederatedService'), $c->query('MiscService')
195
			);
196
		}
197
		);
198
199
		$container->registerService(
200 View Code Duplication
			'MembersController', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
			return new MembersController(
202
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
203
				$c->query('ConfigService'), $c->query('CirclesService'),
204
				$c->query('MembersService'), $c->query('SharesService'),
205
				$c->query('FederatedService'), $c->query('MiscService')
206
			);
207
		}
208
		);
209
210
		$container->registerService(
211 View Code Duplication
			'SharesController', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
			return new SharesController(
213
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
214
				$c->query('ConfigService'), $c->query('CirclesService'),
215
				$c->query('MembersService'), $c->query('SharesService'),
216
				$c->query('FederatedService'), $c->query('MiscService')
217
			);
218
		}
219
		);
220
221
		$container->registerService(
222 View Code Duplication
			'FederatedController', function(IAppContainer $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
			return new FederatedController(
224
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
225
				$c->query('ConfigService'), $c->query('CirclesService'),
226
				$c->query('MembersService'), $c->query('SharesService'),
227
				$c->query('FederatedService'), $c->query('MiscService')
228
			);
229
		}
230
		);
231
232
	}
233
234
235
	/**
236
	 * Register Request Builders
237
	 *
238
	 * @param IAppContainer $container
239
	 */
240 View Code Duplication
	private static function registerDatabaseRequesters(IAppContainer &$container) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
242
		$container->registerService(
243
			'CirclesRequest', function(IAppContainer $c) {
244
			return new CirclesRequest(
245
				$c->query('L10N'), $c->query('ServerContainer')
246
									 ->getDatabaseConnection(), $c->query('MiscService')
247
			);
248
		}
249
		);
250
251
		$container->registerService(
252
			'FederatedLinksRequest', function(IAppContainer $c) {
253
			return new FederatedLinksRequest(
254
				$c->query('ServerContainer')
255
				  ->getDatabaseConnection(), $c->query('MiscService')
256
			);
257
		}
258
		);
259
260
261
	}
262
263
	/**
264
	 * Register Mappers
265
	 *
266
	 * @param $container
267
	 */
268 View Code Duplication
	private static function registerMappers(IAppContainer &$container) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
270
		$container->registerService(
271
			'CirclesMapper', function(IAppContainer $c) {
272
			return new CirclesMapper(
273
				$c->query('ServerContainer')
274
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
275
			);
276
		}
277
		);
278
279
		$container->registerService(
280
			'MembersMapper', function(IAppContainer $c) {
281
			return new MembersMapper(
282
				$c->query('ServerContainer')
283
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
284
			);
285
		}
286
		);
287
288
	}
289
290
291
	/**
292
	 * Register Cores
293
	 *
294
	 * @param $container
295
	 */
296
	private static function registerCores(IAppContainer &$container) {
297
298
		$container->registerService(
299
			'Logger', function(IAppContainer $c) {
300
			return $c->query('ServerContainer')
301
					 ->getLogger();
302
		}
303
		);
304
		$container->registerService(
305
			'CoreConfig', function(IAppContainer $c) {
306
			return $c->query('ServerContainer')
307
					 ->getConfig();
308
		}
309
		);
310
311
		$container->registerService(
312
			'UserId', function(IAppContainer $c) {
313
			$user = $c->query('ServerContainer')
314
					  ->getUserSession()
315
					  ->getUser();
316
317
			/** @noinspection PhpUndefinedMethodInspection */
318
			return is_null($user) ? '' : $user->getUID();
319
		}
320
		);
321
322
		$container->registerService(
323
			'UserManager', function(IAppContainer $c) {
324
			return $c->query('ServerContainer')
325
					 ->getUserManager();
326
		}
327
		);
328
329
		$container->registerService(
330
			'HTTPClientService', function(IAppContainer $c) {
331
			return $c->query('ServerContainer')
332
					 ->getHTTPClientService();
333
		}
334
		);
335
336
337
		$container->registerService(
338
			'ServerHost', function(IAppContainer $c) {
339
			return $c->query('ServerContainer')
340
					 ->getRequest()
341
					 ->getServerHost();
342
		}
343
		);
344
345
	}
346
347
348
	public function registerHooks() {
349
		Util::connectHook(
350
			'OC_User', 'post_createUser', '\OCA\Circles\Hooks\UserHooks', 'onUserCreated'
351
		);
352
		Util::connectHook(
353
			'OC_User', 'post_deleteUser', '\OCA\Circles\Hooks\UserHooks', 'onUserDeleted'
354
		);
355
	}
356
357
358
	public function registerEvents(IAppContainer $container) {
359
		$container->registerService(
360
			'UserEvents', function(IAppContainer $c) {
361
			return new UserEvents($c->query('MembersService'), $c->query('MiscService'));
362
		}
363
		);
364
	}
365
366
	/**
367
	 * Register Navigation Tab
368
	 */
369
	public function registerNavigation() {
370
371
		$this->getContainer()
372
			 ->getServer()
373
			 ->getNavigationManager()
374
			 ->add(
375
				 function() {
376
					 $urlGen = \OC::$server->getURLGenerator();
377
					 $navName = \OC::$server->getL10N($this->appName)
378
											->t('Circles');
379
380
					 return [
381
						 'id'    => $this->appName,
382
						 'order' => 5,
383
						 'href'  => $urlGen->linkToRoute('circles.Navigation.navigate'),
384
						 'icon'  => $urlGen->imagePath($this->appName, 'circles.svg'),
385
						 'name'  => $navName
386
					 ];
387
				 }
388
			 );
389
	}
390
}
391
392