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
Michel Roux 7da83b2595
All checks were successful
epubreader / nextcloud-22 (push) Successful in 1m4s
epubreader / nextcloud-27 (push) Successful in 56s
cleanup (again)
2023-08-02 22:04:03 +02:00

98 lines
2.4 KiB
PHP

<?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;
use OCA\Epubreader\Db\PreferenceMapper;
use OCA\Epubreader\Db\ReaderEntity;
/**
* @psalm-import-type SerializedEntity from ReaderEntity
*/
class PreferenceService extends Service
{
// (ab)use the fact that $fileId never goes below 1 by using the
// value 0 to indicate a default preference
private const DEFAULTS = 0;
private PreferenceMapper $preferenceMapper;
public function __construct(PreferenceMapper $preferenceMapper)
{
parent::__construct($preferenceMapper);
$this->preferenceMapper = $preferenceMapper;
}
/**
* @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
*
* @return SerializedEntity
*/
public function get(string $scope, int $fileId, ?string $name = null): array
{
$result = $this->preferenceMapper->get($scope, $fileId, $name);
return array_map(
function (ReaderEntity $entity): array {
return $entity->toService();
},
$result
);
}
/**
* @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
*/
public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity
{
return $this->preferenceMapper->set($scope, $fileId, $name, $value);
}
/**
* @brief get default preference
*/
public function getDefault(string $scope, ?string $name = null): array
{
return $this->get($scope, self::DEFAULTS, $name);
}
/**
* @brief set default preference
*/
public function setDefault(string $scope, string $name, string $value): ReaderEntity
{
return $this->preferenceMapper->set($scope, self::DEFAULTS, $name, $value);
}
/**
* @brief delete preference
*/
public function delete(string $scope, int $fileId, ?string $name = null): void
{
foreach ($this->preferenceMapper->get($scope, $fileId, $name) as $preference) {
$this->preferenceMapper->delete($preference);
}
}
/**
* @brief delete default
*/
public function deleteDefault(string $scope, ?string $name = null): void
{
$this->delete($scope, self::DEFAULTS, $name);
}
}