Completed
Push — master ( 1801e4...143145 )
by Morris
19:04 queued 02:13
created

WhatsNewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 10
dl 0
loc 19
rs 9.6333
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) 2018 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Core\Controller;
25
26
use OC\CapabilitiesManager;
27
use OC\Security\IdentityProof\Manager;
28
use OC\Updater\ChangesCheck;
29
use OCP\AppFramework\Db\DoesNotExistException;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\DataResponse;
32
use OCP\Defaults;
33
use OCP\IConfig;
34
use OCP\IRequest;
35
use OCP\IUserManager;
36
use OCP\IUserSession;
37
use OCP\L10N\IFactory;
38
39
class WhatsNewController extends OCSController {
40
41
	/** @var IConfig */
42
	protected $config;
43
	/** @var IUserSession */
44
	private $userSession;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
45
	/** @var ChangesCheck */
46
	private $whatsNewService;
47
	/** @var IFactory */
48
	private $langFactory;
49
	/** @var Defaults */
50
	private $defaults;
51
52
	public function __construct(
53
		string $appName,
54
		IRequest $request,
55
		CapabilitiesManager $capabilitiesManager,
56
		IUserSession $userSession,
57
		IUserManager $userManager,
58
		Manager $keyManager,
59
		IConfig $config,
60
		ChangesCheck $whatsNewService,
61
		IFactory $langFactory,
62
		Defaults $defaults
63
	) {
64
		parent::__construct($appName, $request, $capabilitiesManager, $userSession, $userManager, $keyManager);
65
		$this->config = $config;
66
		$this->userSession = $userSession;
67
		$this->whatsNewService = $whatsNewService;
68
		$this->langFactory = $langFactory;
69
		$this->defaults = $defaults;
70
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 */
75
	public function get():DataResponse {
76
		$user = $this->userSession->getUser();
77
		if($user === null) {
78
			throw new \RuntimeException("Acting user cannot be resolved");
79
		}
80
		$lastRead = $this->config->getUserValue($user->getUID(), 'core', 'whatsNewLastRead', 0);
81
		$currentVersion = $this->whatsNewService->normalizeVersion($this->config->getSystemValue('version'));
82
83
		if(version_compare($lastRead, $currentVersion, '>=')) {
84
			return new DataResponse([], Http::STATUS_NO_CONTENT);
85
		}
86
87
		try {
88
			$iterator = $this->langFactory->getLanguageIterator();
89
			$whatsNew = $this->whatsNewService->getChangesForVersion($currentVersion);
90
			$resultData = [
91
				'changelogURL' => $whatsNew['changelogURL'],
92
				'product' => $this->defaults->getName(),
93
				'version' => $currentVersion,
94
			];
95 View Code Duplication
			do {
96
				$lang = $iterator->current();
97
				if(isset($whatsNew['whatsNew'][$lang])) {
98
					$resultData['whatsNew'] = $whatsNew['whatsNew'][$lang];
99
					break;
100
				}
101
				$iterator->next();
102
			} while ($lang !== 'en' && $iterator->valid());
103
			return new DataResponse($resultData);
104
		} catch (DoesNotExistException $e) {
105
			return new DataResponse([], Http::STATUS_NO_CONTENT);
106
		}
107
	}
108
109
	/**
110
	 * @NoAdminRequired
111
	 *
112
	 * @throws \OCP\PreConditionNotMetException
113
	 * @throws DoesNotExistException
114
	 */
115
	public function dismiss(string $version):DataResponse {
116
		$user = $this->userSession->getUser();
117
		if($user === null) {
118
			throw new \RuntimeException("Acting user cannot be resolved");
119
		}
120
		$version = $this->whatsNewService->normalizeVersion($version);
121
		// checks whether it's a valid version, throws an Exception otherwise
122
		$this->whatsNewService->getChangesForVersion($version);
123
		$this->config->setUserValue($user->getUID(), 'core', 'whatsNewLastRead', $version);
124
		return new DataResponse();
125
	}
126
}
127