From 9a7f7ea5e7181a09f3f2feb8f3dcd0ec4f21134f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Tue, 18 Aug 2020 20:40:19 +0200
Subject: [PATCH] Fix php cs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 .../lib/Controller/DashboardController.php    |  7 ++--
 .../lib/Service/BackgroundService.php         | 35 +++++++++++++------
 2 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/apps/dashboard/lib/Controller/DashboardController.php b/apps/dashboard/lib/Controller/DashboardController.php
index 95656b260e8..5d9f886f0c1 100644
--- a/apps/dashboard/lib/Controller/DashboardController.php
+++ b/apps/dashboard/lib/Controller/DashboardController.php
@@ -39,7 +39,6 @@ use OCP\Dashboard\IManager;
 use OCP\Dashboard\IWidget;
 use OCP\Dashboard\RegisterWidgetEvent;
 use OCP\EventDispatcher\IEventDispatcher;
-use OCP\Files\NotFoundException;
 use OCP\IConfig;
 use OCP\IInitialStateService;
 use OCP\IRequest;
@@ -128,8 +127,8 @@ class DashboardController extends Controller {
 	/**
 	 * @NoAdminRequired
 	 */
-	public function setBackground(string $type = 'default', $value): JSONResponse {
-		$currentVersion = $this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0);
+	public function setBackground(string $type, string $value): JSONResponse {
+		$currentVersion = (int)$this->config->getUserValue($this->userId, 'dashboard', 'backgroundVersion', 0);
 		try {
 			switch ($type) {
 				case 'shipped':
@@ -149,6 +148,8 @@ class DashboardController extends Controller {
 			}
 		} catch (\InvalidArgumentException $e) {
 			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
+		} catch (\Throwable $e) {
+			return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
 		}
 		$currentVersion++;
 		$this->config->setUserValue($this->userId, 'dashboard', 'backgroundVersion', $currentVersion);
diff --git a/apps/dashboard/lib/Service/BackgroundService.php b/apps/dashboard/lib/Service/BackgroundService.php
index da0de6117e6..ab5f58b6cb7 100644
--- a/apps/dashboard/lib/Service/BackgroundService.php
+++ b/apps/dashboard/lib/Service/BackgroundService.php
@@ -26,8 +26,6 @@ declare(strict_types=1);
 
 namespace OCA\Dashboard\Service;
 
-
-use OCP\Files\File;
 use OCP\Files\IAppData;
 use OCP\Files\IRootFolder;
 use OCP\Files\NotFoundException;
@@ -35,7 +33,6 @@ use OCP\Files\SimpleFS\ISimpleFile;
 use OCP\IConfig;
 
 class BackgroundService {
-
 	public const THEMING_MODE_DARK = 'dark';
 
 	public const SHIPPED_BACKGROUNDS = [
@@ -81,6 +78,19 @@ class BackgroundService {
 			'attribution_url' => '',
 		]
 	];
+	/**
+	 * @var \OCP\Files\Folder
+	 */
+	private $userFolder;
+	/**
+	 * @var \OCP\Files\SimpleFS\ISimpleFolder
+	 */
+	private $dashboardUserFolder;
+	/**
+	 * @var IConfig
+	 */
+	private $config;
+	private $userId;
 
 	public function __construct(IRootFolder $rootFolder, IAppData $appData, IConfig $config, $userId) {
 		if ($userId === null) {
@@ -96,31 +106,37 @@ class BackgroundService {
 		$this->userId = $userId;
 	}
 
-	public function setDefaultBackground() {
+	public function setDefaultBackground(): void {
 		$this->config->deleteUserValue($this->userId, 'dashboard', 'background');
 	}
 
-	public function setFileBackground($path) {
+	/**
+	 * @param $path
+	 * @throws NotFoundException
+	 * @throws \OCP\Files\NotPermittedException
+	 * @throws \OCP\PreConditionNotMetException
+	 */
+	public function setFileBackground($path): void {
 		$this->config->setUserValue($this->userId, 'dashboard', 'background', 'custom');
 		$file = $this->userFolder->get($path);
-		$newFile = $this->dashboardUserFolder->newFile('background.jpg', $file->fopen('r'));
+		$this->dashboardUserFolder->newFile('background.jpg', $file->fopen('r'));
 	}
 
-	public function setShippedBackground($fileName) {
+	public function setShippedBackground($fileName): void {
 		if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
 			throw new \InvalidArgumentException('The given file name is invalid');
 		}
 		$this->config->setUserValue($this->userId, 'dashboard', 'background', $fileName);
 	}
 
-	public function setColorBackground(string $color) {
+	public function setColorBackground(string $color): void {
 		if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
 			throw new \InvalidArgumentException('The given color is invalid');
 		}
 		$this->config->setUserValue($this->userId, 'dashboard', 'background', $color);
 	}
 
-	public function getBackground() {
+	public function getBackground(): ?ISimpleFile {
 		$background = $this->config->getUserValue($this->userId, 'dashboard', 'background', 'default');
 		if ($background === 'custom') {
 			try {
@@ -130,5 +146,4 @@ class BackgroundService {
 		}
 		return null;
 	}
-
 }
-- 
GitLab