Application::__construct()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 223

Duplication

Lines 50
Ratio 22.42 %

Code Coverage

Tests 123
CRAP Score 3.016

Importance

Changes 0
Metric Value
dl 50
loc 223
ccs 123
cts 140
cp 0.8786
rs 8
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 3.016

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
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity\AppInfo;
23
24
use OC\Files\View;
25
use OCA\Activity\Consumer;
26
use OCA\Activity\Controller\Activities;
27
use OCA\Activity\Controller\EndPoint;
28
use OCA\Activity\Controller\Feed;
29
use OCA\Activity\Controller\OCSEndPoint;
30
use OCA\Activity\Controller\Settings;
31
use OCA\Activity\Data;
32
use OCA\Activity\DataHelper;
33
use OCA\Activity\GroupHelper;
34
use OCA\Activity\FilesHooks;
35
use OCA\Activity\MailQueueHandler;
36
use OCA\Activity\Navigation;
37
use OCA\Activity\Parameter\Factory;
38
use OCA\Activity\UserSettings;
39
use OCA\Activity\ViewInfoCache;
40
use OCP\AppFramework\App;
41
use OCP\AppFramework\IAppContainer;
42
use OCP\IContainer;
43
use OCP\Util;
44
45
class Application extends App {
46 24
	public function __construct(array $urlParams = []) {
47 24
		parent::__construct('activity', $urlParams);
48 24
		$container = $this->getContainer();
49
50
		/**
51
		 * Activity Services
52
		 */
53
		$container->registerService('ActivityData', function (IContainer $c) {
54
			/** @var \OC\Server $server */
55 16
			$server = $c->query('ServerContainer');
56 16
			return new Data(
57 16
				$server->getActivityManager(),
58 16
				$server->getDatabaseConnection(),
59 16
				$server->getUserSession()
60
			);
61 24
		});
62
63
		$container->registerService('Consumer', function (IContainer $c) {
64
			/** @var \OC\Server $server */
65 2
			$server = $c->query('ServerContainer');
66
67 2
			return new Consumer(
68 2
				$c->query('ActivityData'),
69 2
				$c->query('UserSettings'),
70 2
				$server->getL10NFactory()
71
			);
72 24
		});
73
74
		$container->registerService('DataHelper', function (IContainer $c) {
75
			/** @var \OC\Server $server */
76 10
			$server = $c->query('ServerContainer');
77 10
			return new DataHelper(
78 10
				$server->getActivityManager(),
79 10
				new Factory(
80 10
					$server->getActivityManager(),
81 10
					$server->getUserManager(),
82 10
					$server->getURLGenerator(),
83 10
					$server->getContactsManager(),
84 10
					$c->query('OCA\Activity\ViewInfoCache'),
85 10
					$c->query('OCP\IL10N'),
86 10
					$server->getGroupManager(),
87 10
					$c->query('CurrentUID')
88
				),
89 10
				$server->getL10NFactory(),
90 10
				$c->query('OCP\IL10N')
91
			);
92 24
		});
93
94
		$container->registerService('GroupHelper', function (IContainer $c) {
95 6
			return new GroupHelper(
96 6
				$c->query('ServerContainer')->getActivityManager(),
97 6
				$c->query('DataHelper'),
98 6
				true
99
			);
100 24
		});
101
102
		$container->registerService('GroupHelperSingleEntries', function (IContainer $c) {
103 1
			return new GroupHelper(
104 1
				$c->query('ServerContainer')->getActivityManager(),
105 1
				$c->query('DataHelper'),
106 1
				false
107
			);
108 24
		});
109
110
		$container->registerService('Hooks', function (IContainer $c) {
111
			/** @var \OC\Server $server */
112 1
			$server = $c->query('ServerContainer');
113
114 1
			return new FilesHooks(
115 1
				$server->getActivityManager(),
116 1
				$c->query('ActivityData'),
117 1
				$c->query('UserSettings'),
118 1
				$server->getGroupManager(),
119 1
				new View(''),
120 1
				$server->getDatabaseConnection(),
121 1
				$server->getURLGenerator(),
122 1
				$c->query('CurrentUID')
123
			);
124 24
		});
125
126
		$container->registerService('MailQueueHandler', function (IContainer $c) {
127
			/** @var \OC\Server $server */
128 2
			$server = $c->query('ServerContainer');
129
130 2
			return new MailQueueHandler(
131 2
				$server->getDateTimeFormatter(),
132 2
				$server->getDatabaseConnection(),
133 2
				$c->query('DataHelper'),
134 2
				$server->getMailer(),
135 2
				$server->getURLGenerator(),
136 2
				$server->getUserManager(),
137 2
				$server->getActivityManager()
138
			);
139 24
		});
140
141
		$container->registerService('Navigation', function (IContainer $c) {
142
			/** @var \OC\Server $server */
143 2
			$server = $c->query('ServerContainer');
144 2
			$rssToken = ($c->query('CurrentUID') !== '') ? $server->getConfig()->getUserValue($c->query('CurrentUID'), 'activity', 'rsstoken') : '';
145
146 2
			return new Navigation(
147 2
				$c->query('OCP\IL10N'),
148 2
				$server->getActivityManager(),
149 2
				$server->getURLGenerator(),
150 2
				$c->query('CurrentUID'),
151 2
				$rssToken
152
			);
153 24
		});
154
155
		$container->registerService('UserSettings', function (IContainer $c) {
156
			/** @var \OC\Server $server */
157 11
			$server = $c->query('ServerContainer');
158 11
			return new UserSettings(
159 11
				$server->getActivityManager(),
160 11
				$server->getConfig(),
161 11
				$c->query('ActivityData')
162
			);
163 24
		});
164
165
		$container->registerService('OCA\Activity\ViewInfoCache', function () {
166 11
			return new ViewInfoCache(
167 11
				new View('')
168
			);
169 24
		});
170
171
		/**
172
		 * Core Services
173
		 */
174
		$container->registerService('CurrentUID', function (IContainer $c) {
175
			/** @var \OC\Server $server */
176 13
			$server = $c->query('ServerContainer');
177
178 13
			$user = $server->getUserSession()->getUser();
179 13
			return ($user) ? $user->getUID() : '';
180 24
		});
181
		$container->registerService('CurrentUser', function (IContainer $c) {
182
			/** @var \OC\Server $server */
183 1
			$server = $c->query('ServerContainer');
184
185 1
			$user = $server->getUserSession()->getUser();
186 1
			return $user;
187 24
		});
188
189
		/**
190
		 * Controller
191
		 */
192 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...
193
			/** @var \OC\Server $server */
194 1
			$server = $c->query('ServerContainer');
195
196 1
			return new Settings(
197 1
				$c->getAppName(),
198 1
				$server->getRequest(),
199 1
				$server->getConfig(),
200 1
				$server->getSecureRandom(),
201 1
				$server->getURLGenerator(),
202 1
				$c->query('ActivityData'),
203 1
				$c->query('UserSettings'),
204 1
				$c->query('OCP\IL10N'),
205 1
				$c->query('CurrentUser')
206
			);
207 24
		});
