diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-28 22:50:45 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-28 22:50:45 +0200 |
commit | 9949bcf7a08a436bc0c54b43dd94214025086eb0 (patch) | |
tree | f21a7122d9f247c033af95da34d27eeedba49f78 /apps/theming/lib | |
parent | a0b2297c536923867ed3f8e489dabe8a278ac0be (diff) | |
download | nextcloud-server-9949bcf7a08a436bc0c54b43dd94214025086eb0.tar.gz nextcloud-server-9949bcf7a08a436bc0c54b43dd94214025086eb0.zip |
refactor(theming): Reduce duplicated code in `BackgroundService`refactor/background-service
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'apps/theming/lib')
-rw-r--r-- | apps/theming/lib/Service/BackgroundService.php | 62 |
1 files changed, 28 insertions, 34 deletions
diff --git a/apps/theming/lib/Service/BackgroundService.php b/apps/theming/lib/Service/BackgroundService.php index bf650874448..057dda2e20e 100644 --- a/apps/theming/lib/Service/BackgroundService.php +++ b/apps/theming/lib/Service/BackgroundService.php @@ -206,10 +206,7 @@ class BackgroundService { } public function setDefaultBackground(?string $userId = null): void { - $userId = $userId ?? $this->userId; - if ($userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + $userId = $userId ?? $this->getUserId(); $this->config->deleteUserValue($userId, Application::APP_ID, 'background_image'); $this->config->deleteUserValue($userId, Application::APP_ID, 'background_color'); @@ -224,11 +221,9 @@ class BackgroundService { * @throws PreConditionNotMetException * @throws NoUserException */ - public function setFileBackground($path): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } - $userFolder = $this->rootFolder->getUserFolder($this->userId); + public function setFileBackground(string $path, ?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + $userFolder = $this->rootFolder->getUserFolder($userId); /** @var File $file */ $file = $userFolder->get($path); @@ -242,10 +237,7 @@ class BackgroundService { } public function recalculateMeanColor(?string $userId = null): void { - $userId = $userId ?? $this->userId; - if ($userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + $userId = $userId ?? $this->getUserId(); $image = new \OCP\Image(); $handle = $this->getAppDataFolder($userId)->getFile('background.jpg')->read(); @@ -268,10 +260,8 @@ class BackgroundService { * @throws InvalidArgumentException If the specified filename does not match any shipped background */ public function setShippedBackground(string $filename, ?string $userId = null): void { - $userId = $userId ?? $this->userId; - if ($userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + $userId = $userId ?? $this->getUserId(); + if (!array_key_exists($filename, self::SHIPPED_BACKGROUNDS)) { throw new InvalidArgumentException('The given file name is invalid'); } @@ -285,10 +275,8 @@ class BackgroundService { * @param string|null $userId The user to set the color - default to current logged-in user */ public function setColorBackground(string $color, ?string $userId = null): void { - $userId = $userId ?? $this->userId; - if ($userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + $userId = $userId ?? $this->getUserId(); + if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) { throw new InvalidArgumentException('The given color is invalid'); } @@ -296,15 +284,14 @@ class BackgroundService { $this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); } - public function deleteBackgroundImage(): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); + public function deleteBackgroundImage(?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + $this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); } - public function getBackground(): ?ISimpleFile { - $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT); + public function getBackground(?string $userId = null): ?ISimpleFile { + $userId = $userId ?? $this->getUserId(); + $background = $this->config->getUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT); if ($background === self::BACKGROUND_CUSTOM) { try { return $this->getAppDataFolder()->getFile('background.jpg'); @@ -398,20 +385,27 @@ class BackgroundService { * @throws NotPermittedException */ private function getAppDataFolder(?string $userId = null): ISimpleFolder { - $userId = $userId ?? $this->userId; - if ($userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + $userId = $userId ?? $this->getUserId(); try { $rootFolder = $this->appData->getFolder('users'); - } catch (NotFoundException $e) { + } catch (NotFoundException) { $rootFolder = $this->appData->newFolder('users'); } try { return $rootFolder->getFolder($userId); - } catch (NotFoundException $e) { + } catch (NotFoundException) { return $rootFolder->newFolder($userId); } } + + /** + * @throws RuntimeException Thrown if a method that needs a user is called without any logged-in user + */ + private function getUserId(): string { + if ($this->userId === null) { + throw new RuntimeException('No currently logged-in user'); + } + return $this->userId; + } } |