Completed
Pull Request — stable8.2 (#24656)
by Joas
12:22
created

Server   F

Complexity

Total Complexity 92

Size/Duplication

Total Lines 1031
Duplicated Lines 0.78 %

Coupling/Cohesion

Components 1
Dependencies 87

Test Coverage

Coverage 86.19%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 92
c 1
b 1
f 0
lcom 1
cbo 87
dl 8
loc 1031
ccs 412
cts 478
cp 0.8619
rs 0.8817

66 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 8 443 22
A getContactsManager() 0 3 1
A getEncryptionManager() 0 3 1
A getEncryptionFilesHelper() 0 3 1
A getEncryptionKeyStorage() 0 3 1
A getRequest() 0 3 1
A getPreviewManager() 0 3 1
A getTagManager() 0 3 1
A getAvatarManager() 0 3 1
A getRootFolder() 0 3 1
A getUserFolder() 0 11 3
A getAppFolder() 0 11 2
A getUserManager() 0 3 1
A getGroupManager() 0 3 1
A getUserSession() 0 3 1
A getSession() 0 3 1
A setSession() 0 3 1
A getNavigationManager() 0 3 1
A getConfig() 0 3 1
A getSystemConfig() 0 3 1
A getAppConfig() 0 3 1
A getL10NFactory() 0 3 1
A getL10N() 0 3 1
A getURLGenerator() 0 3 1
A getHelper() 0 3 1
A getCache() 0 3 1
A getMemCacheFactory() 0 3 1
A getDatabaseConnection() 0 3 1
A getActivityManager() 0 3 1
A getJobList() 0 3 1
A getLogger() 0 3 1
A getRouter() 0 3 1
A getSearch() 0 3 1
A getSecureRandom() 0 3 1
A getCrypto() 0 3 1
A getHasher() 0 3 1
A getDb() 0 3 1
A getHTTPHelper() 0 3 1
A getCertificateManager() 0 11 3
A getHTTPClientService() 0 3 1
A createEventSource() 0 3 1
A getEventLogger() 0 3 1
A getQueryLogger() 0 3 1
A getTempManager() 0 3 1
A getAppManager() 0 3 1
A getMailer() 0 3 1
A getWebRoot() 0 3 1
A getOcsClient() 0 3 1
A getDateTimeZone() 0 3 1
A getDateTimeFormatter() 0 3 1
A getMountProviderCollection() 0 3 1
A getIniWrapper() 0 3 1
A getCommandBus() 0 3 1
A getTrustedDomainHelper() 0 3 1
A getLockingProvider() 0 3 1
A getMountManager() 0 3 1
A getMimeTypeDetector() 0 3 1
A getMimeTypeLoader() 0 3 1
A getCapabilitiesManager() 0 3 1
A getEventDispatcher() 0 3 1
A getNotificationManager() 0 3 1
A getSessionCryptoWrapper() 0 3 1
A getStoragesBackendService() 0 3 1
A getGlobalStoragesService() 0 3 1
A getUserGlobalStoragesService() 0 3 1
A getUserStoragesService() 0 3 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Server often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Server, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Bernhard Reiter <[email protected]>
7
 * @author Björn Schießle <[email protected]>
8
 * @author Christopher Schäpers <[email protected]>
9
 * @author Individual IT Services <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Jörn Friedrich Dreyer <[email protected]>
12
 * @author Lukas Reschke <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Robin Appelman <[email protected]>
15
 * @author Robin McCorkell <[email protected]>
16
 * @author Roeland Jago Douma <[email protected]>
17
 * @author Sander <[email protected]>
18
 * @author Thomas Müller <[email protected]>
19
 * @author Thomas Tanghus <[email protected]>
20
 * @author Vincent Petry <[email protected]>
21
 *
22
 * @copyright Copyright (c) 2015, ownCloud, Inc.
23
 * @license AGPL-3.0
24
 *
25
 * This code is free software: you can redistribute it and/or modify
26
 * it under the terms of the GNU Affero General Public License, version 3,
27
 * as published by the Free Software Foundation.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32
 * GNU Affero General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU Affero General Public License, version 3,
35
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
36
 *
37
 */
38
namespace OC;
39
40
use bantu\IniGetWrapper\IniGetWrapper;
41
use OC\AppFramework\Http\Request;
42
use OC\AppFramework\Db\Db;
43
use OC\AppFramework\Utility\SimpleContainer;
44
use OC\AppFramework\Utility\TimeFactory;
45
use OC\Command\AsyncBus;
46
use OC\Diagnostics\EventLogger;
47
use OC\Diagnostics\NullEventLogger;
48
use OC\Diagnostics\NullQueryLogger;
49
use OC\Diagnostics\QueryLogger;
50
use OC\Files\Node\HookConnector;
51
use OC\Files\Node\Root;
52
use OC\Files\View;
53
use OC\Http\Client\ClientService;
54
use OC\Lock\DBLockingProvider;
55
use OC\Lock\MemcacheLockingProvider;
56
use OC\Lock\NoopLockingProvider;
57
use OC\Mail\Mailer;
58
use OC\Memcache\ArrayCache;
59
use OC\Notification\Manager;
60
use OC\Security\CertificateManager;
61
use OC\Security\Crypto;
62
use OC\Security\Hasher;
63
use OC\Security\SecureRandom;
64
use OC\Security\TrustedDomainHelper;
65
use OC\Session\CryptoWrapper;
66
use OC\Tagging\TagMapper;
67
use OCP\IServerContainer;
68
use Symfony\Component\EventDispatcher\EventDispatcher;
69
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
70
71
/**
72
 * Class Server
73
 *
74
 * @package OC
75
 *
76
 * TODO: hookup all manager classes
77
 */
78
class Server extends SimpleContainer implements IServerContainer {
79
	/** @var string */
80
	private $webRoot;
81
82
	/**
83
	 * @param string $webRoot
84 622
	 */
85 93
	public function __construct($webRoot) {
86 93
		parent::__construct();
87
		$this->webRoot = $webRoot;
88
89 2
		$this->registerService('ContactsManager', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90 93
			return new ContactsManager();
91
		});
92
93 3
		$this->registerService('PreviewManager', function (Server $c) {
94 93
			return new PreviewManager($c->getConfig());
95
		});
96
97 2
		$this->registerService('EncryptionManager', function (Server $c) {
98 2
			$view = new View();
99 2
			$util = new Encryption\Util(
100 2
				$view,
101 2
				$c->getUserManager(),
102 2
				$c->getGroupManager(),
103 2
				$c->getConfig()
104 2
			);
105 2
			return new Encryption\Manager(
106 2
				$c->getConfig(),
107 2
				$c->getLogger(),
108 2
				$c->getL10N('core'),
109
				new View(),
110 2
				$util,
111 93
				new ArrayCache()
112
			);
113
		});
114 2
115 2
		$this->registerService('EncryptionFileHelper', function (Server $c) {
116 2
			$util = new Encryption\Util(
117 2
				new View(),
118 2
				$c->getUserManager(),
119 2
				$c->getGroupManager(),
120 2
				$c->getConfig()
121 93
			);
122
			return new Encryption\File($util);
123
		});
124 2
125 2
		$this->registerService('EncryptionKeyStorage', function (Server $c) {
126 2
			$view = new View();
127 2
			$util = new Encryption\Util(
128 2
				$view,
129 2
				$c->getUserManager(),
130 2
				$c->getGroupManager(),
131
				$c->getConfig()
132 2
			);
133 93
134
			return new Encryption\Keys\Storage($view, $util);
135 5
		});
136 93
		$this->registerService('TagMapper', function(Server $c) {
137
			return new TagMapper($c->getDatabaseConnection());
138 3
		});
139 3
		$this->registerService('TagManager', function (Server $c) {
140 93
			$tagMapper = $c->query('TagMapper');
141
			return new TagManager($tagMapper, $c->getUserSession());
142
		});
143 5
		$this->registerService('RootFolder', function (Server $c) {
144
			// TODO: get user and user manager from container as well
145 5
			$user = \OC_User::getUser();
146 5
			/** @var $c SimpleContainer */
147 5
			$userManager = $c->query('UserManager');
148 5
			$user = $userManager->get($user);
149 5
			$manager = \OC\Files\Filesystem::getMountManager();
150 5
			$view = new View();
151 5
			$root = new Root($manager, $view, $user);
152 5
			$connector = new HookConnector($root, $view);
153 93
			$connector->viewToNode();
154
			return $root;
155 28
		});
156 28
		$this->registerService('UserManager', function (Server $c) {
157 93
			$config = $c->getConfig();
158
			return new \OC\User\Manager($config);
159 10
		});
160
		$this->registerService('GroupManager', function (Server $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
161 406
			$groupManager = new \OC\Group\Manager($this->getUserManager());
162 416
			$groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
163
				\OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid));
164 406
			});
