Completed
Pull Request — stable9 (#1501)
by Morris
111:49 queued 102:51
created

AdminController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 9
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0

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 Lukas Reschke <[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\UpdateNotification\Controller;
24
25
use OCA\UpdateNotification\UpdateChecker;
26
use OCP\AppFramework\Controller;
27
use OCP\AppFramework\Http\DataResponse;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\BackgroundJob\IJobList;
31
use OCP\IConfig;
32
use OCP\IDateTimeFormatter;
33
use OCP\IL10N;
34
use OCP\IRequest;
35
use OCP\Security\ISecureRandom;
36
37
class AdminController extends Controller {
38
	/** @var IJobList */
39
	private $jobList;
40
	/** @var ISecureRandom */
41
	private $secureRandom;
42
	/** @var IConfig */
43
	private $config;
44
	/** @var ITimeFactory */
45
	private $timeFactory;
46
	/** @var UpdateChecker */
47
	private $updateChecker;
48
	/** @var IL10N */
49
	private $l10n;
50
	/** @var IDateTimeFormatter */
51
	private $dateTimeFormatter;
52
53
	/**
54
	 * @param string $appName
55
	 * @param IRequest $request
56
	 * @param IJobList $jobList
57
	 * @param ISecureRandom $secureRandom
58
	 * @param IConfig $config
59
	 * @param ITimeFactory $timeFactory
60
	 * @param IL10N $l10n
61
	 * @param UpdateChecker $updateChecker
62
	 * @param IDateTimeFormatter $dateTimeFormatter
63
	 */
64
	public function __construct($appName,
65
								IRequest $request,
66
								IJobList $jobList,
67
								ISecureRandom $secureRandom,
68
								IConfig $config,
69
								ITimeFactory $timeFactory,
70
								IL10N $l10n,
71
								UpdateChecker $updateChecker,
72
								IDateTimeFormatter $dateTimeFormatter) {
73
		parent::__construct($appName, $request);
74
		$this->jobList = $jobList;
75
		$this->secureRandom = $secureRandom;
76
		$this->config = $config;
77
		$this->timeFactory = $timeFactory;
78
		$this->l10n = $l10n;
79
		$this->updateChecker = $updateChecker;
80
		$this->dateTimeFormatter = $dateTimeFormatter;
81
	}
82
83
	/**
84
	 * @return TemplateResponse
85
	 */
86
	public function displayPanel() {
87
		$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime(
88
			$this->config->getAppValue('core', 'lastupdatedat')
89
		);
90
91
		$channels = [
92
			'daily',
93
			'beta',
94
			'stable',
95
			'production',
96
		];
97
		$currentChannel = \OCP\Util::getChannel();
98
99
		// Remove the currently used channel from the channels list
100
		if(($key = array_search($currentChannel, $channels)) !== false) {
101
			unset($channels[$key]);
102
		}
103
		$updateState = $this->updateChecker->getUpdateState();
104
		$params = [
105
			'isNewVersionAvailable' => ($updateState === []) ? false : true,
106
			'lastChecked' => $lastUpdateCheck,
107
			'currentChannel' => $currentChannel,
108
			'channels' => $channels,
109
			'newVersionString' => ($updateState === []) ? '' : $updateState['updateVersion'],
110
		];
111
112
		return new TemplateResponse($this->appName, 'admin', $params, '');
113
	}
114
115
	/**
116
	 * @UseSession
117
	 *
118
	 * @param string $channel
119
	 * @return DataResponse
120
	 */
121
	public function setChannel($channel) {
122
		\OCP\Util::setChannel($channel);
123
		$this->config->setAppValue('core', 'lastupdatedat', 0);
124
		return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
125
	}
126
127
	/**
128
	 * @return DataResponse
129
	 */
130
	public function createCredentials() {
131
		// Create a new job and store the creation date
132
		$this->jobList->add('OCA\UpdateNotification\ResetTokenBackgroundJob');
133
		$this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
134
135
		// Create a new token
136
		$newToken = $this->secureRandom->generate(64);
137
		$this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
138
139
		return new DataResponse($newToken);
0 ignored issues
show
Documentation introduced by
$newToken is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
140
	}
141
}
142