Completed
Push — master ( a7d2b9...a7d2b9 )
by Maxence
06:32 queued 02:55
created

Application   C

Complexity

Total Complexity 10

Size/Duplication

Total Lines 349
Duplicated Lines 27.22 %

Coupling/Cohesion

Components 1
Dependencies 19

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 19
dl 95
loc 349
rs 6.875
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
B registerServices() 12 83 1
A registerControllers() 40 59 1
A registerDatabaseRequesters() 22 22 1
A registerMappers() 21 21 1
A registerCores() 0 57 2
A registerHooks() 0 5 1
A registerEvents() 0 7 1
A registerNavigation() 0 21 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\EventsService;
45
use OCA\Circles\Service\FederatedService;
46
use \OCA\Circles\Service\MembersService;
47
use \OCA\Circles\Service\ConfigService;
48
use \OCA\Circles\Service\MiscService;
49
use OCA\Circles\Service\SharesService;
50
use OCP\AppFramework\App;
51
use OCP\AppFramework\IAppContainer;
52
use OCP\Util;
53
54
class Application extends App {
55
56
	/** @var string */
57
	private $appName;
58
59
60
	/**
61
	 * @param array $params
62
	 */
63
	public function __construct(array $params = array()) {
64
		parent::__construct('circles', $params);
65
66
		$container = $this->getContainer();
67
		$this->appName = $container->query('AppName');
68
69
		self::registerServices($container);
70
		self::registerControllers($container);
71
		self::registerMappers($container);
72
		self::registerDatabaseRequesters($container);
73
		self::registerCores($container);
74
		self::registerEvents($container);
75
		self::registerHooks();
76
77
		// Translates
78
		$container->registerService(
79
			'L10N', function(IAppContainer $c) {
80
			return $c->query('ServerContainer')
81
					 ->getL10N($c->query('AppName'));
82
		}
83
		);
84
	}
85
86
87
	/**
88
	 * Register Containers
89
	 *
90
	 * @param $container
91
	 */
92
	private function registerServices(IAppContainer &$container) {
93
94
		$container->registerService(
95
			'MiscService', function(IAppContainer $c) {
96
			return new MiscService($c->query('Logger'), $c->query('AppName'));
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('EventsService'), $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
		$container->registerService(
136
			'BroadcastService', function(IAppContainer $c) {
137
			return new BroadcastService(
138
				$c->query('UserId'), $c->query('ConfigService'), $c->query('CirclesRequest'),
139
				$c->query('MiscService')
140
			);
141
		}
142
		);
143
144
		$container->registerService(
145
			'SharesService', function(IAppContainer $c) {
146
			return new SharesService(
147
				$c->query('UserId'), $c->query('ConfigService'), $c->query('CirclesRequest'),
148
				$c->query('BroadcastService'), $c->query('FederatedService'),
149
				$c->query('MiscService')
150
			);
151
		}
152
		);
153
154
		$container->registerService(
155
			'EventsService', function(IAppContainer $c) {
156
			return new EventsService(
157
				$c->query('UserId'), $c->query('ActivityManager'), $c->query('UserManager'),
158
				$c->query('CirclesRequest'), $c->query('MiscService')
159
			);
160
		}
161
		);
162
163
164
		$container->registerService(
165
			'FederatedService', function(IAppContainer $c) {
166
			return new FederatedService(
167
				$c->query('UserId'), $c->query('L10N'), $c->query('CirclesRequest'),
168
				$c->query('ConfigService'), $c->query('CirclesService'),
169
				$c->query('BroadcastService'), $c->query('FederatedLinksRequest'),
170
				$c->query('ServerHost'), $c->query('HTTPClientService'), $c->query('MiscService')
171
			);
172
		}
173
		);
174
	}
175
176
177
	/**
178
	 * Register Controllers
179
	 *
180
	 * @param $container
181
	 */
182
	private static function registerControllers(IAppContainer &$container) {
183
184
		$container->registerService(
185 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...
186
			return new NavigationController(
187
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
188
				$c->query('ConfigService'), $c->query('CirclesService'),
189
				$c->query('MembersService'), $c->query('SharesService'),
190
				$c->query('FederatedService'), $c->query('MiscService')
191
			);
192
		}
193
		);
194
195
196
		$container->registerService(
197 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...
198
			return new CirclesController(
199
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
200
				$c->query('ConfigService'), $c->query('CirclesService'),
201
				$c->query('MembersService'), $c->query('SharesService'),
202
				$c->query('FederatedService'), $c->query('MiscService')
203
			);
204
		}
205
		);
206
207
		$container->registerService(
208 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...
209
			return new MembersController(
210
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
211
				$c->query('ConfigService'), $c->query('CirclesService'),
212
				$c->query('MembersService'), $c->query('SharesService'),
213
				$c->query('FederatedService'), $c->query('MiscService')
214
			);
215
		}
216
		);
217
218
		$container->registerService(
219 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...
220
			return new SharesController(
221
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
222
				$c->query('ConfigService'), $c->query('CirclesService'),
223
				$c->query('MembersService'), $c->query('SharesService'),
224
				$c->query('FederatedService'), $c->query('MiscService')
225
			);
226
		}
227
		);
