Skip to content

Commit

Permalink
Extract magic values into constants
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Wolf <github@christianwolf.email>
  • Loading branch information
christianlupus committed Mar 23, 2022
1 parent cf21946 commit 8a1242d
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/Helper/UserConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\Cookbook\Helper;

use OCA\Cookbook\AppInfo\Application;
use OCP\IConfig;
use OCP\IL10N;

Expand Down Expand Up @@ -34,14 +35,19 @@ public function __construct(
$this->l = $l;
}

protected const KEY_LAST_INDEX_UPDATE = 'last_index_update';
protected const KEY_UPDATE_INTERVAL = 'update_interval';
protected const KEY_PRINT_IMAGE = 'print_image';
protected const KEY_FOLDER = 'folder';

/**
* Get a config value from the database
*
* @param string $key The key to get
* @return string The resulting value or '' if the key was not found
*/
private function getRawValue(string $key): string {
return $this->config->getUserValue($this->userId, 'cookbook', $key);
return $this->config->getUserValue($this->userId, Application::APP_ID, $key);
}

/**
Expand All @@ -52,7 +58,7 @@ private function getRawValue(string $key): string {
* @return void
*/
private function setRawValue(string $key, string $value): void {
$this->config->setUserValue($this->userId, 'cookbook', $key, $value);
$this->config->setUserValue($this->userId, Application::APP_ID, $key, $value);
}

/**
Expand All @@ -61,7 +67,7 @@ private function setRawValue(string $key, string $value): void {
* @return integer The timestamp of the last index rebuild
*/
public function getLastIndexUpdate(): int {
$rawValue = $this->getRawValue('last_index_update');
$rawValue = $this->getRawValue(self::KEY_LAST_INDEX_UPDATE);
if ($rawValue === '') {
return 0;
}
Expand All @@ -76,7 +82,7 @@ public function getLastIndexUpdate(): int {
* @return void
*/
public function setLastIndexUpdate(int $value): void {
$this->setRawValue('last_index_update', strval($value));
$this->setRawValue(self::KEY_LAST_INDEX_UPDATE, strval($value));
}

/**
Expand All @@ -85,7 +91,7 @@ public function setLastIndexUpdate(int $value): void {
* @return integer The number of seconds to wait before a new rescan is triggered
*/
public function getUpdateInterval(): int {
$rawValue = $this->getRawValue('update_interval');
$rawValue = $this->getRawValue(self::KEY_UPDATE_INTERVAL);
if ($rawValue === '') {
return 5;
}
Expand All @@ -100,7 +106,7 @@ public function getUpdateInterval(): int {
* @return void
*/
public function setUpdateInterval(int $value): void {
$this->setRawValue('update_interval', $value);
$this->setRawValue(self::KEY_UPDATE_INTERVAL, $value);
}

/**
Expand All @@ -109,7 +115,7 @@ public function setUpdateInterval(int $value): void {
* @return boolean true, if the image should be printed
*/
public function getPrintImage(): bool {
$rawValue = $this->getRawValue('print_image');
$rawValue = $this->getRawValue(self::KEY_PRINT_IMAGE);
if ($rawValue === '') {
return true;
}
Expand All @@ -124,9 +130,9 @@ public function getPrintImage(): bool {
*/
public function setPrintImage(bool $value): void {
if ($value) {
$this->setRawValue('print_image', '1');
$this->setRawValue(self::KEY_PRINT_IMAGE, '1');
} else {
$this->setRawValue('print_image', '0');
$this->setRawValue(self::KEY_PRINT_IMAGE, '0');
}
}

Expand All @@ -138,7 +144,8 @@ public function setPrintImage(bool $value): void {
* @return string The name of the folder within the users files
*/
public function getFolderName(): string {
$rawValue = $this->getRawValue('folder');
$rawValue = $this->getRawValue(self::KEY_FOLDER);

if ($rawValue === '') {
$path = '/' . $this->l->t('Recipes');
$this->setFolderName($path);
Expand All @@ -155,6 +162,6 @@ public function getFolderName(): string {
* @return void
*/
public function setFolderName(string $value): void {
$this->setRawValue('folder', $value);
$this->setRawValue(self::KEY_FOLDER, $value);
}
}

0 comments on commit 8a1242d

Please sign in to comment.