Completed
Pull Request — master (#546)
by Thomas
03:34
created

Application   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 282
Duplicated Lines 17.73 %

Coupling/Cohesion

Components 0
Dependencies 15

Test Coverage

Coverage 70.1%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 15
dl 50
loc 282
ccs 129
cts 184
cp 0.701
rs 9.1666
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 50 215 3
A registerNavigationEntry() 0 16 1
A registerActivityConsumer() 0 9 1
A registerHooksAndEvents() 0 8 1
A registerFilesActivity() 0 9 1
A registerPersonalPage() 0 3 1

How to fix   Duplicated Code   

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:

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 25
	public function __construct (array $urlParams = array()) {
47 25
		parent::__construct('activity', $urlParams);
48 25
		$container = $this->getContainer();
49
50
		/**
51
		 * Activity Services
52
		 */
53
		$container->registerService('ActivityData', function(IContainer $c) {
54
			/** @var \OC\Server $server */
55 17
			$server = $c->query('ServerContainer');
56 17
			return new Data(
57 17
				$server->getActivityManager(),
58 17
				$server->getDatabaseConnection(),
59 17
				$server->getUserSession()
60 17
			);
61 25
		});
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 2
			);
72 25
		});
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
					$c->query('CurrentUID')
87 10
				),
88 10
				$server->getL10NFactory(),
89 10
				$c->query('OCP\IL10N')
90 10
			);
91 25
		});
92
93
		$container->registerService('GroupHelper', function(IContainer $c) {
94 6
			return new GroupHelper(
95 6
				$c->query('ServerContainer')->getActivityManager(),
96 6
				$c->query('DataHelper'),
97
				true
98 6
			);
99 25
		});
100
101
		$container->registerService('GroupHelperSingleEntries', function(IContainer $c) {
102 1
			return new GroupHelper(
103 1
				$c->query('ServerContainer')->getActivityManager(),
104 1
				$c->query('DataHelper'),
105
				false
106 1
			);
107 25
		});
108
109
		$container->registerService('Hooks', function(IContainer $c) {
110
			/** @var \OC\Server $server */
111 1
			$server = $c->query('ServerContainer');
112
113 1
			return new FilesHooks(
114 1
				$server->getActivityManager(),
115 1
				$c->query('ActivityData'),
116 1
				$c->query('UserSettings'),
117 1
				$server->getGroupManager(),
118 1
				new View(''),
119 1
				$server->getDatabaseConnection(),
120 1
				$server->getURLGenerator(),
121 1
				$c->query('CurrentUID')
122 1
			);
123 25
		});
124
125
		$container->registerService('MailQueueHandler', function(IContainer $c) {
126
			/** @var \OC\Server $server */
127 2
			$server = $c->query('ServerContainer');
128
129 2
			return new MailQueueHandler(
130 2
				$server->getDateTimeFormatter(),
131 2
				$server->getDatabaseConnection(),
132 2
				$c->query('DataHelper'),
133 2
				$server->getMailer(),
134 2
				$server->getURLGenerator(),
135 2
				$server->getUserManager(),
136 2
				$server->getActivityManager()
137 2
			);
138 25
		});
139
140
		$container->registerService('Navigation', function(IContainer $c) {
141
			/** @var \OC\Server $server */
142 2
			$server = $c->query('ServerContainer');
143 2
			$rssToken = ($c->query('CurrentUID') !== '') ? $server->getConfig()->getUserValue($c->query('CurrentUID'), 'activity', 'rsstoken') : '';
144
145 2
			return new Navigation(
146 2
				$c->query('OCP\IL10N'),
147 2
				$server->getActivityManager(),
148 2
				$server->getURLGenerator(),
149 2
				$c->query('CurrentUID'),
150
				$rssToken
151 2
			);
152 25
		});
153
154
		$container->registerService('UserSettings', function(IContainer $c) {
155
			/** @var \OC\Server $server */
156 12
			$server = $c->query('ServerContainer');
157 12
			return new UserSettings(
158 12
				$server->getActivityManager(),
159 12
				$server->getConfig(),
160 12
				$c->query('ActivityData')
161 12
			);
162 25
		});
163
164
		$container->registerService('OCA\Activity\ViewInfoCache', function() {
165 11
			return new ViewInfoCache(
166 11
				new View('')
167 11
			);
168 25
		});
169
170
		/**
171
		 * Core Services
172
		 */
173
		$container->registerService('CurrentUID', function(IContainer $c) {
174
			/** @var \OC\Server $server */
175 15
			$server = $c->query('ServerContainer');
176
177 15
			$user = $server->getUserSession()->getUser();
178 15
			return ($user) ? $user->getUID() : '';
179 25
		});
180
181
		/**
182
		 * Controller
183
		 */
184 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...
185
			/** @var \OC\Server $server */
186 2
			$server = $c->query('ServerContainer');
