Completed
Push — master ( b890e4...0afa7f )
by Maxence
10:12 queued 07:24
created

Application::registerNavigation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
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('EventsService'), $c->query('MiscService')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 114 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

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