208
209 View Code Duplication
		$container->registerService('OCA\Activity\Controller\OCSEndPoint', 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...
210
			/** @var \OC\Server $server */
211
			$server = $c->query('ServerContainer');
212
213
			return new OCSEndPoint(
214
				$c->query('ActivityData'),
215
				$c->query('GroupHelper'),
216
				$c->query('UserSettings'),
217
				$server->getRequest(),
218
				$server->getURLGenerator(),
219
				$server->getUserSession(),
220
				$server->getPreviewManager(),
221
				$server->getMimeTypeDetector(),
222
				new View(''),
223
				$c->query('OCA\Activity\ViewInfoCache')
224
			);
225 24
		});
226
227
		$container->registerService('EndPointController', function (IAppContainer $c) {
228
			/** @var \OC\Server $server */
229
			$server = $c->query('ServerContainer');
230
231
			return new EndPoint(
232
				$c->getAppName(),
233
				$server->getRequest(),
234
				$c->query('OCA\Activity\Controller\OCSEndPoint')
235
			);
236 24
		});
237
238
		$container->registerService('ActivitiesController', function (IAppContainer $c) {
239
			/** @var \OC\Server $server */
240 1
			$server = $c->query('ServerContainer');
241
242 1
			return new Activities(
243 1
				$c->getAppName(),
244 1
				$server->getRequest(),
245 1
				$server->getConfig(),
246 1
				$c->query('ActivityData'),
247 1
				$c->query('Navigation')
248
			);
249 24
		});
250
251 View Code Duplication
		$container->registerService('FeedController', 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...
252
			/** @var \OC\Server $server */
253 1
			$server = $c->query('ServerContainer');
254
255 1
			return new Feed(
256 1
				$c->getAppName(),
257 1
				$server->getRequest(),
258 1
				$c->query('ActivityData'),
259 1
				$c->query('GroupHelperSingleEntries'),
260 1
				$c->query('UserSettings'),
261 1
				$server->getURLGenerator(),
262 1
				$server->getActivityManager(),
263 1
				$server->getL10NFactory(),
264 1
				$server->getConfig(),
265 1
				$c->query('CurrentUID')
266
			);
267 24
		});
268 24
	}
269
270
	/**
271
	 * Registers the consumer to the Activity Manager
272
	 */
273
	public function registerActivityConsumer() {
274
		$c = $this->getContainer();
275
		/** @var \OCP\IServerContainer $server */
276
		$server = $c->getServer();
277
278
		$server->getActivityManager()->registerConsumer(function () use ($c) {
279
			return $c->query('Consumer');
280
		});
281
	}
282
283
	/**
284
	 * Register the hooks and events
285
	 */
286
	public function registerHooksAndEvents() {
287
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
288
		$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Activity\FilesHooksStatic', 'onLoadFilesAppScripts']);
289
290
		Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Activity\Hooks', 'deleteUser');
291
292
		$this->registerFilesActivity();
293
	}
294
295
	/**
296
	 * Register the hooks for filesystem operations
297
	 */
298
	public function registerFilesActivity() {
299
		// All other events from other apps have to be send via the Consumer
300
		Util::connectHook('OC_Filesystem', 'post_create', 'OCA\Activity\FilesHooksStatic', 'fileCreate');
301
		Util::connectHook('OC_Filesystem', 'post_update', 'OCA\Activity\FilesHooksStatic', 'fileUpdate');
302
		Util::connectHook('OC_Filesystem', 'delete', 'OCA\Activity\FilesHooksStatic', 'fileDelete');
303
		Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OCA\Activity\FilesHooksStatic', 'fileRestore');
304
		Util::connectHook('OCP\Share', 'post_shared', 'OCA\Activity\FilesHooksStatic', 'share');
305
		Util::connectHook('OCP\Share', 'pre_unshare', 'OCA\Activity\FilesHooksStatic', 'unShare');
306
	}
307
}
308