Completed
Push — master ( ba614c...7b56fc )
by
unknown
30:52 queued 28:38
created

Feed   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 128
Duplicated Lines 15.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.62%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 20
loc 128
ccs 44
cts 47
cp 0.9362
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 20 20 1
B show() 0 53 5

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\Controller;
23
24
use OCA\Activity\Data;
25
use OCA\Activity\GroupHelper;
26
use OCA\Activity\PlainTextParser;
27
use OCA\Activity\UserSettings;
28
use OCP\Activity\IManager;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\IURLGenerator;
35
use OCP\L10N\IFactory;
36
use OCP\Util;
37
38
class Feed extends Controller {
39
	const DEFAULT_PAGE_SIZE = 30;
40
41
	/** @var \OCA\Activity\Data */
42
	protected $data;
43
44
	/** @var \OCA\Activity\GroupHelper */
45
	protected $helper;
46
47
	/** @var \OCA\Activity\UserSettings */
48
	protected $settings;
49
50
	/** @var IURLGenerator */
51
	protected $urlGenerator;
52
53
	/** @var IManager */
54
	protected $activityManager;
55
56
	/** @var IConfig */
57
	protected $config;
58
59
	/** @var IFactory */
60
	protected $l10nFactory;
61
62
	/** @var IL10N */
63
	protected $l;
64
65
	/** @var string */
66
	protected $user;
67
68
	/** @var string */
69
	protected $tokenUser;
70
71
	/**
72
	 * constructor of the controller
73
	 *
74
	 * @param string $appName
75
	 * @param IRequest $request
76
	 * @param Data $data
77
	 * @param GroupHelper $helper
78
	 * @param UserSettings $settings
79
	 * @param IURLGenerator $urlGenerator
80
	 * @param IManager $activityManager
81
	 * @param IFactory $l10nFactory
82
	 * @param IConfig $config
83
	 * @param string $user
84
	 */
85 5 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
								IRequest $request,
87
								Data $data,
88
								GroupHelper $helper,
89
								UserSettings $settings,
90
								IURLGenerator $urlGenerator,
91
								IManager $activityManager,
92
								IFactory $l10nFactory,
93
								IConfig $config,
94
								$user) {
95 5
		parent::__construct($appName, $request);
96 5
		$this->data = $data;
97 5
		$this->helper = $helper;
98 5
		$this->settings = $settings;
99 5
		$this->urlGenerator = $urlGenerator;
100 5
		$this->activityManager = $activityManager;
101 5
		$this->l10nFactory = $l10nFactory;
102 5
		$this->config = $config;
103 5
		$this->user = $user;
104 5
	}
105
106
	/**
107
	 * @PublicPage
108
	 * @NoCSRFRequired
109
	 *
110
	 * @return TemplateResponse
111
	 */
112 4
	public function show() {
113
		try {
114 4
			$user = $this->activityManager->getCurrentUserId();
115
116 2
			$userLang = $this->config->getUserValue($user, 'core', 'lang');
117
118
			// Overwrite user and language in the helper
119 2
			$this->l = $this->l10nFactory->get('activity', $userLang);
120 2
			$parser = new PlainTextParser($this->l);
121 2
			$this->helper->setL10n($this->l);
122 2
			$this->helper->setUser($user);
123
124 2
			$description = (string) $this->l->t('Personal activity feed for %s', $user);
125 2
			$response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all');
126 2
			$data = $response['data'];
127
128 2
			$activities = [];
129 2
			foreach ($data as $activity) {
130
				$activity['subject_prepared'] = $parser->parseMessage($activity['subject_prepared']);
131
				$activity['message_prepared'] = $parser->parseMessage($activity['message_prepared']);
132
				$activities[] = $activity;
133 2
			}
134
135 4
		} catch (\UnexpectedValueException $e) {
136 2
			$this->l = $this->l10nFactory->get('activity');
137 2
			$description = (string) $this->l->t('Your feed URL is invalid');
138
139
			$activities = [
140
				[
141 2
					'activity_id'	=> -1,
142 2
					'timestamp'		=> time(),
143 2
					'subject'		=> true,
144 2
					'subject_prepared'	=> $description,
145
				]
146 2
			];
147
		}
148
149 4
		$response = new TemplateResponse('activity', 'rss', [
150 4
			'rssLang'		=> $this->l->getLanguageCode(),
151 4
			'rssLink'		=> $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'),
152 4
			'rssPubDate'	=> date('r'),
153 4
			'description'	=> $description,
154 4
			'activities'	=> $activities,
155 4
		], '');
156
157 4
		if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) {
158 2
			$response->addHeader('Content-Type', 'application/rss+xml');
159 2
		} else {
160 2
			$response->addHeader('Content-Type', 'text/xml; charset=UTF-8');
161
		}
162
163 4
		return $response;
164
	}
165
}
166