165 416
			$groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $gid) {
166
				\OC_Hook::emit('OC_User', 'post_createGroup', array('gid' => $gid->getGID()));
167 395
			});
168 405
			$groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) {
169
				\OC_Hook::emit('OC_Group', 'pre_deleteGroup', array('run' => true, 'gid' => $group->getGID()));
170 395
			});
171 405
			$groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) {
172
				\OC_Hook::emit('OC_User', 'post_deleteGroup', array('gid' => $group->getGID()));
173 417
			});
174 427
			$groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
175
				\OC_Hook::emit('OC_Group', 'pre_addToGroup', array('run' => true, 'uid' => $user->getUID(), 'gid' => $group->getGID()));
176 417
			});
177
			$groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
178 417
				\OC_Hook::emit('OC_Group', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
179 427
				//Minimal fix to keep it backward compatible TODO: clean up all the GroupManager hooks
180 10
				\OC_Hook::emit('OC_User', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
181 93
			});
182
			return $groupManager;
183 14
		});
184
		$this->registerService('UserSession', function (Server $c) {
185 14
			$manager = $c->getUserManager();
186
187 14
			$session = new \OC\Session\Memory('');
188
189 608
			$userSession = new \OC\User\Session($manager, $session);
190 622
			$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
191
				\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
192
			});
193 608
			$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
194 622
				/** @var $user \OC\User\User */
