Passed
Push — master ( 613f0f...8f650f )
by Roeland
11:57 queued 12s
created

Application::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 68
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 37
nc 1
nop 1
dl 0
loc 68
rs 9.328
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author John Molakvoæ (skjnldsv) <[email protected]>
10
 * @author Julius Härtl <[email protected]>
11
 * @author Lukas Reschke <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Robin Appelman <[email protected]>
14
 * @author Roeland Jago Douma <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OCA\Files_Sharing\AppInfo;
34
35
use OC\AppFramework\Utility\SimpleContainer;
36
use OCA\Files_Sharing\Capabilities;
37
use OCA\Files_Sharing\External\Manager;
38
use OCA\Files_Sharing\Listener\LoadAdditionalListener;
39
use OCA\Files_Sharing\Listener\LoadSidebarListener;
40
use OCA\Files_Sharing\Listener\ShareInteractionListener;
41
use OCA\Files_Sharing\Listener\UserAddedToGroupListener;
42
use OCA\Files_Sharing\Listener\UserShareAcceptanceListener;
43
use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
44
use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
45
use OCA\Files_Sharing\Middleware\SharingCheckMiddleware;
46
use OCA\Files_Sharing\MountProvider;
47
use OCA\Files_Sharing\Notification\Listener;
48
use OCA\Files_Sharing\Notification\Notifier;
49
use OCA\Files\Event\LoadAdditionalScriptsEvent;
50
use OCA\Files\Event\LoadSidebar;
51
use OCP\AppFramework\App;
52
use OCP\EventDispatcher\IEventDispatcher;
53
use OCP\Files\Config\IMountProviderCollection;
54
use OCP\Group\Events\UserAddedEvent;
55
use OCP\IContainer;
56
use OCP\IGroup;
57
use OCP\IServerContainer;
58
use OCP\Share\Events\ShareCreatedEvent;
59
use OCP\Util;
60
use Symfony\Component\EventDispatcher\GenericEvent;
61
62
class Application extends App {
63
	public const APP_ID = 'files_sharing';
64
65
	public function __construct(array $urlParams = []) {
66
		parent::__construct(self::APP_ID, $urlParams);
67
68
		$container = $this->getContainer();
69
70
		/** @var IServerContainer $server */
71
		$server = $container->getServer();
72
73
		/** @var IEventDispatcher $dispatcher */
74
		$dispatcher = $container->query(IEventDispatcher::class);
75
		$mountProviderCollection = $server->getMountProviderCollection();
76
		$notifications = $server->getNotificationManager();
77
78
		/**
79
		 * Core class wrappers
80
		 */
81
		$container->registerService(Manager::class, function (SimpleContainer $c) use ($server) {
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

81
		$container->registerService(Manager::class, function (/** @scrutinizer ignore-unused */ SimpleContainer $c) use ($server) {

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...
82
			$user = $server->getUserSession()->getUser();
83
			$uid = $user ? $user->getUID() : null;
84
			return new \OCA\Files_Sharing\External\Manager(
85
				$server->getDatabaseConnection(),
86
				\OC\Files\Filesystem::getMountManager(),
87
				\OC\Files\Filesystem::getLoader(),
88
				$server->getHTTPClientService(),
89
				$server->getNotificationManager(),
90
				$server->query(\OCP\OCS\IDiscoveryService::class),
91
				$server->getCloudFederationProviderManager(),
92
				$server->getCloudFederationFactory(),
93
				$server->getGroupManager(),
94
				$server->getUserManager(),
95
				$uid
96
			);
97
		});
98
99
		/**
100
		 * Middleware
101
		 */
102
		$container->registerMiddleWare(SharingCheckMiddleware::class);
103
		$container->registerMiddleWare(OCSShareAPIMiddleware::class);
104
		$container->registerMiddleWare(ShareInfoMiddleware::class);
105
106
		$container->registerService('ExternalMountProvider', function (IContainer $c) {
107
			/** @var \OCP\IServerContainer $server */
108
			$server = $c->query('ServerContainer');
109
			return new \OCA\Files_Sharing\External\MountProvider(
110
				$server->getDatabaseConnection(),
111
				function () use ($c) {
112
					return $c->query(Manager::class);
113
				},
114
				$server->getCloudIdManager()
115
			);
116
		});
117
118
		/**
119
		 * Register capabilities
120
		 */
121
		$container->registerCapability(Capabilities::class);
122
123
		$notifications->registerNotifierService(Notifier::class);
124
125
		$this->registerMountProviders($mountProviderCollection);
126
		$this->registerEventsScripts($dispatcher);
127
		$this->setupSharingMenus();
128
129
		/**
130
		 * Always add main sharing script
131
		 */
132
		Util::addScript(self::APP_ID, 'dist/main');
133
	}
134
135
	protected function registerMountProviders(IMountProviderCollection $mountProviderCollection) {
136
		$mountProviderCollection->registerProvider($this->getContainer()->query(MountProvider::class));
137
		$mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider'));
138
	}
139
140
	protected function registerEventsScripts(IEventDispatcher $dispatcher) {
141
		// sidebar and files scripts
142
		$dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LoadAdditionalListener::class);
143
		$dispatcher->addServiceListener(LoadSidebar::class, LoadSidebarListener::class);
144
		$dispatcher->addServiceListener(ShareCreatedEvent::class, ShareInteractionListener::class);
145
		$dispatcher->addListener('\OCP\Collaboration\Resources::loadAdditionalScripts', function () {
146
			\OCP\Util::addScript('files_sharing', 'dist/collaboration');
147
		});
148
		$dispatcher->addServiceListener(ShareCreatedEvent::class, UserShareAcceptanceListener::class);
149
		$dispatcher->addServiceListener(UserAddedEvent::class, UserAddedToGroupListener::class);
150
151
		// notifications api to accept incoming user shares
152
		$dispatcher->addListener('OCP\Share::postShare', function (GenericEvent $event) {
153
			/** @var Listener $listener */
154
			$listener = $this->getContainer()->query(Listener::class);
155
			$listener->shareNotification($event);
156
		});
157
		$dispatcher->addListener(IGroup::class . '::postAddUser', function (GenericEvent $event) {
158
			/** @var Listener $listener */
159
			$listener = $this->getContainer()->query(Listener::class);
160
			$listener->userAddedToGroup($event);
161
		});
162
	}
163
164
	protected function setupSharingMenus() {
165
		$config = \OC::$server->getConfig();
166
		$l = \OC::$server->getL10N('files_sharing');
167
168
		if ($config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
169
			return;
170
		}
171
172
		$sharingSublistArray = [];
173
174
		if (\OCP\Util::isSharingDisabledForUser() === false) {
0 ignored issues
show
Deprecated Code introduced by
The function OCP\Util::isSharingDisabledForUser() has been deprecated: 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser ( Ignorable by Annotation )

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

174
		if (/** @scrutinizer ignore-deprecated */ \OCP\Util::isSharingDisabledForUser() === false) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

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

Loading history...
175
			array_push($sharingSublistArray, [
176
				'id' => 'sharingout',
177
				'appname' => 'files_sharing',
178
				'script' => 'list.php',
179
				'order' => 16,
180
				'name' => $l->t('Shared with others'),
181
			]);
182
		}
183
184
		array_push($sharingSublistArray, [
185
			'id' => 'sharingin',
186
			'appname' => 'files_sharing',
187
			'script' => 'list.php',
188
			'order' => 15,
189
			'name' => $l->t('Shared with you'),
190
		]);
191
192
		if (\OCP\Util::isSharingDisabledForUser() === false) {
0 ignored issues
show
Deprecated Code introduced by
The function OCP\Util::isSharingDisabledForUser() has been deprecated: 9.1.0 Use \OC::$server->getShareManager()->sharingDisabledForUser ( Ignorable by Annotation )

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

192
		if (/** @scrutinizer ignore-deprecated */ \OCP\Util::isSharingDisabledForUser() === false) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

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

Loading history...
193
			// Check if sharing by link is enabled
194
			if ($config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes') {
195
				array_push($sharingSublistArray, [
196
					'id' => 'sharinglinks',
197
					'appname' => 'files_sharing',
198
					'script' => 'list.php',
199
					'order' => 17,
200
					'name' => $l->t('Shared by link'),
201
				]);
202
			}
203
		}
204
205
		array_push($sharingSublistArray, [
206
			'id' => 'deletedshares',
207
			'appname' => 'files_sharing',
208
			'script' => 'list.php',
209
			'order' => 19,
210
			'name' => $l->t('Deleted shares'),
211
		]);
212
213
		array_push($sharingSublistArray, [
214
			'id' => 'pendingshares',
215
			'appname' => 'files_sharing',
216
			'script' => 'list.php',
217
			'order' => 19,
218
			'name' => $l->t('Pending shares'),
219
		]);
220
221
222
		// show_Quick_Access stored as string
223
		\OCA\Files\App::getNavigationManager()->add([
224
			'id' => 'shareoverview',
225
			'appname' => 'files_sharing',
226
			'script' => 'list.php',
227
			'order' => 18,
228
			'name' => $l->t('Shares'),
229
			'classes' => 'collapsible',
230
			'sublist' => $sharingSublistArray,
231
			'expandedState' => 'show_sharing_menu'
232
		]);
233
	}
234
}
235