Completed
Push — federated-circles ( 80cab6...74a72b )
by Maxence
02:32
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\MembersMapper;
39
use OCA\Circles\Events\UserEvents;
40
use \OCA\Circles\Service\DatabaseService;
41
use \OCA\Circles\Service\CirclesService;
42
use OCA\Circles\Service\FederatedService;
43
use \OCA\Circles\Service\MembersService;
44
use \OCA\Circles\Service\ConfigService;
45
use \OCA\Circles\Service\MiscService;
46
use OCA\Circles\Service\SharesService;
47
use OCP\AppFramework\App;
48
use OCP\AppFramework\IAppContainer;
49
use OCP\Util;
50
51
class Application extends App {
52
53
	/** @var string */
54
	private $appName;
55
56
57
	/**
58
	 * @param array $params
59
	 */
60
	public function __construct(array $params = array()) {
61
		parent::__construct('circles', $params);
62
63
		$container = $this->getContainer();
64
		$this->appName = $container->query('AppName');
65
66
		self::registerServices($container);
67
		self::registerControllers($container);
68
		self::registerMappers($container);
69
		self::registerDatabaseRequesters($container);
70
		self::registerCores($container);
71
		self::registerEvents($container);
72
		self::registerHooks();
73
74
		// Translates
75
		$container->registerService(
76
			'L10N', function(IAppContainer $c) {
77
			return $c->query('ServerContainer')
78
					 ->getL10N($c->query('AppName'));
79
		}
80
		);
81
	}
82
83
84
	/**
85
	 * Register Containers
86
	 *
87
	 * @param $container
88
	 */
89
	private function registerServices(IAppContainer &$container) {
90
91
		$container->registerService(
92
			'MiscService', function(IAppContainer $c) {
93
			return new MiscService($c->query('Logger'), $c->query('AppName'));
94
		}
95
		);
96
97
98
		$container->registerService(
99
			'ConfigService', function(IAppContainer $c) {
100
			return new ConfigService(
101
				$c->query('AppName'), $c->query('CoreConfig'), $c->query('UserId'),
102
				$c->query('MiscService')
103
			);
104
		}
105
		);
106
107
		$container->registerService(
108
			'DatabaseService', function(IAppContainer $c) {
109
			return new DatabaseService(
110
				$c->query('CirclesMapper'), $c->query('MembersMapper')
111
			);
112
		}
113
		);
114
115
		$container->registerService(
116
			'CirclesService', function(IAppContainer $c) {
117
			return new CirclesService(
118
				$c->query('UserId'), $c->query('L10N'), $c->query('ConfigService'),
119
				$c->query('DatabaseService'), $c->query('MiscService')
120
			);
121
		}
122
		);
123
124
		$container->registerService(
125 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...
126
			return new MembersService(
127
				$c->query('UserId'), $c->query('L10N'), $c->query('UserManager'),
128
				$c->query('ConfigService'), $c->query('DatabaseService'), $c->query('MiscService')
129
			);
130
		}
131
		);
132
133
		$container->registerService(
134
			'SharesService', function(IAppContainer $c) {
135
			return new SharesService(
136
				$c->query('UserId'), $c->query('CirclesRequest'), $c->query('MiscService')
137
			);
138
		}
139
		);
140
141
		$container->registerService(
142 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...
143
			return new FederatedService(
144
				$c->query('UserId'), $c->query('L10N'), $c->query('ConfigService'),
145
				$c->query('DatabaseService'), $c->query('CirclesService'),
146
				$c->query('HTTPClientService'), $c->query('MiscService')
147
			);
148
		}
149
		);
150
	}
151
152
153
	/**
154
	 * Register Controllers
155
	 *
156
	 * @param $container
157
	 */
158
	private static function registerControllers(IAppContainer &$container) {
159
160
		$container->registerService(
161 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...
162
			return new NavigationController(
163
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
164
				$c->query('ConfigService'), $c->query('CirclesService'),
165
				$c->query('MembersService'), $c->query('SharesService'),
166
				$c->query('FederatedService'), $c->query('MiscService')
167
			);
168
		}
169
		);
170
171
172
		$container->registerService(
173 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...
174
			return new CirclesController(
175
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
176
				$c->query('ConfigService'), $c->query('CirclesService'),
177
				$c->query('MembersService'), $c->query('SharesService'),
178
				$c->query('FederatedService'), $c->query('MiscService')
179
			);
180
		}
181
		);
182
183
		$container->registerService(
184 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...
185
			return new MembersController(
186
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
187
				$c->query('ConfigService'), $c->query('CirclesService'),
188
				$c->query('MembersService'), $c->query('SharesService'),
189
				$c->query('FederatedService'), $c->query('MiscService')
190
			);
191
		}