195
				\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
196
			});
197 580
			$userSession->listen('\OC\User', 'preDelete', function ($user) {
198 594
				/** @var $user \OC\User\User */
199
				\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
200
			});
201 580
			$userSession->listen('\OC\User', 'postDelete', function ($user) {
202 594
				/** @var $user \OC\User\User */
203
				\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
204
			});
205 2 View Code Duplication
			$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
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...
206 16
				/** @var $user \OC\User\User */
207
				\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
208
			});
209 2 View Code Duplication
			$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
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...
210 16
				/** @var $user \OC\User\User */
211
				\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
212 263
			});
213 277
			$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
214
				\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
215
			});
216 263
			$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
217 277
				/** @var $user \OC\User\User */
218
				\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
219
			});
220 14
			$userSession->listen('\OC\User', 'logout', function () {
221 14
				\OC_Hook::emit('OC_User', 'logout', array());
222 93
			});
223
			return $userSession;
224 2
		});
225 93
		$this->registerService('NavigationManager', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
226
			return new \OC\NavigationManager();
227 59
		});
228 59
		$this->registerService('AllConfig', function (Server $c) {
229 59
			return new \OC\AllConfig(
230 93
				$c->getSystemConfig()
231
			);
232 68
		});
233 93
		$this->registerService('SystemConfig', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
			return new \OC\SystemConfig();
235 4
		});
236 93
		$this->registerService('AppConfig', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
237
			return new \OC\AppConfig(\OC_DB::getConnection());
238 6
		});
239 93
		$this->registerService('L10NFactory', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
240
			return new \OC\L10N\Factory();
241 2
		});
242 2
		$this->registerService('URLGenerator', function (Server $c) {
243 2
			$config = $c->getConfig();
244 2
			$cacheFactory = $c->getMemCacheFactory();
245
			return new \OC\URLGenerator(
246 2
				$config,
247 93
				$cacheFactory
248
			);
249 2
		});
250 93
		$this->registerService('AppHelper', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
251
			return new \OC\AppHelper();
0 ignored issues
show
Deprecated Code introduced by
The class OC\AppHelper has been deprecated with message: 8.1.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
252 2
		});
253 93
		$this->registerService('UserCache', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
254
			return new Cache\File();
255 8
		});
256
		$this->registerService('MemCacheFactory', function (Server $c) {
257 8
			$config = $c->getConfig();
258
259
			if($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
260
				$v = \OC_App::getAppVersions();
261
				$v['core'] = md5(file_get_contents(\OC::$SERVERROOT . '/version.php'));
262
				$version = implode(',', $v);
263
				$instanceId = \OC_Util::getInstanceId();
264
				$path = \OC::$SERVERROOT;
265
				$prefix = md5($instanceId.'-'.$version.'-'.$path);
266
				return new \OC\Memcache\Factory($prefix, $c->getLogger(),
267
					$config->getSystemValue('memcache.local', null),
268
					$config->getSystemValue('memcache.distributed', null),
269
					$config->getSystemValue('memcache.locking', null)
270
				);
271 8
			}
272 8
273 8
			return new \OC\Memcache\Factory('', $c->getLogger(),
274
				'\\OC\\Memcache\\ArrayCache',
275 8
				'\\OC\\Memcache\\ArrayCache',
276 93
				'\\OC\\Memcache\\ArrayCache'
277
			);
278 2
		});
279 2
		$this->registerService('ActivityManager', function (Server $c) {
280 2
			return new ActivityManager(
281 2
				$c->getRequest(),
282 2
				$c->getUserSession(),
283 93
				$c->getConfig()
284
			);
285 3
		});
286 93
		$this->registerService('AvatarManager', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
287
			return new AvatarManager();
288 17
		});
289 17
		$this->registerService('Logger', function (Server $c) {
290 17
			$logClass = $c->query('AllConfig')->getSystemValue('log_type', 'owncloud');
291
			$logger = 'OC_Log_' . ucfirst($logClass);
292 17
			call_user_func(array($logger, 'init'));
293 93
294
			return new Log($logger);
295 5
		});
296 5
		$this->registerService('JobList', function (Server $c) {
297 93
			$config = $c->getConfig();
298
			return new \OC\BackgroundJob\JobList($c->getDatabaseConnection(), $config);
299 2
		});
300 2
		$this->registerService('Router', function (Server $c) {
301 2
			$cacheFactory = $c->getMemCacheFactory();
302 2
			$logger = $c->getLogger();
303 2
			if ($cacheFactory->isAvailable()) {
304
				$router = new \OC\Route\CachingRouter($cacheFactory->create('route'), $logger);
305
			} else {
306 2
				$router = new \OC\Route\Router($logger);
307 93
			}
308
			return $router;
309 2
		});
310 93
		$this->registerService('Search', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
311
			return new Search();
312 9
		});
313 93
		$this->registerService('SecureRandom', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
314
			return new SecureRandom();
315 3
		});
316 93
		$this->registerService('Crypto', function (Server $c) {
317
			return new Crypto($c->getConfig(), $c->getSecureRandom());
318 3
		});
319 93
		$this->registerService('Hasher', function (Server $c) {
320
			return new Hasher($c->getConfig());
321 12
		});
