Completed
Push — stable9 ( 62391e...efc4a1 )
by
unknown
08:27
created

AdminController::isCompatibleWithUpdater()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 14
nc 10
nop 0
dl 0
loc 21
rs 7.551
c 1
b 0
f 0
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 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...
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
	 * @return TemplateResponse
84
	 */
85
	public function displayPanel() {
86
		$lastUpdateCheck = $this->dateTimeFormatter->formatDateTime(
87
			$this->config->getAppValue('core', 'lastupdatedat')
88
		);
89
90
		$channels = [
91
			'daily',
92
			'beta',
93
			'stable',
94
			'production',
95
		];
96
		$currentChannel = \OCP\Util::getChannel();
97
98
		// Remove the currently used channel from the channels list
99
		if(($key = array_search($currentChannel, $channels)) !== false) {
100
			unset($channels[$key]);
101
		}
102
		$updateState = $this->updateChecker->getUpdateState();
103
		$params = [
104
			'isNewVersionAvailable' => ($updateState === []) ? false : true,
105
			'lastChecked' => $lastUpdateCheck,
106
			'currentChannel' => $currentChannel,
107
			'channels' => $channels,
108
			'newVersionString' => ($updateState === []) ? '' : $updateState['updateVersion'],
109
		];
110
111
		return new TemplateResponse($this->appName, 'admin', $params, '');
112
	}
113
114
	/**
115
	 * @UseSession
116
	 *
117
	 * @param string $channel
118
	 * @return DataResponse
119
	 */
120
	public function setChannel($channel) {
121
		\OCP\Util::setChannel($channel);
122
		$this->config->setAppValue('core', 'lastupdatedat', 0);
123
		return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Updated channel')]]);
124
	}
125
126
	/**
127
	 * @return DataResponse
128
	 */
129
	public function createCredentials() {
130
		// Create a new job and store the creation date
131
		$this->jobList->add('OCA\UpdateNotification\ResetTokenBackgroundJob');
132
		$this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
133
134
		// Create a new token
135
		$newToken = $this->secureRandom->generate(64);
136
		$this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
137
138
		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...
139
	}
140
}
141