Skip to content

Commit 94c5294

Browse files
authored
Merge pull request #48440 from nextcloud/refactor/background-service
refactor(theming): Reduce duplicated code in `BackgroundService`
2 parents 8638a89 + 9949bcf commit 94c5294

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

apps/theming/lib/Service/BackgroundService.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ public function __construct(
208208
}
209209

210210
public function setDefaultBackground(?string $userId = null): void {
211-
$userId = $userId ?? $this->userId;
212-
if ($userId === null) {
213-
throw new RuntimeException('No currently logged-in user');
214-
}
211+
$userId = $userId ?? $this->getUserId();
215212

216213
$this->config->deleteUserValue($userId, Application::APP_ID, 'background_image');
217214
$this->config->deleteUserValue($userId, Application::APP_ID, 'background_color');
@@ -226,11 +223,9 @@ public function setDefaultBackground(?string $userId = null): void {
226223
* @throws PreConditionNotMetException
227224
* @throws NoUserException
228225
*/
229-
public function setFileBackground($path): void {
230-
if ($this->userId === null) {
231-
throw new RuntimeException('No currently logged-in user');
232-
}
233-
$userFolder = $this->rootFolder->getUserFolder($this->userId);
226+
public function setFileBackground(string $path, ?string $userId = null): void {
227+
$userId = $userId ?? $this->getUserId();
228+
$userFolder = $this->rootFolder->getUserFolder($userId);
234229

235230
/** @var File $file */
236231
$file = $userFolder->get($path);
@@ -244,10 +239,7 @@ public function setFileBackground($path): void {
244239
}
245240

246241
public function recalculateMeanColor(?string $userId = null): void {
247-
$userId = $userId ?? $this->userId;
248-
if ($userId === null) {
249-
throw new RuntimeException('No currently logged-in user');
250-
}
242+
$userId = $userId ?? $this->getUserId();
251243

252244
$image = new \OCP\Image();
253245
$handle = $this->getAppDataFolder($userId)->getFile('background.jpg')->read();
@@ -270,10 +262,8 @@ public function recalculateMeanColor(?string $userId = null): void {
270262
* @throws InvalidArgumentException If the specified filename does not match any shipped background
271263
*/
272264
public function setShippedBackground(string $filename, ?string $userId = null): void {
273-
$userId = $userId ?? $this->userId;
274-
if ($userId === null) {
275-
throw new RuntimeException('No currently logged-in user');
276-
}
265+
$userId = $userId ?? $this->getUserId();
266+
277267
if (!array_key_exists($filename, self::SHIPPED_BACKGROUNDS)) {
278268
throw new InvalidArgumentException('The given file name is invalid');
279269
}
@@ -287,26 +277,23 @@ public function setShippedBackground(string $filename, ?string $userId = null):
287277
* @param string|null $userId The user to set the color - default to current logged-in user
288278
*/
289279
public function setColorBackground(string $color, ?string $userId = null): void {
290-
$userId = $userId ?? $this->userId;
291-
if ($userId === null) {
292-
throw new RuntimeException('No currently logged-in user');
293-
}
280+
$userId = $userId ?? $this->getUserId();
281+
294282
if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
295283
throw new InvalidArgumentException('The given color is invalid');
296284
}
297285
$this->config->setUserValue($userId, Application::APP_ID, 'background_color', $color);
298286
$this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR);
299287
}
300288

301-
public function deleteBackgroundImage(): void {
302-
if ($this->userId === null) {
303-
throw new RuntimeException('No currently logged-in user');
304-
}
305-
$this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR);
289+
public function deleteBackgroundImage(?string $userId = null): void {
290+
$userId = $userId ?? $this->getUserId();
291+
$this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR);
306292
}
307293

308-
public function getBackground(): ?ISimpleFile {
309-
$background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT);
294+
public function getBackground(?string $userId = null): ?ISimpleFile {
295+
$userId = $userId ?? $this->getUserId();
296+
$background = $this->config->getUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT);
310297
if ($background === self::BACKGROUND_CUSTOM) {
311298
try {
312299
return $this->getAppDataFolder()->getFile('background.jpg');
@@ -400,20 +387,27 @@ function toHex(int $channel): string {
400387
* @throws NotPermittedException
401388
*/
402389
private function getAppDataFolder(?string $userId = null): ISimpleFolder {
403-
$userId = $userId ?? $this->userId;
404-
if ($userId === null) {
405-
throw new RuntimeException('No currently logged-in user');
406-
}
390+
$userId = $userId ?? $this->getUserId();
407391

408392
try {
409393
$rootFolder = $this->appData->getFolder('users');
410-
} catch (NotFoundException $e) {
394+
} catch (NotFoundException) {
411395
$rootFolder = $this->appData->newFolder('users');
412396
}
413397
try {
414398
return $rootFolder->getFolder($userId);
415-
} catch (NotFoundException $e) {
399+
} catch (NotFoundException) {
416400
return $rootFolder->newFolder($userId);
417401
}
418402
}
403+
404+
/**
405+
* @throws RuntimeException Thrown if a method that needs a user is called without any logged-in user
406+
*/
407+
private function getUserId(): string {
408+
if ($this->userId === null) {
409+
throw new RuntimeException('No currently logged-in user');
410+
}
411+
return $this->userId;
412+
}
419413
}

0 commit comments

Comments
 (0)