Completed
Pull Request — master (#608)
by Sujith
127:21 queued 124:47
created

Application   B

Complexity

Total Complexity 6

Size/Duplication

Total Lines 268
Duplicated Lines 19.78 %

Coupling/Cohesion

Components 0
Dependencies 17

Test Coverage

Coverage 75.71%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 17
dl 53
loc 268
ccs 134
cts 177
cp 0.7571
rs 7.8571
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 53 223 3
A registerActivityConsumer() 0 9 1
A registerHooksAndEvents() 0 9 1
A registerFilesActivity() 0 12 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\FilesHooksStatic;
34
use OCA\Activity\GroupHelper;
35
use OCA\Activity\FilesHooks;
36
use OCA\Activity\Hooks;
37
use OCA\Activity\MailQueueHandler;
38
use OCA\Activity\Navigation;
39
use OCA\Activity\Parameter\Factory;
40
use OCA\Activity\UserSettings;
41
use OCA\Activity\ViewInfoCache;
42
use OCP\AppFramework\App;
43
use OCP\AppFramework\IAppContainer;
44
use OCP\IContainer;
45
use OCP\Util;
46
47
class Application extends App {
48 30
	public function __construct (array $urlParams = array()) {
49 30
		parent::__construct('activity', $urlParams);
50 30
		$container = $this->getContainer();
51
52
		/**
53
		 * Activity Services
54
		 */
55
		$container->registerService('ActivityData', function(IContainer $c) {
56
			/** @var \OC\Server $server */
57 22
			$server = $c->query('ServerContainer');
58 22
			return new Data(
59 22
				$server->getActivityManager(),
60 22
				$server->getDatabaseConnection(),
61 22
				$server->getUserSession()
62 22
			);
63 30
		});
64
65
		$container->registerService('Consumer', function(IContainer $c) {
66
			/** @var \OC\Server $server */
67 2
			$server = $c->query('ServerContainer');
68
69 2
			return new Consumer(
70 2
				$c->query('ActivityData'),
71 2
				$c->query('UserSettings'),
72 2
				$server->getL10NFactory()
73 2
			);
74 30
		});
75
76 View Code Duplication
		$container->registerService('DataHelper', function(IContainer $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...
77
			/** @var \OC\Server $server */
78 10
			$server = $c->query('ServerContainer');
79 10
			return new DataHelper(
80 10
				$server->getActivityManager(),
81 10
				new Factory(
82 10
					$server->getActivityManager(),
83 10
					$server->getUserManager(),
84 10
					$server->getURLGenerator(),
85 10
					$server->getContactsManager(),
86 10
					$c->query('OCA\Activity\ViewInfoCache'),
87 10
					$c->query('OCP\IL10N'),
88 10
					$server->getGroupManager(),
89 10
					$c->query('CurrentUID')
90 10
				),
91 10
				$server->getL10NFactory(),
92 10
				$c->query('OCP\IL10N')
93 10
			);
94 30
		});
95
96
		$container->registerService('GroupHelper', function(IContainer $c) {
97 6
			return new GroupHelper(
98 6
				$c->query('ServerContainer')->getActivityManager(),
99 6
				$c->query('DataHelper'),
100
				true
101 6
			);
102 30
		});
103
104
		$container->registerService('GroupHelperSingleEntries', function(IContainer $c) {
105 1
			return new GroupHelper(
106 1
				$c->query('ServerContainer')->getActivityManager(),
107 1
				$c->query('DataHelper'),
108
				false
109 1
			);
110 30
		});
111
112
		$container->registerService('Hooks', function(IContainer $c) {
113
			/** @var \OC\Server $server */
114 1
			$server = $c->query('ServerContainer');
115
116 1
			return new FilesHooks(
117 1
				$server->getActivityManager(),
118 1
				$c->query('ActivityData'),
119 1
				$c->query('UserSettings'),
120 1
				$server->getGroupManager(),
121 1
				new View(''),
122 1
				$server->getDatabaseConnection(),
123 1
				$server->getURLGenerator(),
124 1
				$c->query('CurrentUID')
125 1
			);
126 30
		});
127
128
		$container->registerService('MailQueueHandler', function(IContainer $c) {
129
			/** @var \OC\Server $server */
130 2
			$server = $c->query('ServerContainer');
131
132 2
			return new MailQueueHandler(
133 2
				$server->getDateTimeFormatter(),
134 2
				$server->getDatabaseConnection(),
135 2
				$c->query('DataHelper'),
136 2
				$server->getMailer(),
137 2
				$server->getURLGenerator(),
138 2
				$server->getUserManager(),
139 2
				$server->getActivityManager()
140 30
			);
141 30
		});
142
143
		$container->registerService('Navigation', function(IContainer $c) {
144
			/** @var \OC\Server $server */
145 2
			$server = $c->query('ServerContainer');
146 2
			$rssToken = ($c->query('CurrentUID') !== '') ? $server->getConfig()->getUserValue($c->query('CurrentUID'), 'activity', 'rsstoken') : '';
147
148 2
			return new Navigation(
149 2
				$c->query('OCP\IL10N'),
150 2
				$server->getActivityManager(),
151 2
				$server->getURLGenerator(),
152 2
				$c->query('CurrentUID'),
153
				$rssToken
154 2
			);
155 30
		});
156
157
		$container->registerService('UserSettings', function(IContainer $c) {
158
			/** @var \OC\Server $server */
159 11
			$server = $c->query('ServerContainer');
160 11
			return new UserSettings(
161 11
				$server->getActivityManager(),
162 11
				$server->getConfig(),
163 11
				$c->query('ActivityData')
164 11
			);
165 30
		});
166
167
		$container->registerService('OCA\Activity\ViewInfoCache', function() {
168 11
			return new ViewInfoCache(
169 11
				new View('')
170 11
			);
171 30
		});
172
173
		/**
174
		 * Core Services
175
		 */
176
		$container->registerService('CurrentUID', function(IContainer $c) {
177
			/** @var \OC\Server $server */
178 13
			$server = $c->query('ServerContainer');
179
180 13
			$user = $server->getUserSession()->getUser();
181 13
			return ($user) ? $user->getUID() : '';
182 30
		});
183
		$container->registerService('CurrentUser', function(IContainer $c) {
184
			/** @var \OC\Server $server */
185 1
			$server = $c->query('ServerContainer');
186
187 1
			$user = $server->getUserSession()->getUser();
188 1
			return $user;
189 30
		});
190
191
		/**
192
		 * Controller
193
		 */
194
		$container->registerService('SettingsController', function(IAppContainer $c) {
195
			/** @var \OC\Server $server */
196 1
			$server = $c->query('ServerContainer');
197
198 1
			return new Settings(
199 1
				$c->getAppName(),
200 1
				$server->getRequest(),
201 1
				$server->getConfig(),
202 1
				$server->getSecureRandom(),
203 1
				$server->getURLGenerator(),
204 1
				$c->query('ActivityData'),
205 1
				$c->query('UserSettings'),
206 1
				$c->query('OCP\IL10N'),
207 1
				$c->query('CurrentUser')
208 1
			);
209 30
		});
210
211 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...
212
			/** @var \OC\Server $server */
213
			$server = $c->query('ServerContainer');
214
215
			return new OCSEndPoint(
216
				$c->query('ActivityData'),
217
				$c->query('GroupHelper'),
218
				$c->query('UserSettings'),
219
				$server->getRequest(),
220
				$server->getURLGenerator(),
221
				$server->getUserSession(),
222
				$server->getPreviewManager(),
223
				$server->getMimeTypeDetector(),
224
				new View(''),
225
				$c->query('OCA\Activity\ViewInfoCache')
226
			);
227 30
		});
228
229
		$container->registerService('EndPointController', function(IAppContainer $c) {
230
			/** @var \OC\Server $server */
231
			$server = $c->query('ServerContainer');
232
233
			return new EndPoint(
234
				$c->getAppName(),
235
				$server->getRequest(),
236
				$c->query('OCA\Activity\Controller\OCSEndPoint')
237
			);
238 30
		});
239
240
		$container->registerService('ActivitiesController', function(IAppContainer $c) {
241
			/** @var \OC\Server $server */
242 1
			$server = $c->query('ServerContainer');
243
244 1
			return new Activities(
245 1
				$c->getAppName(),
246 1
				$server->getRequest(),
247 1
				$server->getConfig(),
248 1
				$c->query('ActivityData'),
249 1
				$c->query('Navigation')
250 1
			);
251 30
		});
252
253 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...
254
			/** @var \OC\Server $server */
255 1
			$server = $c->query('ServerContainer');
256
257 1
			return new Feed(
258 1
				$c->getAppName(),
259 1
				$server->getRequest(),
260 1
				$c->query('ActivityData'),
261 1
				$c->query('GroupHelperSingleEntries'),
262 1
				$c->query('UserSettings'),
263 1
				$server->getURLGenerator(),
264 1
				$server->getActivityManager(),
265 1
				$server->getL10NFactory(),
266 1
				$server->getConfig(),
267 1
				$c->query('CurrentUID')
268 1
			);
269 30
		});
