Feed::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity\Controller;
24
25
use OCA\Activity\Data;
26
use OCA\Activity\GroupHelper;
27
use OCA\Activity\PlainTextParser;
28
use OCA\Activity\UserSettings;
29
use OCP\Activity\IManager;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\IConfig;
33
use OCP\IL10N;
34
use OCP\IRequest;
35
use OCP\IURLGenerator;
36
use OCP\L10N\IFactory;
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
	/**
66
	 * constructor of the controller
67
	 *
68
	 * @param string $appName
69
	 * @param IRequest $request
70
	 * @param Data $data
71
	 * @param GroupHelper $helper
72
	 * @param UserSettings $settings
73
	 * @param IURLGenerator $urlGenerator
74
	 * @param IManager $activityManager
75
	 * @param IFactory $l10nFactory
76
	 * @param IConfig $config
77
	 */
78 6
	public function __construct($appName,
79
								IRequest $request,
80
								Data $data,
81
								GroupHelper $helper,
82
								UserSettings $settings,
83
								IURLGenerator $urlGenerator,
84
								IManager $activityManager,
85
								IFactory $l10nFactory,
86
								IConfig $config) {
87 6
		parent::__construct($appName, $request);
88 6
		$this->data = $data;
89 6
		$this->helper = $helper;
90 6
		$this->settings = $settings;
91 6
		$this->urlGenerator = $urlGenerator;
92 6
		$this->activityManager = $activityManager;
93 6
		$this->l10nFactory = $l10nFactory;
94 6
		$this->config = $config;
95 6
	}
96
97
	/**
98
	 * @PublicPage
99
	 * @NoCSRFRequired
100
	 *
101
	 * @return TemplateResponse
102
	 */
103 4
	public function show() {
104
		try {
105 4
			$user = $this->activityManager->getCurrentUserId();
106
107 2
			$userLang = $this->config->getUserValue($user, 'core', 'lang');
108
109
			// Overwrite user and language in the helper
110 2
			$this->l = $this->l10nFactory->get('activity', $userLang);
111 2
			$parser = new PlainTextParser($this->l);
0 ignored issues
show
Unused Code introduced by
$parser is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
112 2
			$this->helper->setL10n($this->l);
113 2
			$this->helper->setUser($user);
114
115 2
			$description = (string) $this->l->t('Personal activity feed for %s', $user);
116 2
			$response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all');
117 2
			$activities = $response['data'];
118
119 2
		} catch (\UnexpectedValueException $e) {
120 2
			$this->l = $this->l10nFactory->get('activity');
121 2
			$description = (string) $this->l->t('Your feed URL is invalid');
122
123
			$activities = [
124
				[
125 2
					'activity_id'	=> -1,
126 2
					'timestamp'		=> time(),
127
					'subject'		=> true,
128 2
					'subject_prepared'	=> $description,
129
				]
130
			];
131
		}
132
133 4
		$response = new TemplateResponse('activity', 'rss', [
134 4
			'rssLang'		=> $this->l->getLanguageCode(),
135 4
			'rssLink'		=> $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'),
136 4
			'rssPubDate'	=> date('r'),
137 4
			'description'	=> $description,
138 4
			'activities'	=> $activities,
139 4
		], '');
140
141 4
		if (stristr($this->request->getHeader('accept'), 'application/rss+xml')) {
142 2
			$response->addHeader('Content-Type', 'application/rss+xml');
143
		} else {
144 2
			$response->addHeader('Content-Type', 'text/xml; charset=UTF-8');
145
		}
146
147 4
		return $response;
148
	}
149
}
150