322 12
		$this->registerService('DatabaseConnection', function (Server $c) {
323 12
			$factory = new \OC\DB\ConnectionFactory();
324 12
			$systemConfig = $c->getSystemConfig();
325
			$type = $systemConfig->getValue('dbtype', 'sqlite');
326
			if (!$factory->isValidType($type)) {
327 12
				throw new \OC\DatabaseException('Invalid database type');
328 12
			}
329 12
			$connectionParams = $factory->createConnectionParams($systemConfig);
330 12
			$connection = $factory->getConnection($type, $connectionParams);
331 93
			$connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
332
			return $connection;
333 2
		});
334 93
		$this->registerService('Db', function (Server $c) {
335
			return new Db($c->getDatabaseConnection());
0 ignored issues
show
Deprecated Code introduced by
The class OC\AppFramework\Db\Db has been deprecated with message: use IDBConnection directly, will be removed in ownCloud 10
Small Facade for being able to inject the database connection for tests

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
336 1
		});
337 1
		$this->registerService('HTTPHelper', function (Server $c) {
338 1
			$config = $c->getConfig();
339 1
			return new HTTPHelper(
0 ignored issues
show
Deprecated Code introduced by
The class OC\HTTPHelper has been deprecated with message: Use \OCP\Http\Client\IClientService

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
340 1
				$config,
341 93
				$c->getHTTPClientService()
342
			);
343 4
		});
344 4
		$this->registerService('HttpClientService', function (Server $c) {
345 4
			$user = \OC_User::getUser();
346 4
			$uid = $user ? $user : null;
347 4
			return new ClientService(
348 4
				$c->getConfig(),
349 93
				new \OC\Security\CertificateManager($uid, new View(), $c->getConfig())
350
			);
351 1
		});
352
		$this->registerService('EventLogger', function (Server $c) {
353
			if ($c->getSystemConfig()->getValue('debug', false)) {
354 1
				return new EventLogger();
355
			} else {
356 93
				return new NullEventLogger();
357
			}
358 13
		});
359
		$this->registerService('QueryLogger', function (Server $c) {
360
			if ($c->getSystemConfig()->getValue('debug', false)) {
361 13
				return new QueryLogger();
362
			} else {
363 93
				return new NullQueryLogger();
364
			}
365 2
		});
366 2
		$this->registerService('TempManager', function (Server $c) {
367 2
			return new TempManager(
368 2
				$c->getLogger(),
369 93
				$c->getConfig()
370
			);
371 2
		});
372 2
		$this->registerService('AppManager', function(Server $c) {
373 2
			return new \OC\App\AppManager(
374 2
				$c->getUserSession(),
375 2
				$c->getAppConfig(),
376 2
				$c->getGroupManager(),
377 93
				$c->getMemCacheFactory()
378
			);
379 5
		});
380 5
		$this->registerService('DateTimeZone', function(Server $c) {
381 5
			return new DateTimeZone(
382 5
				$c->getConfig(),
383 93
				$c->getSession()
384
			);
385 3
		});
386
		$this->registerService('DateTimeFormatter', function(Server $c) {
387 3
			$language = $c->getConfig()->getUserValue($c->getSession()->get('user_id'), 'core', 'lang', null);
388 3
389 3
			return new DateTimeFormatter(
390 3
				$c->getDateTimeZone()->getTimeZone(),
391 93
				$c->getL10N('lib', $language)
392
			);
393 2
		});
394 2
		$this->registerService('MountConfigManager', function () {
395 93
			$loader = \OC\Files\Filesystem::getLoader();
396
			return new \OC\Files\Config\MountProviderCollection($loader);
397 1
		});
398 93
		$this->registerService('IniWrapper', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
399
			return new IniGetWrapper();
400 2
		});
401 2
		$this->registerService('AsyncCommandBus', function (Server $c) {
402 93
			$jobList = $c->getJobList();
403
			return new AsyncBus($jobList);
404 1
		});
405 93
		$this->registerService('TrustedDomainHelper', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
406
			return new TrustedDomainHelper($this->getConfig());
407 4
		});
408
		$this->registerService('Request', function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
409
			if (isset($this['urlParams'])) {
410 4
				$urlParams = $this['urlParams'];
411
			} else {
412
				$urlParams = [];
413 4
			}
414
415
			if ($this->getSession()->exists('requesttoken')) {
416 4
				$requestToken = $this->getSession()->get('requesttoken');
417
			} else {
418
				$requestToken = false;
419 4
			}
420 4
421 4
			if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
422
				&& in_array('fakeinput', stream_get_wrappers())
423
			) {
424 4
				$stream = 'fakeinput://data';
425
			} else {
426
				$stream = 'php://input';
427 4
			}
428
429 4
			return new Request(
430 4
				[
431 4
					'get' => $_GET,
432 4
					'post' => $_POST,
433 4
					'files' => $_FILES,
434 4
					'server' => $_SERVER,
435 4
					'env' => $_ENV,
436 4
					'cookies' => $_COOKIE,
437 4
					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
438 4
						? $_SERVER['REQUEST_METHOD']
439 4
						: null,
440 4
					'urlParams' => $urlParams,
441 4
					'requesttoken' => $requestToken,
442 4
				],
443
				$this->getSecureRandom(),
444 4
				$this->getConfig(),
445 93
				$stream
446
			);
