Passed
Push — master ( 111496...487d17 )
by Jan-Christoph
15:49
created

DashboardController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

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
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020 Julius Härtl <[email protected]>
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Dashboard\Controller;
28
29
use OCA\Dashboard\Service\BackgroundService;
30
use OCA\Files\Event\LoadSidebar;
31
use OCA\Viewer\Event\LoadViewer;
0 ignored issues
show
Bug introduced by
The type OCA\Viewer\Event\LoadViewer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use OCP\AppFramework\Controller;
33
use OCP\AppFramework\Http;
34
use OCP\AppFramework\Http\FileDisplayResponse;
35
use OCP\AppFramework\Http\JSONResponse;
36
use OCP\AppFramework\Http\NotFoundResponse;
37
use OCP\AppFramework\Http\TemplateResponse;
38
use OCP\Dashboard\IManager;
39
use OCP\Dashboard\IWidget;
40
use OCP\Dashboard\RegisterWidgetEvent;
41
use OCP\EventDispatcher\IEventDispatcher;
42
use OCP\IConfig;
43
use OCP\IInitialStateService;
44
use OCP\IRequest;
45
46
class DashboardController extends Controller {
47
48
	/** @var IInitialStateService */
49
	private $inititalStateService;
50
	/** @var IEventDispatcher */
51
	private $eventDispatcher;
52
	/** @var IManager */
53
	private $dashboardManager;
54
	/** @var IConfig */
55
	private $config;
56
	/** @var string */
57
	private $userId;
58
	/**
59
	 * @var BackgroundService
60
	 */
61
	private $backgroundService;
62
63
	public function __construct(
64
		string $appName,
65
		IRequest $request,
66
		IInitialStateService $initialStateService,
67
		IEventDispatcher $eventDispatcher,
68
		IManager $dashboardManager,
69
		IConfig $config,
70
		BackgroundService $backgroundService,
71
		$userId
72
	) {
73
		parent::__construct($appName, $request);
74
75
		$this->inititalStateService = $initialStateService;
76
		$this->eventDispatcher = $eventDispatcher;
77
		$this->dashboardManager = $dashboardManager;
78
		$this->config = $config;
79
		$this->backgroundService = $backgroundService;
80
		$this->userId = $userId;
81
	}
82
83
	/**
84
	 * @NoCSRFRequired
85
	 * @NoAdminRequired
86
	 * @return TemplateResponse
87
	 */
88
	public function index(): TemplateResponse {
89
		\OCP\Util::addStyle('dashboard', 'dashboard');
90
		$this->eventDispatcher->dispatchTyped(new LoadSidebar());
91
		if (class_exists(LoadViewer::class)) {
92
			$this->eventDispatcher->dispatchTyped(new LoadViewer());
93
		}
94
95
		$this->eventDispatcher->dispatchTyped(new RegisterWidgetEvent($this->dashboardManager));
0 ignored issues
show
Deprecated Code introduced by
The class OCP\Dashboard\RegisterWidgetEvent has been deprecated: 20.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
		$this->eventDispatcher->dispatchTyped(/** @scrutinizer ignore-deprecated */ new RegisterWidgetEvent($this->dashboardManager));
Loading history...
96
97
		$userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', 'recommendations,spreed,mail,calendar'));
98
		$widgets = array_map(function (IWidget $widget) {
99
			return [
100
				'id' => $widget->getId(),
101
				'title' => $widget->getTitle(),
102
				'iconClass' => $widget->getIconClass(),
103
				'url' => $widget->getUrl()
104
			];
105
		}, $this->dashboardManager->getWidgets());
106
		$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '{}');
107
		$statuses = json_decode($configStatuses, true);
108
		$this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets);
109
		$this->inititalStateService->provideInitialState('dashboard', 'statuses', $statuses);
110
		$this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout);
111
		$this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
112
		$this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);
113
		$this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'));
114
		$this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0));
115
		$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
116
117
		$response = new TemplateResponse('dashboard', 'index');
118
119
		// For the weather widget we should allow the geolocation
120
		$featurePolicy = new Http\FeaturePolicy();
121
		$featurePolicy->addAllowedGeoLocationDomain('\'self\'');
122
		$response->setFeaturePolicy($featurePolicy);
123
124
		return $response;
125
	}
126
127
	/**
128
	 * @NoAdminRequired
129
	 * @param string $layout
130
	 * @return JSONResponse
131
	 */
132
	public function updateLayout(string $layout): JSONResponse {
133
		$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
134
		return new JSONResponse(['layout' => $layout]);
135
	}
136
137
	/**
138
	 * @NoAdminRequired
139
	 * @param string $statuses
140
	 * @return JSONResponse
141
	 */
142
	public function updateStatuses(string $statuses): JSONResponse {
143
		$this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
144
		return new JSONResponse(['statuses' => $statuses]);
145
	}
146
147
	/**
148
	 * @NoAdminRequired
149
	 */
150
	public function setBackground(string $type = 'default', string $value = ''): JSONResponse {
151
		$currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', '0');
152
		try {
153
			switch ($type) {
154
				case 'shipped':
155
					$this->backgroundService->setShippedBackground($value);
156
					break;
157
				case 'custom':
158
					$this->backgroundService->setFileBackground($value);
159
					break;
160
				case 'color':
161
					$this->backgroundService->setColorBackground($value);
162
					break;
163
				case 'default':
164
					$this->backgroundService->setDefaultBackground();
165
					break;
166
				default:
167
					return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST);
168
			}
169
		} catch (\InvalidArgumentException $e) {
170
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
171
		} catch (\Throwable $e) {
172
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
173
		}
174
		$currentVersion++;
175
		$this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion);
176
		return new JSONResponse([
177
			'type' => $type,
178
			'value' => $value,
179
			'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion)
180
		]);
181
	}
182
183
	/**
184
	 * @NoAdminRequired
185
	 * @NoCSRFRequired
186
	 */
187
	public function getBackground() {
188
		$file = $this->backgroundService->getBackground();
189
		if ($file !== null) {
190
			$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
191
			$response->cacheFor(24 * 60 * 60);
192
			return $response;
193
		}
194
		return new NotFoundResponse();
195
	}
196
}
197