187
188 2
			return new Settings(
189 2
				$c->getAppName(),
190 2
				$server->getRequest(),
191 2
				$server->getConfig(),
192 2
				$server->getSecureRandom()->getMediumStrengthGenerator(),
193 2
				$server->getURLGenerator(),
194 2
				$c->query('ActivityData'),
195 2
				$c->query('UserSettings'),
196 2
				$c->query('OCP\IL10N'),
197 2
				$c->query('CurrentUID')
198 2
			);
199 25
		});
200
201 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...
202
			/** @var \OC\Server $server */
203
			$server = $c->query('ServerContainer');
204
205
			return new OCSEndPoint(
206
				$c->query('ActivityData'),
207
				$c->query('GroupHelper'),
208
				$c->query('UserSettings'),
209
				$server->getRequest(),
210
				$server->getURLGenerator(),
211
				$server->getUserSession(),
212
				$server->getPreviewManager(),
213
				$server->getMimeTypeDetector(),
214
				new View(''),
215
				$c->query('OCA\Activity\ViewInfoCache')
216
			);
217 25
		});
218
219
		$container->registerService('EndPointController', function(IAppContainer $c) {
220
			/** @var \OC\Server $server */
221
			$server = $c->query('ServerContainer');
222
223
			return new EndPoint(
224
				$c->getAppName(),
225
				$server->getRequest(),
226
				$c->query('OCA\Activity\Controller\OCSEndPoint')
227
			);
228 25
		});
229
230
		$container->registerService('ActivitiesController', function(IAppContainer $c) {
231
			/** @var \OC\Server $server */
232 1
			$server = $c->query('ServerContainer');
233
234 1
			return new Activities(
235 1
				$c->getAppName(),
236 1
				$server->getRequest(),
237 1
				$server->getConfig(),
238 1
				$c->query('ActivityData'),
239 1
				$c->query('Navigation')
240 1
			);
241 25
		});
242
243 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...
244
			/** @var \OC\Server $server */
245 1
			$server = $c->query('ServerContainer');
246
247 1
			return new Feed(
248 1
				$c->getAppName(),
249 1
				$server->getRequest(),
250 1
				$c->query('ActivityData'),
251 1
				$c->query('GroupHelperSingleEntries'),
252 1
				$c->query('UserSettings'),
253 1
				$server->getURLGenerator(),
254 1
				$server->getActivityManager(),
255 1
				$server->getL10NFactory(),
256 1
				$server->getConfig(),
257 1
				$c->query('CurrentUID')
258 1
			);
259 25
		});
260 25
	}
261
262
	/**
263
	 * Register the navigation entry
264
	 */
265
	public function registerNavigationEntry() {
266
		$c = $this->getContainer();
267
		/** @var \OCP\IServerContainer $server */
268
		$server = $c->getServer();
269
270
		$navigationEntry = function () use ($c, $server) {
271
			return [
272
				'id' => $c->getAppName(),
273
				'order' => 1,
274
				'name' => $c->query('OCP\IL10N')->t('Activity'),
275
				'href' => $server->getURLGenerator()->linkToRoute('activity.Activities.showList'),
276
				'icon' => $server->getURLGenerator()->imagePath($c->getAppName(), 'activity.svg'),
277
			];
278
		};
279
		$server->getNavigationManager()->add($navigationEntry);
280
	}
281
282
	/**
283
	 * Registers the consumer to the Activity Manager
284
	 */
285
	public function registerActivityConsumer() {
286
		$c = $this->getContainer();
287
		/** @var \OCP\IServerContainer $server */
288
		$server = $c->getServer();
289
290
		$server->getActivityManager()->registerConsumer(function() use ($c) {
291
			return $c->query('Consumer');
292
		});
293
	}
294
295
	/**
296
	 * Register the hooks and events
297
	 */
298
	public function registerHooksAndEvents() {
299
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
300
		$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Activity\FilesHooksStatic', 'onLoadFilesAppScripts']);
301
302
		Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Activity\Hooks', 'deleteUser');
303
304
		$this->registerFilesActivity();
305
	}
306
307
	/**
308
	 * Register the hooks for filesystem operations
309
	 */
310
	public function registerFilesActivity() {
311
		// All other events from other apps have to be send via the Consumer
312
		Util::connectHook('OC_Filesystem', 'post_create', 'OCA\Activity\FilesHooksStatic', 'fileCreate');
313
		Util::connectHook('OC_Filesystem', 'post_update', 'OCA\Activity\FilesHooksStatic', 'fileUpdate');
314
		Util::connectHook('OC_Filesystem', 'delete', 'OCA\Activity\FilesHooksStatic', 'fileDelete');
315
		Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OCA\Activity\FilesHooksStatic', 'fileRestore');
316
		Util::connectHook('OCP\Share', 'post_shared', 'OCA\Activity\FilesHooksStatic', 'share');
317
		Util::connectHook('OCP\Share', 'pre_unshare', 'OCA\Activity\FilesHooksStatic', 'unShare');
318
	}
319
320
	/**
321
	 * Register personal settings for notifications and emails
322
	 */
323
	public function registerPersonalPage() {
324
		\OCP\App::registerPersonal($this->getContainer()->getAppName(), 'personal');
325
	}
326
}
327