447 2
		});
448 2
		$this->registerService('Mailer', function(Server $c) {
449 2
			return new Mailer(
450 2
				$c->getConfig(),
451 2
				$c->getLogger(),
452 93
				new \OC_Defaults()
453
			);
454 1
		});
455 1
		$this->registerService('OcsClient', function(Server $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
456 1
			return new OCSClient(
457 1
				$this->getHTTPClientService(),
458 1
				$this->getConfig(),
459 93
				$this->getLogger()
460
			);
461 1
		});
462
		$this->registerService('LockingProvider', function (Server $c) {
463 1
			if ($c->getConfig()->getSystemValue('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
464 1
				/** @var \OC\Memcache\Factory $memcacheFactory */
465 1
				$memcacheFactory = $c->getMemCacheFactory();
466 1
				$memcache = $memcacheFactory->createLocking('lock');
467
				if (!($memcache instanceof \OC\Memcache\NullCache)) {
468
					return new MemcacheLockingProvider($memcache);
469
				}
470
				return new DBLockingProvider($c->getDatabaseConnection(), $c->getLogger(), new TimeFactory());
471 93
			}
472
			return new NoopLockingProvider();
473 1
		});
474 93
		$this->registerService('MountManager', function () {
475
			return new \OC\Files\Mount\Manager();
476 1
		});
477 1
		$this->registerService('MimeTypeDetector', function(Server $c) {
478 1
			return new \OC\Files\Type\Detection(
479
				$c->getURLGenerator(),
480 1
				\OC::$SERVERROOT . '/config/',
481 93
				\OC::$SERVERROOT . '/resources/config/'
482
				);
483 1
		});
484 1
		$this->registerService('MimeTypeLoader', function(Server $c) {
485 1
			return new \OC\Files\Type\Loader(
486 93
				$c->getDatabaseConnection()
487
			);
488 2
		});
489 93
		$this->registerService('NotificationManager', function() {
490
			return new Manager();
491 1
		});
492
		$this->registerService('CapabilitiesManager', function (Server $c) {
493
			$manager = new \OC\CapabilitiesManager();
494 1
			$manager->registerCapability(function() use ($c) {
495 1
				return new \OC\OCS\CoreCapabilities($c->getConfig());
496 93
			});
497
			return $manager;
498
		});
499 93
		$this->registerService('EventDispatcher', function() {
500 93
			return new EventDispatcher();
501
		});
502 1
		$this->registerService('CryptoWrapper', function (Server $c) {
503
			// FIXME: Instantiiated here due to cyclic dependency
504 1
			$request = new Request(
505 1
				[
506 1
					'get' => $_GET,
507 1
					'post' => $_POST,
508 1
					'files' => $_FILES,
509 1
					'server' => $_SERVER,
510 1
					'env' => $_ENV,
511 1
					'cookies' => $_COOKIE,
512 1
					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
513 1
						? $_SERVER['REQUEST_METHOD']
514 1
						: null,
515 1
				],
516 1
				new SecureRandom(),
517
				$c->getConfig()
518 1
			);
519 1
520 1
			return new CryptoWrapper(
521 1
				$c->getConfig(),
522
				$c->getCrypto(),
523 1
				$c->getSecureRandom(),
524 93
				$request
525 93
			);
526
		});
527
	}
528
529
	/**
530 14
	 * @return \OCP\Contacts\IManager
531 14
	 */
532
	public function getContactsManager() {
533
		return $this->query('ContactsManager');
534
	}
535
536
	/**
537 8
	 * @return \OC\Encryption\Manager
538 8
	 */
539
	public function getEncryptionManager() {
540
		return $this->query('EncryptionManager');
541
	}
542
543
	/**
544 7
	 * @return \OC\Encryption\File
545 7
	 */
546
	public function getEncryptionFilesHelper() {
547
		return $this->query('EncryptionFileHelper');
548
	}
549
550
	/**
551 7
	 * @return \OCP\Encryption\Keys\IStorage
552 7
	 */
553
	public function getEncryptionKeyStorage() {
554
		return $this->query('EncryptionKeyStorage');
555
	}
556
557
	/**
558
	 * The current request object holding all information about the request
559
	 * currently being processed is returned from this method.
560
	 * In case the current execution was not initiated by a web request null is returned
561
	 *
562 251
	 * @return \OCP\IRequest
563 251
	 */
564
	public function getRequest() {
565
		return $this->query('Request');
566
	}
567
568
	/**
569
	 * Returns the preview manager which can create preview images for a given file
570
	 *
571 176
	 * @return \OCP\IPreview
572 176
	 */
573
	public function getPreviewManager() {
574
		return $this->query('PreviewManager');
575
	}
576
577
	/**
578
	 * Returns the tag manager which can get and set tags for different object types
579
	 *
580
	 * @see \OCP\ITagManager::load()
581 22
	 * @return \OCP\ITagManager
582 22
	 */
