Passed
Push — master ( 275f88...a4cf13 )
by Roeland
11:21 queued 14s
created

Application::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 31
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Georg Ehrke <[email protected]>
7
 * @author John Molakvoæ (skjnldsv) <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Victor Dubiniuk <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OCA\Files_Versions\AppInfo;
30
31
use OCA\DAV\CalDAV\Proxy\ProxyMapper;
32
use OCA\DAV\Connector\Sabre\Principal;
33
use OCA\Files\Event\LoadAdditionalScriptsEvent;
34
use OCA\Files\Event\LoadSidebar;
35
use OCA\Files_Versions\Capabilities;
36
use OCA\Files_Versions\Hooks;
37
use OCA\Files_Versions\Listener\LoadAdditionalListener;
38
use OCA\Files_Versions\Listener\LoadSidebarListener;
39
use OCA\Files_Versions\Versions\IVersionManager;
40
use OCA\Files_Versions\Versions\VersionManager;
41
use OCP\App\IAppManager;
42
use OCP\AppFramework\App;
43
use OCP\AppFramework\Bootstrap\IBootContext;
44
use OCP\AppFramework\Bootstrap\IBootstrap;
45
use OCP\AppFramework\Bootstrap\IRegistrationContext;
46
use OCP\AppFramework\IAppContainer;
47
use OCP\ILogger;
48
use OCP\IServerContainer;
49
50
class Application extends App implements IBootstrap {
51
	public const APP_ID = 'files_versions';
52
53
	public function __construct(array $urlParams = []) {
54
		parent::__construct(self::APP_ID, $urlParams);
55
	}
56
57
	public function register(IRegistrationContext $context): void {
58
		/**
59
		 * Register capabilities
60
		 */
61
		$context->registerCapability(Capabilities::class);
62
63
		/**
64
		 * Register $principalBackend for the DAV collection
65
		 */
66
		$context->registerService('principalBackend', function (IAppContainer $c) {
67
			$server = $c->getServer();
68
			return new Principal(
69
				$server->getUserManager(),
70
				$server->getGroupManager(),
71
				$server->getShareManager(),
72
				$server->getUserSession(),
73
				$server->getAppManager(),
74
				$server->query(ProxyMapper::class),
75
				$server->getConfig()
76
			);
77
		});
78
79
		$context->registerService(IVersionManager::class, function (IAppContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

79
		$context->registerService(IVersionManager::class, function (/** @scrutinizer ignore-unused */ IAppContainer $c) {

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

Loading history...
80
			return new VersionManager();
81
		});
82
83
		/**
84
		 * Register Events
85
		 */
86
		$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
87
		$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
88
	}
89
90
	public function boot(IBootContext $context): void {
91
		$context->injectFn(\Closure::fromCallable([$this, 'registerVersionBackends']));
92
93
		/**
94
		 * Register hooks
95
		 */
96
		Hooks::connectHooks();
97
	}
98
99
	public function registerVersionBackends(IServerContainer $server, IAppManager $appManager, ILogger $logger) {
100
		foreach ($appManager->getInstalledApps() as $app) {
101
			$appInfo = $appManager->getAppInfo($app);
102
			if (isset($appInfo['versions'])) {
103
				$backends = $appInfo['versions'];
104
				foreach ($backends as $backend) {
105
					if (isset($backend['@value'])) {
106
						$this->loadBackend($backend, $server, $logger);
107
					} else {
108
						foreach ($backend as $singleBackend) {
109
							$this->loadBackend($singleBackend, $server, $logger);
110
						}
111
					}
112
				}
113
			}
114
		}
115
	}
116
117
	private function loadBackend(array $backend, IServerContainer $server, ILogger $logger) {
118
		/** @var IVersionManager $versionManager */
119
		$versionManager = $server->query(IVersionManager::class);
120
		$class = $backend['@value'];
121
		$for = $backend['@attributes']['for'];
122
		try {
123
			$backendObject = $server->query($class);
124
			$versionManager->registerBackend($for, $backendObject);
125
		} catch (\Exception $e) {
126
			$logger->logException($e);
127
		}
128
	}
129
}
130