Completed
Push — master ( a84fba...e8b4d4 )
by Thomas
11:12
created

Application::registerService()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 9
Ratio 21.43 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 0
dl 9
loc 42
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Robin Appelman <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2016, ownCloud GmbH.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Federation\AppInfo;
25
26
use OCA\Federation\API\OCSAuthAPI;
27
use OCA\Federation\Controller\SettingsController;
28
use OCA\Federation\DAV\FedAuth;
29
use OCA\Federation\DbHandler;
30
use OCA\Federation\Hooks;
31
use OCA\Federation\Middleware\AddServerMiddleware;
32
use OCA\Federation\SyncFederationAddressBooks;
33
use OCA\Federation\SyncJob;
34
use OCA\Federation\TrustedServers;
35
use OCP\API;
36
use OCP\App;
37
use OCP\AppFramework\IAppContainer;
38
use OCP\SabrePluginEvent;
39
use OCP\Util;
40
use Sabre\DAV\Auth\Plugin;
41
42
class Application extends \OCP\AppFramework\App {
43
44
	/**
45
	 * @param array $urlParams
46
	 */
47
	public function __construct($urlParams = []) {
48
		parent::__construct('federation', $urlParams);
49
		$this->registerService();
50
		$this->registerMiddleware();
51
	}
52
53
	/**
54
	 * register setting scripts
55
	 */
56
	public function registerSettings() {
57
		App::registerAdmin('federation', 'settings/settings-admin');
58
	}
59
60
	private function registerService() {
61
		$container = $this->getContainer();
62
63
		$container->registerService('addServerMiddleware', function(IAppContainer $c) {
64
			return new AddServerMiddleware(
65
				$c->getAppName(),
66
				\OC::$server->getL10N($c->getAppName()),
67
				\OC::$server->getLogger()
68
			);
69
		});
70
71
		$container->registerService('DbHandler', function(IAppContainer $c) {
72
			return new DbHandler(
73
				\OC::$server->getDatabaseConnection(),
74
				\OC::$server->getL10N($c->getAppName())
75
			);
76
		});
77
78
		$container->registerService('TrustedServers', function(IAppContainer $c) {
79
			$server = $c->getServer();
80
			return new TrustedServers(
81
				$c->query('DbHandler'),
82
				$server->getHTTPClientService(),
83
				$server->getLogger(),
84
				$server->getJobList(),
85
				$server->getSecureRandom(),
86
				$server->getConfig(),
87
				$server->getEventDispatcher()
88
			);
89
		});
90
91 View Code Duplication
		$container->registerService('SettingsController', 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...
92
			$server = $c->getServer();
93
			return new SettingsController(
94
				$c->getAppName(),
95
				$server->getRequest(),
96
				$server->getL10N($c->getAppName()),
97
				$c->query('TrustedServers')
98
			);
99
		});
100
101
	}
102
103
	private function registerMiddleware() {
104
		$container = $this->getContainer();
105
		$container->registerMiddleWare('addServerMiddleware');
106
	}
107
108
	/**
109
	 * listen to federated_share_added hooks to auto-add new servers to the
110
	 * list of trusted servers.
111
	 */
112
	public function registerHooks() {
113
114
		$container = $this->getContainer();
115
		$hooksManager = new Hooks($container->query('TrustedServers'));
116
117
		Util::connectHook(
118
				'OCP\Share',
119
				'federated_share_added',
120
				$hooksManager,
121
				'addServerHook'
122
		);
123
124
		$dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
125
		$dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) {
126
			if ($event instanceof SabrePluginEvent) {
127
				$authPlugin = $event->getServer()->getPlugin('auth');
128
				if ($authPlugin instanceof Plugin) {
129
					$h = new DbHandler($container->getServer()->getDatabaseConnection(),
130
							$container->getServer()->getL10N('federation')
131
					);
132
					$authPlugin->addBackend(new FedAuth($h));
133
				}
134
			}
135
		});
136
	}
137
138
	/**
139
	 * @return SyncFederationAddressBooks
140
	 */
141
	public function getSyncService() {
142
		$syncService = \OC::$server->query('CardDAVSyncService');
143
		$dbHandler = $this->getContainer()->query('DbHandler');
144
		return new SyncFederationAddressBooks($dbHandler, $syncService);
145
	}
146
147
}
148