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

WhatsNewController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 8
loc 88
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
B get() 8 33 7
A dismiss() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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