Completed
Push — stable9 ( 127965...531e69 )
by Lukas
09:03
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
 * @author Lukas Reschke <[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\UpdateNotification\Controller;
23
24
use OCA\UpdateNotification\UpdateChecker;
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http\DataResponse;
27
use OCP\AppFramework\Http\TemplateResponse;
28
use OCP\AppFramework\Utility\ITimeFactory;
29
use OCP\BackgroundJob\IJobList;
30
use OCP\IConfig;
31
use OCP\IDateTimeFormatter;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\Security\ISecureRandom;
35
36
class AdminController extends Controller {
37
	/** @var IJobList */
38
	private $jobList;
39
	/** @var ISecureRandom */
40
	private $secureRandom;
41
	/** @var IConfig */
42
	private $config;
43
	/** @var ITimeFactory */
44
	private $timeFactory;
45
	/** @var UpdateChecker */
46
	private $updateChecker;
47
	/** @var IL10N */
48
	private $l10n;
49
	/** @var IDateTimeFormatter */
50
	private $dateTimeFormatter;
51
52
	/**
53
	 * @param string $appName
54
	 * @param IRequest $request
55
	 * @param IJobList $jobList
56
	 * @param ISecureRandom $secureRandom
57
	 * @param IConfig $config
58
	 * @param ITimeFactory $timeFactory
59
	 * @param IL10N $l10n
60
	 * @param UpdateChecker $updateChecker
61
	 * @param IDateTimeFormatter $dateTimeFormatter
62
	 */
63
	public function __construct($appName,
64
								IRequest $request,
65
								IJobList $jobList,
66
								ISecureRandom $secureRandom,
67
								IConfig $config,
68
								ITimeFactory $timeFactory,
69
								IL10N $l10n,
70
								UpdateChecker $updateChecker,
71
								IDateTimeFormatter $dateTimeFormatter) {
72
		parent::__construct($appName, $request);
73
		$this->jobList = $jobList;
74
		$this->secureRandom = $secureRandom;
75
		$this->config = $config;
76
		$this->timeFactory = $timeFactory;
77
		$this->l10n = $l10n;
78
		$this->updateChecker = $updateChecker;
79
		$this->dateTimeFormatter = $dateTimeFormatter;
80
	}
81
82
	/**
83
	 * Whether the instance is compatible with the updater
84
	 *
85
	 * @return bool
86
	 */
87
	protected function isCompatibleWithUpdater() {
88
		$updaterCompatible = true;
89
		if(!function_exists('proc_open') || !function_exists('shell_exec')) {
90
			$updaterCompatible = false;
91
		} else {
92
			$whichUnzip = shell_exec('command -v unzip');
93
			if(!class_exists('ZipArchive') && empty($whichUnzip)) {
94
				$updaterCompatible = false;
95
			}
96
97
			$whichPhp = shell_exec('command -v php');
98
			if(empty($whichPhp)) {
99
				$updaterCompatible = false;
100
			}
101
		}
102
		if(!function_exists('curl_exec')) {
103
			$updaterCompatible = false;
104
		}
105
106
		return $updaterCompatible;
107
	}
108
109
	/**
110
	 * @return TemplateResponse
111
	 */
112
	public function displayPanel() {
113
		$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime(
114
			$this->config->getAppValue('core', 'lastupdatedat')
115
		);
116
117
		$channels = [
118
			'daily',
119
			'beta',
120
			'stable',
121
			'production',
122
		];
123
		$currentChannel = \OCP\Util::getChannel();
124
		// Remove the currently used channel from the channels list
125
		if(($key = array_search($currentChannel, $channels)) !== false) {
126
			unset($channels[$key]);
127
		}
128
		$updateState = $this->updateChecker->getUpdateState();
129
		$params = [
130
			'isNewVersionAvailable' => ($updateState === []) ? false : true,
131
			'lastChecked' => $lastUpdateCheck,
132
			'currentChannel' => $currentChannel,
133
			'channels' => $channels,
134
			'newVersionString' => ($updateState === []) ? '' : $updateState['updateVersion'],
135
			'updaterRequirementsFulfilled' => $this->isCompatibleWithUpdater(),
136
		];
137
138
		return new TemplateResponse($this->appName, 'admin', $params, '');
139
	}
140
141
	/**
142
	 * @UseSession
143
	 *
144
	 * @param string $channel
145
	 * @return DataResponse
146
	 */
147
	public function setChannel($channel) {
148
		\OCP\Util::setChannel($channel);
149
		$this->config->setAppValue('core', 'lastupdatedat', 0);
150
		return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Updated channel')]]);
151
	}
152
153
	/**
154
	 * @return DataResponse
155
	 */
156
	public function createCredentials() {
157
		// Create a new job and store the creation date
158
		$this->jobList->add('OCA\UpdateNotification\ResetTokenBackgroundJob');
159
		$this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
160
161
		// Create a new token
162
		$newToken = $this->secureRandom->generate(64);
163
		$this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
164
165
		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...
166
	}
167
}
168