Skip to content
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 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
[#914](https://github.com/nextcloud/cookbook/pull/914) @MarcelRobitaille
- Replace multiple spaces with a single one when pasting
[#924](https://github.com/nextcloud/cookbook/pull/924) @MarcelRobitaille
- Create abstraction class for access to user configuration
[#926](https://github.com/nextcloud/cookbook/pull/926) @christianlupus

### Documentation
- Introduction about how to start coding
Expand Down
167 changes: 167 additions & 0 deletions lib/Helper/UserConfigHelper.php
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 {
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

$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);
}
}
16 changes: 8 additions & 8 deletions lib/Service/DbCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use OCA\Cookbook\Db\RecipeDb;
use OCP\Files\File;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IConfig;
use OCA\Cookbook\Exception\InvalidJSONFileException;
use OCA\Cookbook\Helper\UserConfigHelper;

class DbCacheService {
private $userId;
Expand All @@ -23,9 +23,9 @@ class DbCacheService {
private $recipeService;

/**
* @var IConfig
* @var UserConfigHelper
*/
private $config;
private $userConfigHelper;

private $jsonFiles;
private $dbReceipeFiles;
Expand All @@ -36,11 +36,11 @@ class DbCacheService {
private $obsoleteRecipes;
private $updatedRecipes;

public function __construct(?string $UserId, RecipeDb $db, RecipeService $recipeService, IConfig $config) {
public function __construct(?string $UserId, RecipeDb $db, RecipeService $recipeService, UserConfigHelper $userConfigHelper) {
$this->userId = $UserId;
$this->db = $db;
$this->recipeService = $recipeService;
$this->config = $config;
$this->userConfigHelper = $userConfigHelper;
}

public function updateCache() {
Expand Down Expand Up @@ -340,14 +340,14 @@ private function updateKeywords() {
* Gets the last time the search index was updated
*/
public function getSearchIndexLastUpdateTime() {
return (int) $this->config->getUserValue($this->userId, 'cookbook', 'last_index_update');
return $this->userConfigHelper->getLastIndexUpdate();
}

/**
* @return int
*/
public function getSearchIndexUpdateInterval(): int {
$interval = (int)$this->config->getUserValue($this->userId, 'cookbook', 'update_interval');
$interval = $this->userConfigHelper->getUpdateInterval();

if ($interval < 1) {
$interval = 5;
Expand All @@ -373,7 +373,7 @@ private function checkSearchIndexUpdate() {
$this->updateCache();

// Cache the last index update
$this->config->setUserValue($this->userId, 'cookbook', 'last_index_update', time());
$this->userConfigHelper->setLastIndexUpdate(time());

// TODO Make triggers more general, need refactoring of *all* Services
$this->recipeService->updateSearchIndex();
Expand Down
29 changes: 13 additions & 16 deletions lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Image;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\File;
Expand All @@ -16,6 +15,7 @@
use Psr\Log\LoggerInterface;
use OCA\Cookbook\Exception\UserFolderNotWritableException;
use OCA\Cookbook\Exception\RecipeExistsException;
use OCA\Cookbook\Helper\UserConfigHelper;

/**
* Main service class for the cookbook app.
Expand All @@ -26,17 +26,21 @@ class RecipeService {
private $root;
private $user_id;
private $db;
private $config;
private $il10n;
private $logger;

public function __construct(?string $UserId, IRootFolder $root, RecipeDb $db, IConfig $config, IL10N $il10n, LoggerInterface $logger) {
/**
* @var UserConfigHelper
*/
private $userConfigHelper;

public function __construct(?string $UserId, IRootFolder $root, RecipeDb $db, UserConfigHelper $userConfigHelper, IL10N $il10n, LoggerInterface $logger) {
$this->user_id = $UserId;
$this->root = $root;
$this->db = $db;
$this->config = $config;
$this->il10n = $il10n;
$this->logger = $logger;
$this->userConfigHelper = $userConfigHelper;
}

/**
Expand Down Expand Up @@ -1020,29 +1024,22 @@ public function findRecipesInSearchIndex($keywords_string): array {
* @param string $path
*/
public function setUserFolderPath(string $path) {
$this->config->setUserValue($this->user_id, 'cookbook', 'folder', $path);
$this->userConfigHelper->setFolderName($path);
}

/**
* @return string
*/
public function getUserFolderPath() {
$path = $this->config->getUserValue($this->user_id, 'cookbook', 'folder');

if (!$path) {
$path = '/' . $this->il10n->t('Recipes');
$this->config->setUserValue($this->user_id, 'cookbook', 'folder', $path);
}

return $path;
return $this->userConfigHelper->getFolderName();
}

/**
* @param int $interval
* @throws PreConditionNotMetException
*/
public function setSearchIndexUpdateInterval(int $interval) {
$this->config->setUserValue($this->user_id, 'cookbook', 'update_interval', $interval);
$this->userConfigHelper->setUpdateInterval($interval);
}

/**
Expand All @@ -1060,15 +1057,15 @@ public function getFolderForUser() {
* @throws PreConditionNotMetException
*/
public function setPrintImage(bool $printImage) {
$this->config->setUserValue($this->user_id, 'cookbook', 'print_image', (int) $printImage);
$this->userConfigHelper->setPrintImage($printImage);
}

/**
* Should image be printed with the recipe
* @return bool
*/
public function getPrintImage() {
return (bool) $this->config->getUserValue($this->user_id, 'cookbook', 'print_image');
return $this->userConfigHelper->getPrintImage();
}

/**
Expand Down
Loading