-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8129b5
Create config abstraction for user configurations
christianlupus a419af4
Replaced user config by class access
christianlupus d993f44
Update changelog
christianlupus 00ecbd2
Fix type hint for PHP 7.3
christianlupus cf21946
Apply suggestions from code review
christianlupus 8a1242d
Extract magic values into constants
christianlupus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
namespace OCA\Cookbook\Helper; | ||
|
||
use OCA\Cookbook\AppInfo\Application; | ||
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; | ||
} | ||
|
||
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, Application::APP_ID, $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, Application::APP_ID, $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(self::KEY_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(self::KEY_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(self::KEY_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(self::KEY_UPDATE_INTERVAL, $value); | ||
} | ||
|
||
/** | ||
* Check if the primary imgae should be printed or not | ||
* | ||
* @return boolean true, if the image should be printed | ||
*/ | ||
public function getPrintImage(): bool { | ||
$rawValue = $this->getRawValue(self::KEY_PRINT_IMAGE); | ||
if ($rawValue === '') { | ||
return true; | ||
} | ||
return $rawValue === '1'; | ||
} | ||
|
||
/** | ||
* Set if the image should be printed | ||
* | ||
* @param boolean $value true if the image should be printed | ||
* @return void | ||
*/ | ||
public function setPrintImage(bool $value): void { | ||
if ($value) { | ||
$this->setRawValue(self::KEY_PRINT_IMAGE, '1'); | ||
} else { | ||
$this->setRawValue(self::KEY_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 { | ||
$rawValue = $this->getRawValue(self::KEY_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 within the user's files | ||
* @return void | ||
*/ | ||
public function setFolderName(string $value): void { | ||
$this->setRawValue(self::KEY_FOLDER, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we think of having multiple cookbooks here? As discussed in #340
You probably just want to provide the same functionality as the current code but in a nicer package?
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.
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.