192
		);
193
194
		$container->registerService(
195 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...
196
			return new SharesController(
197
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
198
				$c->query('ConfigService'), $c->query('CirclesService'),
199
				$c->query('MembersService'), $c->query('SharesService'),
200
				$c->query('FederatedService'), $c->query('MiscService')
201
			);
202
		}
203
		);
204
205
		$container->registerService(
206 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...
207
			return new FederatedController(
208
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
209
				$c->query('ConfigService'), $c->query('CirclesService'),
210
				$c->query('MembersService'), $c->query('SharesService'),
211
				$c->query('FederatedService'), $c->query('MiscService')
212
			);
213
		}
214
		);
215
216
	}
217
218
219
	/**
220
	 * Register Request Builders
221
	 *
222
	 * @param IAppContainer $container
223
	 */
224
	private static function registerDatabaseRequesters(IAppContainer &$container) {
225
226
		$container->registerService(
227
			'CirclesRequest', function(IAppContainer $c) {
228
			return new CirclesRequest(
229
				$c->query('ServerContainer')
230
				  ->getDatabaseConnection(), $c->query('MiscService')
231
			);
232
		}
233
		);
234
235
	}
236
237
	/**
238
	 * Register Mappers
239
	 *
240
	 * @param $container
241
	 */
242
	private static function registerMappers(IAppContainer &$container) {
243
244
		$container->registerService(
245
			'CirclesMapper', function(IAppContainer $c) {
246
			return new CirclesMapper(
247
				$c->query('ServerContainer')
248
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
249
			);
250
		}
251
		);
252
253
		$container->registerService(
254
			'MembersMapper', function(IAppContainer $c) {
255
			return new MembersMapper(
256
				$c->query('ServerContainer')
257
				  ->getDatabaseConnection(), $c->query('L10N'), $c->query('MiscService')
258
			);
259
		}
260
		);
261
262
	}
263
264
265
	/**
266
	 * Register Cores
267
	 *
268
	 * @param $container
269
	 */
270
	private static function registerCores(IAppContainer &$container) {
271
272
		$container->registerService(
273
			'Logger', function(IAppContainer $c) {
274
			return $c->query('ServerContainer')
275
					 ->getLogger();
276
		}
277
		);
278
		$container->registerService(
279
			'CoreConfig', function(IAppContainer $c) {
280
			return $c->query('ServerContainer')
281
					 ->getConfig();
282
		}
283
		);
284
285
		$container->registerService(
286
			'UserId', function(IAppContainer $c) {
287
			$user = $c->query('ServerContainer')
288
					  ->getUserSession()
289
					  ->getUser();
290
291
			/** @noinspection PhpUndefinedMethodInspection */
292
			return is_null($user) ? '' : $user->getUID();
293
		}
294
		);
295
296
		$container->registerService(
297
			'UserManager', function(IAppContainer $c) {
298
			return $c->query('ServerContainer')
299
					 ->getUserManager();
300
		}
301
		);
302
303
		$container->registerService(
304
			'HTTPClientService', function(IAppContainer $c) {
305
			return $c->query('ServerContainer')
306
					 ->getHTTPClientService();
307
		}
308
		);
309
310
	}
311
312
313
	public function registerHooks() {
314
		Util::connectHook(
315
			'OC_User', 'post_createUser', '\OCA\Circles\Hooks\UserHooks', 'onUserCreated'
316
		);
317
		Util::connectHook(
318
			'OC_User', 'post_deleteUser', '\OCA\Circles\Hooks\UserHooks', 'onUserDeleted'
319
		);
320
	}
321
322
323
	public function registerEvents(IAppContainer $container) {
324
		$container->registerService(
325
			'UserEvents', function(IAppContainer $c) {
326
			return new UserEvents($c->query('MembersService'), $c->query('MiscService'));
327
		}
328
		);
329
	}
330
331
	/**
332
	 * Register Navigation Tab
333
	 */
334
	public function registerNavigation() {
335
336
		$this->getContainer()
337
			 ->getServer()
338
			 ->getNavigationManager()
339
			 ->add(
340
				 function() {
341
					 $urlGen = \OC::$server->getURLGenerator();
342
					 $navName = \OC::$server->getL10N($this->appName)
343
											->t('Circles');
344
345
					 return [
346
						 'id'    => $this->appName,
347
						 'order' => 5,
348
						 'href'  => $urlGen->linkToRoute('circles.Navigation.navigate'),
349
						 'icon'  => $urlGen->imagePath($this->appName, 'circles.svg'),
350
						 'name'  => $navName
351
					 ];
352
				 }
353
			 );
354
	}
355
}
356
357