Skip to content

Commit

Permalink
Merge pull request #48440 from nextcloud/refactor/background-service
Browse files Browse the repository at this point in the history
refactor(theming): Reduce duplicated code in `BackgroundService`
  • Loading branch information
susnux authored Oct 3, 2024
2 parents 8638a89 + 9949bcf commit 94c5294
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions apps/theming/lib/Service/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ public function __construct(
}

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');
Expand All @@ -226,11 +223,9 @@ public function setDefaultBackground(?string $userId = null): void {
* @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);
Expand All @@ -244,10 +239,7 @@ public function setFileBackground($path): void {
}

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();
Expand All @@ -270,10 +262,8 @@ public function recalculateMeanColor(?string $userId = null): void {
* @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');
}
Expand All @@ -287,26 +277,23 @@ public function setShippedBackground(string $filename, ?string $userId = null):
* @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');
}
$this->config->setUserValue($userId, Application::APP_ID, 'background_color', $color);
$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');
Expand Down Expand Up @@ -400,20 +387,27 @@ function toHex(int $channel): string {
* @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;
}
}

0 comments on commit 94c5294

Please sign in to comment.