270 30
	}
271
272
	/**
273
	 * Registers the consumer to the Activity Manager
274
	 */
275
	public function registerActivityConsumer() {
276
		$c = $this->getContainer();
277
		/** @var \OCP\IServerContainer $server */
278
		$server = $c->getServer();
279
280
		$server->getActivityManager()->registerConsumer(function() use ($c) {
281
			return $c->query('Consumer');
282
		});
283
	}
284
285
	/**
286
	 * Register the hooks and events
287
	 */
288
	public function registerHooksAndEvents() {
289
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
290
		$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Activity\FilesHooksStatic', 'onLoadFilesAppScripts']);
291
292
		$activityHook = new Hooks();
293
		$eventDispatcher->addListener('user.afterdelete', [$activityHook, 'deleteUser']);
294
295
		$this->registerFilesActivity();
296
	}
297
298
	/**
299
	 * Register the hooks for filesystem operations
300
	 */
301
	public function registerFilesActivity() {
302
		$filesHooksStatic = new FilesHooksStatic();
303
		$eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher();
304
305
		// All other events from other apps have to be send via the Consumer
306
		Util::connectHook('OC_Filesystem', 'post_create', 'OCA\Activity\FilesHooksStatic', 'fileCreate');
307
		Util::connectHook('OC_Filesystem', 'post_update', 'OCA\Activity\FilesHooksStatic', 'fileUpdate');
308
		Util::connectHook('OC_Filesystem', 'delete', 'OCA\Activity\FilesHooksStatic', 'fileDelete');
309
		Util::connectHook('\OCA\Files_Trashbin\Trashbin', 'post_restore', 'OCA\Activity\FilesHooksStatic', 'fileRestore');
310
		$eventDispatcher->addListener('file.aftercreateshare', [$filesHooksStatic, 'share']);
311
		$eventDispatcher->addListener('file.beforeunshare', [$filesHooksStatic, 'unshare']);
312
	}
313
314
}
315