Completed
Pull Request — stable9 (#58)
by Joas
02:24
created

Feed::show()   B

Complexity

Conditions 5
Paths 30

Size

Total Lines 53
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 53
ccs 30
cts 30
cp 1
rs 8.7155
c 2
b 0
f 0
cc 5
eloc 36
nc 30
nop 0
crap 5

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