-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extract the access to the user config in its own helper class #926
Changes from 4 commits
d8129b5
a419af4
d993f44
00ecbd2
cf21946
8a1242d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace OCA\Cookbook\Helper; | ||
|
||
use OCP\IConfig; | ||
use OCP\IL10N; | ||
|
||
/** | ||
* This class allows access to the per-user configuration of the app | ||
*/ | ||
class UserConfigHelper { | ||
/** | ||
* @var string | ||
*/ | ||
private $userId; | ||
|
||
/** | ||
* @var IConfig | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var IL10N | ||
*/ | ||
private $l; | ||
|
||
public function __construct( | ||
string $UserId, | ||
IConfig $config, | ||
IL10N $l | ||
) { | ||
$this->userId = $UserId; | ||
$this->config = $config; | ||
$this->l = $l; | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Set a config value in the database | ||
* | ||
* @param string $key The key of the configuration | ||
* @param string $value The value of the config entry | ||
* @return void | ||
*/ | ||
private function setRawValue(string $key, string $value): void { | ||
$this->config->setUserValue($this->userId, 'cookbook', $key, $value); | ||
} | ||
|
||
/** | ||
* Get the timestamp of the last rescan of the library | ||
* | ||
* @return integer The timestamp of the last index rebuild | ||
*/ | ||
public function getLastIndexUpdate(): int { | ||
$rawValue = $this->getRawValue('last_index_update'); | ||
if ($rawValue === '') { | ||
return 0; | ||
} | ||
|
||
return intval($rawValue); | ||
} | ||
|
||
/** | ||
* Set the timestamp of the last rescan of the library | ||
* | ||
* @param integer $value The timestamp of the last index rebuild | ||
* @return void | ||
*/ | ||
public function setLastIndexUpdate(int $value): void { | ||
$this->setRawValue('last_index_update', strval($value)); | ||
} | ||
|
||
/** | ||
* Get the number of seconds between rescans of the library | ||
* | ||
* @return integer The number of seconds to wait before a new rescan is triggered | ||
*/ | ||
public function getUpdateInterval(): int { | ||
$rawValue = $this->getRawValue('update_interval'); | ||
if ($rawValue === '') { | ||
return 5; | ||
} | ||
|
||
return intval($rawValue); | ||
} | ||
|
||
/** | ||
* Set the interval between the rescan events of the complete library | ||
* | ||
* @param integer $value The number of seconds to wait at least between rescans | ||
* @return void | ||
*/ | ||
public function setUpdateInterval(int $value): void { | ||
$this->setRawValue('update_interval', $value); | ||
} | ||
|
||
/** | ||
* Check if the primary imgae should be printed or not | ||
* | ||
* @return boolean true, if the imgae shoudl be printed | ||
christianlupus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function getPrintImage(): bool { | ||
$rawValue = $this->getRawValue('print_image'); | ||
if ($rawValue === '') { | ||
return true; | ||
} | ||
return $rawValue === '1'; | ||
} | ||
|
||
/** | ||
* Set if the imgae should be printed | ||
christianlupus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param boolean $value true if the image should be printed | ||
* @return void | ||
*/ | ||
public function setPrintImage(bool $value): void { | ||
if ($value) { | ||
$this->setRawValue('print_image', '1'); | ||
} else { | ||
$this->setRawValue('print_image', '0'); | ||
} | ||
} | ||
|
||
/** | ||
* Get the name of the default cookbook. | ||
* | ||
* If no folder is stored in the config yet, a default setting will be generated and saved. | ||
* | ||
* @return string The name of the folder within the users files | ||
*/ | ||
public function getFolderName(): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we think of having multiple cookbooks here? As discussed in #340 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly. For now, I wanted to create something like a DBAL. Extending this and returning an array of strings (and storing appropriately), is just a small change in this class plus the corresponding upper layers. These upper layers I wanted to generate some abstractions as well. So the switch from one to many cookbooks should be a smaller one. When separating file system access from business logic, we can work towards the discussed results in #340 in the classes there. |
||
$rawValue = $this->getRawValue('folder'); | ||
if ($rawValue === '') { | ||
$path = '/' . $this->l->t('Recipes'); | ||
$this->setFolderName($path); | ||
return $path; | ||
} | ||
|
||
return $rawValue; | ||
} | ||
|
||
/** | ||
* Set the folder for the user's cookbook. | ||
* | ||
* @param string $value The name of the folder withon the user's files | ||
christianlupus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @return void | ||
*/ | ||
public function setFolderName(string $value): void { | ||
$this->setRawValue('folder', $value); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a sensible way to get rid of those magic strings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was exactly the reason for introducing this class. Until now, these magic strings were distributed in the complete code. Now, they are at least condensed into a single file/class.
One could at most introduce constants of the class to use them. But the effect would be similar. The thing is that the server provides just a string based key-value interface. We could as well store one JSON object there. (Now easier as all is contained in the class)