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/BookmarkService.php

103 lines
2.4 KiB
PHP
Raw Permalink 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\BookmarkMapper;
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 BookmarkService extends Service
{
2023-06-16 14:58:23 +00:00
// "bookmark" name to use for the cursor (current reading position)
2023-06-17 23:41:18 +00:00
private const CURSOR = '__CURSOR__';
private const BOOKMARK_TYPE = 'bookmark';
2020-04-21 20:37:42 +00:00
2023-06-16 19:20:03 +00:00
private BookmarkMapper $bookmarkMapper;
2020-04-21 20:37:42 +00:00
2023-08-02 20:04:03 +00:00
public function __construct(BookmarkMapper $bookmarkMapper)
{
2023-06-16 14:58:23 +00:00
parent::__construct($bookmarkMapper);
$this->bookmarkMapper = $bookmarkMapper;
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief get bookmark
*
* bookmark 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(int $fileId, ?string $name = null, ?string $type = null): array
{
2023-06-16 14:58:23 +00:00
$result = $this->bookmarkMapper->get($fileId, $name, $type);
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 bookmark
*
* 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(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity
{
2023-06-16 14:58:23 +00:00
return $this->bookmarkMapper->set($fileId, $name, $value, $type, $content);
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief get cursor (current position in book)
*
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 getCursor(int $fileId): array
{
2023-06-16 19:31:46 +00:00
$result = $this->get($fileId, self::CURSOR);
2023-08-02 20:04:03 +00:00
if (1 === count($result)) {
2023-06-16 14:58:23 +00:00
return $result[0];
}
2023-08-02 20:04:03 +00:00
2023-06-16 19:20:03 +00:00
return [];
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 cursor (current position in book)
*/
2023-08-02 20:04:03 +00:00
public function setCursor(int $fileId, string $value): ReaderEntity
{
2023-06-17 23:41:18 +00:00
return $this->bookmarkMapper->set($fileId, self::CURSOR, $value, self::BOOKMARK_TYPE);
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 bookmark
*/
2023-08-02 20:04:03 +00:00
public function delete(int $fileId, ?string $name = null, ?string $type = null): void
{
2023-06-16 14:58:23 +00:00
foreach ($this->bookmarkMapper->get($fileId, $name, $type) as $bookmark) {
$this->bookmarkMapper->delete($bookmark);
}
}
2020-04-21 20:37:42 +00:00
2023-06-16 14:58:23 +00:00
/**
* @brief delete cursor
*/
2023-08-02 20:04:03 +00:00
public function deleteCursor(int $fileId): void
{
2023-06-17 23:41:18 +00:00
$this->delete($fileId, self::CURSOR, self::BOOKMARK_TYPE);
2023-06-16 14:58:23 +00:00
}
2020-04-21 20:37:42 +00:00
}