Skip to content

Commit

Permalink
Merge pull request #926 from nextcloud/dev/config-abstraction
Browse files Browse the repository at this point in the history
Extract the access to the user config in its own helper class
  • Loading branch information
christianlupus authored Mar 23, 2022
2 parents 30a0f04 + 8a1242d commit ee393d6
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 24 deletions.
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 {
$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

0 comments on commit ee393d6

Please sign in to comment.