Completed
Push — some-scrutinizing ( 29b6ab...e5bac5 )
by Maxence
18:16
created

Application   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 212
Duplicated Lines 12.74 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 10
dl 27
loc 212
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
B registerServices() 0 44 1
B registerControllers() 27 33 1
A registerMappers() 0 20 1
B registerCores() 0 32 2
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\NavigationController;
30
use \OCA\Circles\Controller\CirclesController;
31
use \OCA\Circles\Controller\MembersController;
32
33
34
use \OCA\Circles\Db\CirclesMapper;
35
use \OCA\Circles\Db\MembersMapper;
36
use \OCA\Circles\Service\DatabaseService;
37
use \OCA\Circles\Service\CirclesService;
38
use \OCA\Circles\Service\MembersService;
39
use \OCA\Circles\Service\ConfigService;
40
use \OCA\Circles\Service\MiscService;
41
use OCP\AppFramework\App;
42
43
class Application extends App {
44
45
	/** @var string */
46
	private $appName;
47
48
49
	/**
50
	 * @param array $params
51
	 */
52
	public function __construct(array $params = array()) {
53
		parent::__construct('circles', $params);
54
55
		$container = $this->getContainer();
56
		$this->appName = $container->query('AppName');
57
58
		self::registerServices($container);
59
		self::registerControllers($container);
60
		self::registerMappers($container);
61
		self::registerCores($container);
62
63
		// Translates
64
		$container->registerService(
65
			'L10N', function($c) {
66
			return $c->query('ServerContainer')
67
					 ->getL10N($c->query('AppName'));
68
		}
69
		);
70
	}
71
72
73
	/**
74
	 * Register Containers
75
	 *
76
	 * @param $container
77
	 */
78
	private function registerServices(& $container) {
79
80
		$container->registerService(
81
			'MiscService', function($c) {
82
			return new MiscService($c->query('Logger'), $c->query('AppName'));
83
		}
84
		);
85
86
87
		$container->registerService(
88
			'ConfigService', function($c) {
89
			return new ConfigService(
90
				$c->query('AppName'), $c->query('CoreConfig'), $c->query('UserId'),
91
				$c->query('MiscService')
92
			);
93
		}
94
		);
95
96
		$container->registerService(
97
			'DatabaseService', function($c) {
98
			return new DatabaseService(
99
				$c->query('CirclesMapper'), $c->query('MembersMapper')
100
			);
101
		}
102
		);
103
104
		$container->registerService(
105
			'CirclesService', function($c) {
106
			return new CirclesService(
107
				$c->query('UserId'), $c->query('L10N'), $c->query('ConfigService'),
108
				$c->query('DatabaseService'), $c->query('MiscService')
109
			);
110
		}
111
		);
112
113
		$container->registerService(
114
			'MembersService', function($c) {
115
			return new MembersService(
116
				$c->query('UserId'), $c->query('L10N'), $c->query('UserManager'),
117
				$c->query('ConfigService'), $c->query('DatabaseService'), $c->query('MiscService')
118
			);
119
		}
120
		);
121
	}
122
123
124
	/**
125
	 * Register Controllers
126
	 *
127
	 * @param $container
128
	 */
129
	private static function registerControllers(& $container) {
130
131
		$container->registerService(
132 View Code Duplication
			'NavigationController', function($c) {
133
			return new NavigationController(
134
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
135
				$c->query('ConfigService'), $c->query('CirclesService'),
136
				$c->query('MembersService'), $c->query('MiscService')
137
			);
138
		}
139
		);
140
141
		$container->registerService(
142 View Code Duplication
			'CirclesController', function($c) {
143
			return new CirclesController(
144
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
145
				$c->query('ConfigService'), $c->query('CirclesService'),
146
				$c->query('MembersService'), $c->query('MiscService')
147
			);
148
		}
149
		);
150
151
		$container->registerService(
152 View Code Duplication
			'MembersController', function($c) {
153
			return new MembersController(
154
				$c->query('AppName'), $c->query('Request'), $c->query('UserId'), $c->query('L10N'),
155
				$c->query('ConfigService'), $c->query('CirclesService'),
156
				$c->query('MembersService'), $c->query('MiscService')
157
			);
158
		}
159
		);
160
161
	}
162
163
164
	/**
165
	 * Register Mappers
166
	 *
167
	 * @param $container
168
	 */
169
	private static function registerMappers(& $container) {
170
		$container->registerService(
171
			'CirclesMapper', function($c) {
172
			return new CirclesMapper(
173
				$c->query('ServerContainer')
174
				  ->getDatabaseConnection(), $c->query('MiscService')
175
			);
176
		}
177
		);
178
179
		$container->registerService(
180
			'MembersMapper', function($c) {
181
			return new MembersMapper(
182
				$c->query('ServerContainer')
183
				  ->getDatabaseConnection(), $c->query('MiscService')
184
			);
185
		}
186
		);
187
188
	}
189
190
191
	/**
192
	 * Register Cores
193
	 *
194
	 * @param $container
195
	 */
196
	private static function registerCores(& $container) {
197
		
198
		$container->registerService(
199
			'Logger', function($c) {
200
			return $c->query('ServerContainer')
201
					 ->getLogger();
202
		}
203
		);
204
		$container->registerService(
205
			'CoreConfig', function($c) {
206
			return $c->query('ServerContainer')
207
					 ->getConfig();
208
		}
209
		);
210
211
		$container->registerService(
212
			'UserId', function($c) {
213
			$user = $c->query('ServerContainer')
214
					  ->getUserSession()
215
					  ->getUser();
216
217
			return is_null($user) ? '' : $user->getUID();
218
		}
219
		);
220
221
		$container->registerService(
222
			'UserManager', function($c) {
223
			return $c->query('ServerContainer')
224
					 ->getUserManager();
225
		}
226
		);
227
	}
228
229
230
	/**
231
	 * Register Navigation Tab
232
	 */
233
	public function registerNavigation() {
234
235
		$this->getContainer()
236
			 ->getServer()
237
			 ->getNavigationManager()
238
			 ->add(
239
				 function() {
240
					 $urlGen = \OC::$server->getURLGenerator();
241
					 $navName = \OC::$server->getL10N($this->appName)
242
											->t('Circles');
243
244
					 return [
245
						 'id'    => $this->appName,
246
						 'order' => 5,
247
						 'href'  => $urlGen->linkToRoute('circles.Navigation.navigate'),
248
						 'icon'  => $urlGen->imagePath($this->appName, 'circles.svg'),
249
						 'name'  => $navName
250
					 ];
251
				 }
252
			 );
253
	}
254
}
255
256