583
	public function getTagManager() {
584
		return $this->query('TagManager');
585
	}
586
587
	/**
588
	 * Returns the avatar manager, used for avatar functionality
589
	 *
590 1
	 * @return \OCP\IAvatarManager
591 1
	 */
592
	public function getAvatarManager() {
593
		return $this->query('AvatarManager');
594
	}
595
596
	/**
597
	 * Returns the root folder of ownCloud's data directory
598
	 *
599 885
	 * @return \OCP\Files\IRootFolder
600 885
	 */
601
	public function getRootFolder() {
602
		return $this->query('RootFolder');
603
	}
604
605
	/**
606
	 * Returns a view to ownCloud's files folder
607
	 *
608
	 * @param string $userId user ID
609 885
	 * @return \OCP\Files\Folder
610 885
	 */
611 12
	public function getUserFolder($userId = null) {
612 12
		if ($userId === null) {
613
			$user = $this->getUserSession()->getUser();
614
			if (!$user) {
615 12
				return null;
616 12
			}
617 885
			$userId = $user->getUID();
618 885
		}
619
		$root = $this->getRootFolder();
620
		return $root->getUserFolder($userId);
621
	}
622
623
	/**
624
	 * Returns an app-specific view in ownClouds data directory
625
	 *
626
	 * @return \OCP\Files\Folder
627
	 */
628
	public function getAppFolder() {
629
		$dir = '/' . \OC_App::getCurrentApp();
630
		$root = $this->getRootFolder();
631
		$folder = null;
0 ignored issues
show
Unused Code introduced by
$folder is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
632
		if (!$root->nodeExists($dir)) {
633
			$folder = $root->newFolder($dir);
634
		} else {
635
			$folder = $root->get($dir);
636
		}
637
		return $folder;
638
	}
639
640
	/**
641 1357
	 * @return \OC\User\Manager
642 1357
	 */
643
	public function getUserManager() {
644
		return $this->query('UserManager');
645
	}
646
647
	/**
648 972
	 * @return \OC\Group\Manager
649 972
	 */
650
	public function getGroupManager() {
651
		return $this->query('GroupManager');
652
	}
653
654
	/**
655 1259
	 * @return \OC\User\Session
656 1259
	 */
657
	public function getUserSession() {
658
		return $this->query('UserSession');
659
	}
660
661
	/**
662 1263
	 * @return \OCP\ISession
663 1263
	 */
664
	public function getSession() {
665
		return $this->query('UserSession')->getSession();
666
	}
667
668
	/**
669
	 * @param \OCP\ISession $session
670
	 */
671
	public function setSession(\OCP\ISession $session) {
672
		return $this->query('UserSession')->setSession($session);
673
	}
674
675
	/**
676 1
	 * @return \OC\NavigationManager
677 1
	 */
678
	public function getNavigationManager() {
679
		return $this->query('NavigationManager');
680
	}
681
682
	/**
683 1477
	 * @return \OCP\IConfig
684 1477
	 */
685
	public function getConfig() {
686
		return $this->query('AllConfig');
687
	}
688
689
	/**
690
	 * For internal use only
691
	 *
692 328
	 * @return \OC\SystemConfig
693 328
	 */
694
	public function getSystemConfig() {
695
		return $this->query('SystemConfig');
696
	}
697
698
	/**
699
	 * Returns the app config manager
700
	 *
701 1152
	 * @return \OCP\IAppConfig
702 1152
	 */
703
	public function getAppConfig() {
704
		return $this->query('AppConfig');
705
	}
706
707
	/**
708 1014
	 * @return \OCP\L10N\IFactory
709 1014
	 */
710
	public function getL10NFactory() {
711
		return $this->query('L10NFactory');
712
	}
713
714
	/**
715
	 * get an L10N instance
716
	 *
717
	 * @param string $app appid
718
	 * @param string $lang
719 1013
	 * @return \OC_L10N
720 1013
	 */
721
	public function getL10N($app, $lang = null) {
722
		return $this->getL10NFactory()->get($app, $lang);
723
	}
724
725
	/**
726 83
	 * @return \OCP\IURLGenerator
727 83
	 */
728
	public function getURLGenerator() {
729
		return $this->query('URLGenerator');
730
	}
731
732
	/**
733
	 * @return \OCP\IHelper
734
	 */
735
	public function getHelper() {
736
		return $this->query('AppHelper');
737
	}
738
739
	/**
740
	 * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
741
	 * getMemCacheFactory() instead.
742
	 *
743
	 * @return \OCP\ICache
744
	 * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
745
	 */
746
	public function getCache() {
747
		return $this->query('UserCache');
748
	}
749
750
	/**
751
	 * Returns an \OCP\CacheFactory instance
752
	 *
753 113
	 * @return \OCP\ICacheFactory
754 113
	 */
755
	public function getMemCacheFactory() {
756
		return $this->query('MemCacheFactory');
757
	}
758
759
	/**
760
	 * Returns the current session
761
	 *
762 1836
	 * @return \OCP\IDBConnection
763 1836
	 */
764
	public function getDatabaseConnection() {
765
		return $this->query('DatabaseConnection');
766
	}
