2020-04-21 20:37:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Frank de Lange
|
|
|
|
* @copyright 2017 Frank de Lange
|
|
|
|
*
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 21:45:26 +00:00
|
|
|
namespace OCA\Epubreader\Service;
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2020-04-21 21:45:26 +00:00
|
|
|
use OCA\Epubreader\Db\PreferenceMapper;
|
2023-06-16 19:20:03 +00:00
|
|
|
use OCA\Epubreader\Db\ReaderEntity;
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 19:31:46 +00:00
|
|
|
/**
|
|
|
|
* @psalm-import-type SerializedEntity from ReaderEntity
|
|
|
|
*/
|
2020-04-21 20:37:42 +00:00
|
|
|
class PreferenceService extends Service {
|
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
// (ab)use the fact that $fileId never goes below 1 by using the
|
|
|
|
// value 0 to indicate a default preference
|
2023-06-17 23:41:18 +00:00
|
|
|
private const DEFAULTS = 0;
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 19:20:03 +00:00
|
|
|
private PreferenceMapper $preferenceMapper;
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @param PreferenceMapper $preferenceMapper
|
|
|
|
*/
|
|
|
|
public function __construct(PreferenceMapper $preferenceMapper) {
|
|
|
|
parent::__construct($preferenceMapper);
|
|
|
|
$this->preferenceMapper = $preferenceMapper;
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief get preference
|
|
|
|
*
|
|
|
|
* scope identifies preference source, i.e. which renderer the preference applies to
|
|
|
|
* preference type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc
|
|
|
|
*
|
|
|
|
* @param string $scope
|
|
|
|
* @param int $fileId
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param ?string $name
|
2023-06-16 14:58:23 +00:00
|
|
|
*
|
2023-06-16 19:31:46 +00:00
|
|
|
* @psalm-return SerializedEntity
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function get(string $scope, int $fileId, ?string $name = null): array {
|
2023-06-16 14:58:23 +00:00
|
|
|
$result = $this->preferenceMapper->get($scope, $fileId, $name);
|
|
|
|
return array_map(
|
2023-06-16 19:31:46 +00:00
|
|
|
function (ReaderEntity $entity): array {
|
2023-06-16 14:58:23 +00:00
|
|
|
return $entity->toService();
|
|
|
|
}, $result);
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief write preference
|
|
|
|
*
|
|
|
|
* scope identifies preference source, i.e. which renderer the preference applies to
|
|
|
|
* position type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc
|
|
|
|
*
|
|
|
|
* @param string $scope
|
|
|
|
* @param int $fileId
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity {
|
2023-06-16 14:58:23 +00:00
|
|
|
return $this->preferenceMapper->set($scope, $fileId, $name, $value);
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief get default preference
|
|
|
|
*
|
|
|
|
* @param string $scope
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param ?string $name
|
2023-06-16 14:58:23 +00:00
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function getDefault(string $scope, ?string $name = null): array {
|
2023-06-16 19:31:46 +00:00
|
|
|
return $this->get($scope, self::DEFAULTS, $name);
|
2023-06-16 14:58:23 +00:00
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief set default preference
|
|
|
|
*
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function setDefault($scope, $name, $value): ReaderEntity {
|
2023-06-16 19:31:46 +00:00
|
|
|
return $this->preferenceMapper->set($scope, self::DEFAULTS, $name, $value);
|
2023-06-16 14:58:23 +00:00
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief delete preference
|
|
|
|
*
|
|
|
|
* @param string $scope
|
|
|
|
* @param int $fileId
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param ?string $name
|
2023-06-16 14:58:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function delete(string $scope, int $fileId, ?string $name = null): void {
|
2023-06-16 14:58:23 +00:00
|
|
|
foreach($this->preferenceMapper->get($scope, $fileId, $name) as $preference) {
|
|
|
|
$this->preferenceMapper->delete($preference);
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
|
2023-06-16 14:58:23 +00:00
|
|
|
/**
|
|
|
|
* @brief delete default
|
|
|
|
*
|
|
|
|
* @param string $scope
|
2023-06-16 19:20:03 +00:00
|
|
|
* @param ?string $name
|
2023-06-16 14:58:23 +00:00
|
|
|
*
|
|
|
|
*/
|
2023-06-16 19:20:03 +00:00
|
|
|
public function deleteDefault(string $scope, ?string $name = null): void {
|
2023-06-16 19:31:46 +00:00
|
|
|
$this->delete($scope, self::DEFAULTS, $name);
|
2023-06-16 14:58:23 +00:00
|
|
|
}
|
2020-04-21 20:37:42 +00:00
|
|
|
}
|