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

98 lines
2.4 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
2023-06-16 19:31:46 +00:00
/**
* @psalm-import-type SerializedEntity from ReaderEntity
*/
2023-08-02 20:04:03 +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-08-02 20:04:03 +00:00
public function __construct(PreferenceMapper $preferenceMapper)
{
2023-06-16 14:58:23 +00:00
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
*
2023-08-02 20:04:03 +00:00
* @return SerializedEntity
2023-06-16 14:58:23 +00:00
*/
2023-08-02 20:04: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);
2023-08-02 20:04:03 +00:00
2023-06-16 14:58:23 +00:00
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();
2023-08-02 20:04:03 +00:00
},
$result
);
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 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
*/
2023-08-02 20:04: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
*/
2023-08-02 20:04: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
*/
2023-08-02 20:04:03 +00:00
public function setDefault(string $scope, string $name, string $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
*/
2023-08-02 20:04:03 +00:00
public function delete(string $scope, int $fileId, ?string $name = null): void
{
foreach ($this->preferenceMapper->get($scope, $fileId, $name) as $preference) {
2023-06-16 14:58:23 +00:00
$this->preferenceMapper->delete($preference);
}
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief delete default
*/
2023-08-02 20:04: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
}