767
768
	/**
769
	 * Returns the activity manager
770
	 *
771 9
	 * @return \OCP\Activity\IManager
772 9
	 */
773
	public function getActivityManager() {
774
		return $this->query('ActivityManager');
775
	}
776
777
	/**
778
	 * Returns an job list for controlling background jobs
779
	 *
780 3
	 * @return \OCP\BackgroundJob\IJobList
781 3
	 */
782
	public function getJobList() {
783
		return $this->query('JobList');
784
	}
785
786
	/**
787
	 * Returns a logger instance
788
	 *
789 968
	 * @return \OCP\ILogger
790 968
	 */
791
	public function getLogger() {
792
		return $this->query('Logger');
793
	}
794
795
	/**
796
	 * Returns a router for generating and matching urls
797
	 *
798 10
	 * @return \OCP\Route\IRouter
799 10
	 */
800
	public function getRouter() {
801
		return $this->query('Router');
802
	}
803
804
	/**
805
	 * Returns a search instance
806
	 *
807
	 * @return \OCP\ISearch
808
	 */
809
	public function getSearch() {
810
		return $this->query('Search');
811
	}
812
813
	/**
814
	 * Returns a SecureRandom instance
815
	 *
816 584
	 * @return \OCP\Security\ISecureRandom
817 584
	 */
818
	public function getSecureRandom() {
819
		return $this->query('SecureRandom');
820
	}
821
822
	/**
823
	 * Returns a Crypto instance
824
	 *
825 1
	 * @return \OCP\Security\ICrypto
826 1
	 */
827
	public function getCrypto() {
828
		return $this->query('Crypto');
829
	}
830
831
	/**
832
	 * Returns a Hasher instance
833
	 *
834 107
	 * @return \OCP\Security\IHasher
835 107
	 */
836
	public function getHasher() {
837
		return $this->query('Hasher');
838
	}
839
840
	/**
841
	 * Returns an instance of the db facade
842
	 * @deprecated use getDatabaseConnection, will be removed in ownCloud 10
843
	 * @return \OCP\IDb
844
	 */
845
	public function getDb() {
846
		return $this->query('Db');
847
	}
848
849
	/**
850
	 * Returns an instance of the HTTP helper class
851
	 * @deprecated Use getHTTPClientService()
852 22
	 * @return \OC\HTTPHelper
853 22
	 */
854
	public function getHTTPHelper() {
855
		return $this->query('HTTPHelper');
856
	}
857
858
	/**
859
	 * Get the certificate manager for the user
860
	 *
861
	 * @param string $userId (optional) if not specified the current loggedin user is used
862 8
	 * @return \OCP\ICertificateManager | null if $uid is null and no user is logged in
863 8
	 */
864 6
	public function getCertificateManager($userId = null) {
865 6
		if (is_null($userId)) {
866 6
			$userSession = $this->getUserSession();
867
			$user = $userSession->getUser();
868
			if (is_null($user)) {
869 6
				return null;
870 6
			}
871 8
			$userId = $user->getUID();
872
		}
873
		return new CertificateManager($userId, new View(), $this->getConfig());
874
	}
875
876
	/**
877
	 * Returns an instance of the HTTP client service
878
	 *
879 13
	 * @return \OCP\Http\Client\IClientService
880 13
	 */
881
	public function getHTTPClientService() {
882
		return $this->query('HttpClientService');
883
	}
884
885
	/**
886
	 * Create a new event source
887
	 *
888 1
	 * @return \OCP\IEventSource
889 1
	 */
890
	public function createEventSource() {
891
		return new \OC_EventSource();
892
	}
893
894
	/**
895
	 * Get the active event logger
896
	 *
897
	 * The returned logger only logs data when debug mode is enabled
898
	 *
899 1056
	 * @return \OCP\Diagnostics\IEventLogger
900 1056
	 */
901
	public function getEventLogger() {
902
		return $this->query('EventLogger');
903
	}
904
905
	/**
906
	 * Get the active query logger
907
	 *
908
	 * The returned logger only logs data when debug mode is enabled
909
	 *
910 12
	 * @return \OCP\Diagnostics\IQueryLogger
911 12
	 */
912
	public function getQueryLogger() {
913
		return $this->query('QueryLogger');
914
	}
915
916
	/**
917
	 * Get the manager for temporary files and folders
918
	 *
919 1478
	 * @return \OCP\ITempManager
920 1478
	 */
921
	public function getTempManager() {
922
		return $this->query('TempManager');
923
	}
924
925
	/**
926
	 * Get the app manager
927
	 *
928 1115
	 * @return \OCP\App\IAppManager
929 1115
	 */
930
	public function getAppManager() {
931
		return $this->query('AppManager');
932
	}
933
934
	/**
935
	 * Creates a new mailer
936
	 *
937 22
	 * @return \OCP\Mail\IMailer
938 22
	 */
939
	public function getMailer() {
940
		return $this->query('Mailer');
941
	}
942
943
	/**
944
	 * Get the webroot
945
	 *
946
	 * @return string
947
	 */
948
	public function getWebRoot() {
949
		return $this->webRoot;
950
	}
951
952
	/**
953
	 * @return \OC\OCSClient
954
	 */
