Passed
Push — master ( da5844...d3a53d )
by Morris
12:45 queued 10s
created

DashboardController::setBackground()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 27
nc 15
nop 2
dl 0
loc 30
rs 8.5546
c 1
b 0
f 0
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
		$this->inititalStateService->provideInitialState('dashboard', 'panels', $widgets);
107
		$this->inititalStateService->provideInitialState('dashboard', 'layout', $userLayout);
108
		$this->inititalStateService->provideInitialState('dashboard', 'firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
109
		$this->inititalStateService->provideInitialState('dashboard', 'shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);
110
		$this->inititalStateService->provideInitialState('dashboard', 'background', $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default'));
111
		$this->inititalStateService->provideInitialState('dashboard', 'version', $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0));
112
		$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');
113
114
		return new TemplateResponse('dashboard', 'index');
115
	}
116
117
	/**
118
	 * @NoAdminRequired
119
	 * @param string $layout
120
	 * @return JSONResponse
121
	 */
122
	public function updateLayout(string $layout): JSONResponse {
123
		$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
124
		return new JSONResponse(['layout' => $layout]);
125
	}
126
127
	/**
128
	 * @NoAdminRequired
129
	 */
130
	public function setBackground(string $type, string $value): JSONResponse {
131
		$currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0);
132
		try {
133
			switch ($type) {
134
				case 'shipped':
135
					$this->backgroundService->setShippedBackground($value);
136
					break;
137
				case 'custom':
138
					$this->backgroundService->setFileBackground($value);
139
					break;
140
				case 'color':
141
					$this->backgroundService->setColorBackground($value);
142
					break;
143
				case 'default':
144
					$this->backgroundService->setDefaultBackground();
145
					break;
146
				default:
147
					return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST);
148
			}
149
		} catch (\InvalidArgumentException $e) {
150
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
151
		} catch (\Throwable $e) {
152
			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
153
		}
154
		$currentVersion++;
155
		$this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', (string)$currentVersion);
156
		return new JSONResponse([
157
			'type' => $type,
158
			'value' => $value,
159
			'version' => $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion)
160
		]);
161
	}
162
163
	/**
164
	 * @NoAdminRequired
165
	 * @NoCSRFRequired
166
	 */
167
	public function getBackground() {
168
		$file = $this->backgroundService->getBackground();
169
		if ($file !== null) {
170
			$response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]);
171
			$response->cacheFor(24 * 60 * 60);
172
			return $response;
173
		}
174
		return new NotFoundResponse();
175
	}
176
}
177