228
229
		$container->registerService(
230 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...
231
			return new FederatedController(
232
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
233
				$c->query('ConfigService'), $c->query('CirclesService'),
234
				$c->query('MembersService'), $c->query('SharesService'),
235
				$c->query('FederatedService'), $c->query('MiscService')
236
			);
237
		}
238
		);
239
240
	}
241
242
243
	/**
244
	 * Register Request Builders
245
	 *
246
	 * @param IAppContainer $container
247
	 */
248 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...
249
250
		$container->registerService(
251
			'CirclesRequest', function(IAppContainer $c) {
252
			return new CirclesRequest(
253
				$c->query('L10N'), $c->query('ServerContainer')
254
									 ->getDatabaseConnection(), $c->query('MiscService')
255
			);
256
		}
257
		);
258
259
		$container->registerService(
260
			'FederatedLinksRequest', function(IAppContainer $c) {
261
			return new FederatedLinksRequest(
262
				$c->query('ServerContainer')
263
				  ->getDatabaseConnection(), $c->query('MiscService')
264
			);
265
		}
266
		);
267
268
269
	}
270
271
	/**
272
	 * Register Mappers
273
	 *
274
	 * @param $container
275
	 */
276 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...
277
278
		$container->registerService(
279
			'CirclesMapper', function(IAppContainer $c) {
280
			return new CirclesMapper(
281
				$c->query('ServerContainer')
282
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
283
			);
284
		}
285
		);
286
287
		$container->registerService(
288
			'MembersMapper', function(IAppContainer $c) {
289
			return new MembersMapper(
290
				$c->query('ServerContainer')
291
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
292
			);
293
		}
294
		);
295
296
	}
297
298
299
	/**
300
	 * Register Cores
301
	 *
302
	 * @param $container
303
	 */
304
	private static function registerCores(IAppContainer &$container) {
305
306
		$container->registerService(
307
			'Logger', function(IAppContainer $c) {
308
			return $c->query('ServerContainer')
309
					 ->getLogger();
310
		}
311
		);
312
		$container->registerService(
313
			'CoreConfig', function(IAppContainer $c) {
314
			return $c->query('ServerContainer')
315
					 ->getConfig();
316
		}
317
		);
318
319
		$container->registerService(
320
			'UserId', function(IAppContainer $c) {
321
			$user = $c->query('ServerContainer')
322
					  ->getUserSession()
323
					  ->getUser();
324
325
			/** @noinspection PhpUndefinedMethodInspection */
326
			return is_null($user) ? '' : $user->getUID();
327
		}
328
		);
329
330
		$container->registerService(
331
			'UserManager', function(IAppContainer $c) {
332
			return $c->query('ServerContainer')
333
					 ->getUserManager();
334
		}
335
		);
336
337
		$container->registerService(
338
			'ActivityManager', function(IAppContainer $c) {
339
			return $c->query('ServerContainer')
340
					 ->getActivityManager();
341
		}
342
		);
343
344
		$container->registerService(
345
			'HTTPClientService', function(IAppContainer $c) {
346
			return $c->query('ServerContainer')
347
					 ->getHTTPClientService();
348
		}
349
		);
350
351
352
		$container->registerService(
353
			'ServerHost', function(IAppContainer $c) {
354
			return $c->query('ServerContainer')
355
					 ->getRequest()
356
					 ->getServerHost();
357
		}
358
		);
359
360
	}
361
362
363
	public function registerHooks() {
364
		Util::connectHook(
365
			'OC_User', 'post_deleteUser', '\OCA\Circles\Hooks\UserHooks', 'onUserDeleted'
366
		);
367
	}
368
369
370
	public function registerEvents(IAppContainer $container) {
371
		$container->registerService(
372
			'UserEvents', function(IAppContainer $c) {
373
			return new UserEvents($c->query('MembersService'), $c->query('MiscService'));
374
		}
375
		);
376
	}
377
378
	/**
379
	 * Register Navigation Tab
380
	 */
381
	public function registerNavigation() {
382
383
		$this->getContainer()
384
			 ->getServer()
385
			 ->getNavigationManager()
386
			 ->add(
387
				 function() {
388
					 $urlGen = \OC::$server->getURLGenerator();
389
					 $navName = \OC::$server->getL10N($this->appName)
390
											->t('Circles');
391
392
					 return [
393
						 'id'    => $this->appName,
394
						 'order' => 5,
395
						 'href'  => $urlGen->linkToRoute('circles.Navigation.navigate'),
396
						 'icon'  => $urlGen->imagePath($this->appName, 'circles.svg'),
397
						 'name'  => $navName
398
					 ];
399
				 }
400
			 );
401
	}
402
}
403
404