This repository has been archived on 2024-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
epubreader/lib/Service/PreferenceService.php

113 lines
2.8 KiB
PHP
Raw Normal View History

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.
*/
namespace OCA\Epubreader\Service;
2020-04-21 20:37:42 +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
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
public 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
*
* @return array
*/
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(
function ($entity) {
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 14:58:23 +00:00
return $this->get($scope, static::DEFAULTS, $name);
}
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 14:58:23 +00:00
return $this->preferenceMapper->set($scope, static::DEFAULTS, $name, $value);
}
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 14:58:23 +00:00
$this->delete($scope, static::DEFAULTS, $name);
}
2020-04-21 20:37:42 +00:00
}