955
	public function getOcsClient() {
956
		return $this->query('OcsClient');
957
	}
958
959
	/**
960 9
	 * @return \OCP\IDateTimeZone
961 9
	 */
962
	public function getDateTimeZone() {
963
		return $this->query('DateTimeZone');
964
	}
965
966
	/**
967
	 * @return \OCP\IDateTimeFormatter
968
	 */
969
	public function getDateTimeFormatter() {
970
		return $this->query('DateTimeFormatter');
971
	}
972
973
	/**
974 983
	 * @return \OCP\Files\Config\IMountProviderCollection
975 983
	 */
976
	public function getMountProviderCollection(){
977
		return $this->query('MountConfigManager');
978
	}
979
980
	/**
981
	 * Get the IniWrapper
982
	 *
983 9
	 * @return IniGetWrapper
984 9
	 */
985
	public function getIniWrapper() {
986
		return $this->query('IniWrapper');
987
	}
988
989
	/**
990 64
	 * @return \OCP\Command\IBus
991 64
	 */
992
	public function getCommandBus(){
993
		return $this->query('AsyncCommandBus');
994
	}
995
996
	/**
997
	 * Get the trusted domain helper
998
	 *
999
	 * @return TrustedDomainHelper
1000
	 */
1001
	public function getTrustedDomainHelper() {
1002
		return $this->query('TrustedDomainHelper');
1003
	}
1004
1005
	/**
1006
	 * Get the locking provider
1007
	 *
1008
	 * @return \OCP\Lock\ILockingProvider
1009 5681
	 * @since 8.1.0
1010 5681
	 */
1011
	public function getLockingProvider() {
1012
		return $this->query('LockingProvider');
1013
	}
1014
1015
	/**
1016 15
	 * @return \OCP\Files\Mount\IMountManager
1017 15
	 **/
1018
	function getMountManager() {
1019
		return $this->query('MountManager');
1020
	}
1021
1022
	/*
1023
	 * Get the MimeTypeDetector
1024
	 *
1025 866
	 * @return \OCP\Files\IMimeTypeDetector
1026 866
	 */
1027
	public function getMimeTypeDetector() {
1028
		return $this->query('MimeTypeDetector');
1029
	}
1030
1031
	/**
1032
	 * Get the MimeTypeLoader
1033
	 *
1034 1094
	 * @return \OCP\Files\IMimeTypeLoader
1035 1094
	 */
1036
	public function getMimeTypeLoader() {
1037
		return $this->query('MimeTypeLoader');
1038
	}
1039
1040
	/**
1041
	 * Get the manager of all the capabilities
1042
	 *
1043 81
	 * @return \OC\CapabilitiesManager
1044 81
	 */
1045
	public function getCapabilitiesManager() {
1046
		return $this->query('CapabilitiesManager');
1047
	}
1048
1049
	/**
1050
	 * Get the EventDispatcher
1051
	 *
1052
	 * @return EventDispatcherInterface
1053 38
	 * @since 8.2.0
1054 38
	 */
1055
	public function getEventDispatcher() {
1056
		return $this->query('EventDispatcher');
1057
	}
1058
1059
	/**
1060
	 * Get the Notification Manager
1061
	 *
1062
	 * @return \OC\Notification\IManager
1063 6
	 * @since 8.2.0
1064 6
	 */
1065
	public function getNotificationManager() {
1066
		return $this->query('NotificationManager');
1067
	}
1068
1069
	/**
1070
	 * @return \OC\Session\CryptoWrapper
1071
	 */
1072
	public function getSessionCryptoWrapper() {
1073
		return $this->query('CryptoWrapper');
1074
	}
1075
1076
	/**
1077
	 * Not a public API as of 8.2, wait for 9.0
1078
	 * @return \OCA\Files_External\Service\BackendService
1079
	 */
1080
	public function getStoragesBackendService() {
1081
		return \OC_Mount_Config::$app->getContainer()->query('OCA\\Files_External\\Service\\BackendService');
1082
	}
1083
1084
	/**
1085
	 * Not a public API as of 8.2, wait for 9.0
1086
	 * @return \OCA\Files_External\Service\GlobalStoragesService
1087
	 */
1088
	public function getGlobalStoragesService() {
1089
		return \OC_Mount_Config::$app->getContainer()->query('OCA\\Files_External\\Service\\GlobalStoragesService');
1090
	}
1091
1092
	/**
1093
	 * Not a public API as of 8.2, wait for 9.0
1094
	 * @return \OCA\Files_External\Service\UserGlobalStoragesService
1095
	 */
1096
	public function getUserGlobalStoragesService() {
1097
		return \OC_Mount_Config::$app->getContainer()->query('OCA\\Files_External\\Service\\UserGlobalStoragesService');
1098
	}
1099
1100
	/**
1101
	 * Not a public API as of 8.2, wait for 9.0
1102
	 * @return \OCA\Files_External\Service\UserStoragesService
1103
	 */
1104
	public function getUserStoragesService() {
1105
		return \OC_Mount_Config::$app->getContainer()->query('OCA\\Files_External\\Service\\UserStoragesService');
1106
	}
1107
	
1108
}
1109