diff --git a/appinfo/routes.php b/appinfo/routes.php index 30803f9..27cdf81 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -23,10 +23,6 @@ return ['routes' => [ ['name' => 'bookmark#set', 'url' => '/bookmark', 'verb' => 'POST'], ['name' => 'bookmark#delete', 'url' => '/bookmark/{fileId}/{name}', 'verb' => 'DELETE'], - // Metadata - ['name' => 'metadata#get', 'url' => '/metadata/{fileId}/{name}', 'verb' => 'GET', 'defaults' => ['name' => '']], - ['name' => 'metadata#set', 'url' => '/metadata/{fileId}/{name}/{value}', 'verb' => 'POST'], - // Preferences ['name' => 'preference#get_default', 'url' => '/preference/default/{scope}/{name}', 'verb' => 'GET', 'defaults' => ['name' => '']], ['name' => 'preference#set_default', 'url' => '/preference/default', 'verb' => 'POST'], @@ -34,7 +30,7 @@ return ['routes' => [ ['name' => 'preference#get', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'GET', 'defaults' => ['name' => '']], ['name' => 'preference#set', 'url' => '/preference', 'verb' => 'POST'], ['name' => 'preference#delete', 'url' => '/preference/{fileId}/{scope}/{name}', 'verb' => 'DELETE'], - + // User Settings ['name' => 'settings#setPreference', 'url' => '/settings/set', 'verb' => 'POST'], ]]; diff --git a/lib/Controller/BookmarkController.php b/lib/Controller/BookmarkController.php index f4a2d86..c608f10 100644 --- a/lib/Controller/BookmarkController.php +++ b/lib/Controller/BookmarkController.php @@ -12,23 +12,23 @@ namespace OCA\Epubreader\Controller; use OCA\Epubreader\Service\BookmarkService; use OCP\AppFramework\Controller; - - +use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; class BookmarkController extends Controller { - private $bookmarkService; + private BookmarkService $bookmarkService; /** * @param string $AppName * @param IRequest $request * @param BookmarkService $bookmarkService */ - public function __construct($AppName, + public function __construct( + string $AppName, IRequest $request, - BookmarkService $bookmarkService) { - + BookmarkService $bookmarkService + ) { parent::__construct($AppName, $request); $this->bookmarkService = $bookmarkService; } @@ -40,12 +40,11 @@ class BookmarkController extends Controller { * @NoCSRFRequired * * @param int $fileId - * @param string $name - * - * @return array|\OCP\AppFramework\Http\JSONResponse + * @param ?string $name + * @param ?string $type */ - public function get($fileId, $name, $type = null) { - return $this->bookmarkService->get($fileId, $name, $type); + public function get(int $fileId, ?string $name = null, ?string $type = null): JSONResponse { + return new JSONResponse($this->bookmarkService->get($fileId, $name, $type)); } /** @@ -57,14 +56,13 @@ class BookmarkController extends Controller { * @param int $fileId * @param string $name * @param string $value - * - * @return array|\OCP\AppFramework\Http\JSONResponse + * @param ?string $type + * @param ?string $content */ - public function set($fileId, $name, $value, $type = null, $content = null) { - return $this->bookmarkService->set($fileId, $name, $value, $type, $content); + public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): JSONResponse { + return new JSONResponse($this->bookmarkService->set($fileId, $name, $value, $type, $content)); } - /** * @brief return cursor for $fileId * @@ -72,11 +70,9 @@ class BookmarkController extends Controller { * @NoCSRFRequired * * @param int $fileId - * - * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function getCursor($fileId) { - return $this->bookmarkService->getCursor($fileId); + public function getCursor(int $fileId): JSONResponse { + return new JSONResponse($this->bookmarkService->getCursor($fileId)); } /** @@ -87,11 +83,9 @@ class BookmarkController extends Controller { * * @param int $fileId * @param string $value - * - * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function setCursor($fileId, $value) { - return $this->bookmarkService->setCursor($fileId, $value); + public function setCursor(int $fileId, string $value): JSONResponse { + return new JSONResponse($this->bookmarkService->setCursor($fileId, $value)); } /** @@ -102,10 +96,9 @@ class BookmarkController extends Controller { * * @param int $fileId * @param string name - * */ - public function delete($fileId, $name) { - return $this->bookmarkService->delete($fileId, $name); + public function delete(int $fileId, string $name): void { + $this->bookmarkService->delete($fileId, $name); } /** @@ -115,9 +108,8 @@ class BookmarkController extends Controller { * @NoCSRFRequired * * @param int $fileId - * */ - public function deleteCursor($fileId) { - return $this->bookmarkService->deleteCursor($fileId); + public function deleteCursor(int $fileId): void { + $this->bookmarkService->deleteCursor($fileId); } } diff --git a/lib/Controller/MetadataController.php b/lib/Controller/MetadataController.php deleted file mode 100644 index a9b87ef..0000000 --- a/lib/Controller/MetadataController.php +++ /dev/null @@ -1,80 +0,0 @@ -metadataService = $metadataService; - } - - - /** - * @brief write metadata - * - * @NoAdminRequired - * - * @param int $fileId - * @param string $value - * - * @return array|\OCP\AppFramework\Http\JSONResponse - */ - public function setAll($fileId, $value) { - return $this->metadataService->setAll($fileId, $value); - } - - /** - * @brief return metadata item - * - * @NoAdminRequired - * @NoCSRFRequired - * - * @param int $fileId - * @param string $name - * - * @return array|\OCP\AppFramework\Http\JSONResponse - */ - public function get($fileId, $name) { - return $this->metadataService->get($fileId, $name); - } - - /** - * @brief write metadata item - * - * @NoAdminRequired - * - * @param int $fileId - * @param string $name - * @param string $value - * - * @return array|\OCP\AppFramework\Http\JSONResponse - */ - public function set($fileId, $name, $value) { - return $this->metadataService->set($fileId, $name, $value); - } - -} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index b9a8f1a..13427dd 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -11,29 +11,26 @@ namespace OCA\Epubreader\Controller; use OCA\Epubreader\Service\BookmarkService; -use OCA\Epubreader\Service\MetadataService; use OCA\Epubreader\Service\PreferenceService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\TemplateResponse; +use OCP\Files\FileInfo; +use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; - use OCP\IRequest; use OCP\IURLGenerator; use OCP\Share\IManager; class PageController extends Controller { - /** @var IURLGenerator */ - private $urlGenerator; - /** @var IRootFolder */ - private $rootFolder; - private $shareManager; - private $userId; - private $bookmarkService; - private $metadataService; - private $preferenceService; + private IURLGenerator $urlGenerator; + private IRootFolder $rootFolder; + private IManager $shareManager; + private string $userId; + private BookmarkService $bookmarkService; + private PreferenceService $preferenceService; /** * @param string $AppName @@ -44,25 +41,23 @@ class PageController extends Controller { * @param string $UserId * @param BookmarkService $bookmarkService * @param PreferenceService $preferenceService - * @param MetadataService $metadataService */ public function __construct( - $AppName, + string $AppName, IRequest $request, IURLGenerator $urlGenerator, IRootFolder $rootFolder, IManager $shareManager, - $UserId, + string $UserId, BookmarkService $bookmarkService, PreferenceService $preferenceService, - MetadataService $metadataService) { + ) { parent::__construct($AppName, $request); $this->urlGenerator = $urlGenerator; $this->rootFolder = $rootFolder; $this->shareManager = $shareManager; $this->userId = $UserId; $this->bookmarkService = $bookmarkService; - $this->metadataService = $metadataService; $this->preferenceService = $preferenceService; } @@ -72,7 +67,7 @@ class PageController extends Controller { * * @return TemplateResponse */ - public function showReader() { + public function showReader(): TemplateResponse { $templates = [ 'application/epub+zip' => 'epubreader', 'application/x-cbr' => 'cbreader', @@ -101,7 +96,7 @@ class PageController extends Controller { 'cursor' => $this->toJson($this->bookmarkService->getCursor($fileId)), 'defaults' => $this->toJson($this->preferenceService->getDefault($scope)), 'preferences' => $this->toJson($this->preferenceService->get($scope, $fileId)), - 'metadata' => $this->toJson($this->metadataService->get($fileId)), + 'metadata' => $this->toJson([]), 'annotations' => $this->toJson($this->bookmarkService->get($fileId)) ]; @@ -110,7 +105,6 @@ class PageController extends Controller { $policy->addAllowedStyleDomain('blob:'); $policy->addAllowedScriptDomain('\'self\''); $policy->addAllowedFrameDomain('\'self\''); - $policy->addAllowedChildSrcDomain('\'self\''); $policy->addAllowedFontDomain('\'self\''); $policy->addAllowedFontDomain('data:'); $policy->addAllowedFontDomain('blob:'); @@ -132,22 +126,21 @@ class PageController extends Controller { * @return array * @throws NotFoundException */ - private function getFileInfo($path) { + private function getFileInfo(string $path): array { $count = 0; $shareToken = preg_replace("/(?:\/index\.php)?\/s\/([A-Za-z0-9]{15,32})\/download.*/", "$1", $path, 1, $count); if ($count === 1) { - /* shared file or directory */ $node = $this->shareManager->getShareByToken($shareToken)->getNode(); $type = $node->getType(); /* shared directory, need file path to continue, */ - if ($type == \OCP\Files\FileInfo::TYPE_FOLDER) { + if ($type == FileInfo::TYPE_FOLDER && $node instanceof Folder) { $query = []; parse_str(parse_url($path, PHP_URL_QUERY), $query); - if (isset($query['path']) && isset($query['files'])) { - $node = $node->get($query['path'])->get($query['files']); + if (isset($query['path'])) { + $node = $node->get($query['path']); } else { throw new NotFoundException('Shared file path or name not set'); } @@ -171,7 +164,10 @@ class PageController extends Controller { ]; } - private function toJson($value) { + /** + * @param mixed $value + */ + private function toJson($value): string { return htmlspecialchars(json_encode($value), ENT_QUOTES, 'UTF-8'); } } diff --git a/lib/Controller/PreferenceController.php b/lib/Controller/PreferenceController.php index ec0887e..7505aed 100644 --- a/lib/Controller/PreferenceController.php +++ b/lib/Controller/PreferenceController.php @@ -12,28 +12,24 @@ namespace OCA\Epubreader\Controller; use OCA\Epubreader\Service\PreferenceService; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; -use OCP\IURLGenerator; - class PreferenceController extends Controller { - private $urlGenerator; - private $preferenceService; + private PreferenceService $preferenceService; /** * @param string $AppName * @param IRequest $request - * @param IURLGenerator $urlGenerator * @param PreferenceService $preferenceService */ - public function __construct($AppName, + public function __construct( + string $AppName, IRequest $request, - IURLGenerator $urlGenerator, - PreferenceService $preferenceService) { - + PreferenceService $preferenceService + ) { parent::__construct($AppName, $request); - $this->urlGenerator = $urlGenerator; $this->preferenceService = $preferenceService; } @@ -45,12 +41,10 @@ class PreferenceController extends Controller { * * @param string $scope * @param int $fileId - * @param string $name if null, return all preferences for $scope + $fileId - * - * @return array|\OCP\AppFramework\Http\JSONResponse + * @param ?string $name if null, return all preferences for $scope + $fileId */ - public function get($scope, $fileId, $name) { - return $this->preferenceService->get($scope, $fileId, $name); + public function get(string $scope, int $fileId, ?string $name = null): JSONResponse { + return new JSONResponse($this->preferenceService->get($scope, $fileId, $name)); } /** @@ -66,8 +60,8 @@ class PreferenceController extends Controller { * * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function set($scope, $fileId, $name, $value) { - return $this->preferenceService->set($scope, $fileId, $name, $value); + public function set(string $scope, int $fileId, string $name, string $value) { + return new JSONResponse($this->preferenceService->set($scope, $fileId, $name, $value)); } @@ -79,11 +73,9 @@ class PreferenceController extends Controller { * * @param string $scope * @param string $name if null, return all default preferences for scope - * - * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function getDefault($scope, $name) { - return $this->preferenceService->getDefault($scope, $name); + public function getDefault(string $scope, string $name): JSONResponse { + return new JSONResponse($this->preferenceService->getDefault($scope, $name)); } /** @@ -95,11 +87,9 @@ class PreferenceController extends Controller { * @param string $scope * @param string $name * @param string $value - * - * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function setDefault($scope, $name, $value) { - return $this->preferenceService->setDefault($scope, $name, $value); + public function setDefault(string $scope, string $name, string $value): JSONResponse { + return new JSONResponse($this->preferenceService->setDefault($scope, $name, $value)); } /** @@ -108,20 +98,18 @@ class PreferenceController extends Controller { * @param string $scope * @param int $fileId * @param string $name - * */ - public function delete($scope, $fileId, $name) { - return $this->preferenceService->delete($scope, $fileId, $name); + public function delete(string $scope, int $fileId, string $name): void { + $this->preferenceService->delete($scope, $fileId, $name); } /** * @brief delete default preference * - * @param $scope - * @param $name - * + * @param string $scope + * @param string $name */ - public function deleteDefault($scope, $name) { - return $this->preferenceService->deleteDefault($scope, $name); + public function deleteDefault(string $scope, string $name): void { + $this->preferenceService->deleteDefault($scope, $name); } } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index aeca7a1..5e82f47 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -13,33 +13,11 @@ namespace OCA\Epubreader\Controller; use OCA\Epubreader\Config; -use OCA\Epubreader\Service\PreferenceService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; -use OCP\IRequest; -use OCP\IURLGenerator; class SettingsController extends Controller { - private $urlGenerator; - private $preferenceService; - - /** - * @param string $AppName - * @param IRequest $request - * @param IURLGenerator $urlGenerator - * @param PreferenceService $preferenceService - */ - public function __construct($AppName, - IRequest $request, - IURLGenerator $urlGenerator, - PreferenceService $preferenceService) { - - parent::__construct($AppName, $request); - $this->urlGenerator = $urlGenerator; - $this->preferenceService = $preferenceService; - } - /** * @brief set preference for file type association * @@ -48,11 +26,8 @@ class SettingsController extends Controller { * @param int $EpubEnable * @param int $PdfEnable * @param int $CbxEnable - * - * @return array|\OCP\AppFramework\Http\JSONResponse */ - public function setPreference(int $EpubEnable, int $PdfEnable, int $CbxEnable) { - + public function setPreference(int $EpubEnable, int $PdfEnable, int $CbxEnable): JSONResponse { $l = \OC::$server->getL10N('epubreader'); Config::set('epub_enable', $EpubEnable); diff --git a/lib/Db/Bookmark.php b/lib/Db/Bookmark.php index 20e3158..0c85f34 100644 --- a/lib/Db/Bookmark.php +++ b/lib/Db/Bookmark.php @@ -12,13 +12,12 @@ namespace OCA\Epubreader\Db; class Bookmark extends ReaderEntity implements \JsonSerializable { - protected $userId; // user - protected $fileId; // book (identified by fileId) for which this mark is valid - protected $type; // type, defaults to "bookmark" - protected $name; // name, defaults to $location - protected $value; // bookmark value (format-specific, eg. page number for PDF, CFI for epub, etc) - protected $content; // bookmark content (annotations etc), can be empty - protected $lastModified; // modification timestamp + protected string $userId; // user + protected int $fileId; // book (identified by fileId) for which this mark is valid + protected string $type; // type, defaults to "bookmark" + protected string $name; // name, defaults to $location + protected string $value; // bookmark value (format-specific, eg. page number for PDF, CFI for epub, etc) + protected string $content; // bookmark content (annotations etc), can be empty public function jsonSerialize(): array { return [ @@ -33,7 +32,7 @@ class Bookmark extends ReaderEntity implements \JsonSerializable { ]; } - public function toService() { + public function toService(): array { return [ 'name' => $this->getName(), 'type' => $this->getType(), @@ -43,59 +42,51 @@ class Bookmark extends ReaderEntity implements \JsonSerializable { ]; } - public function getUserId() { + public function getUserId(): string { return $this->userId; } - public function setUserId($userId) { + public function setUserId(string $userId): void { $this->userId = $userId; } - public function getFileId() { + public function getFileId(): int { return $this->fileId; } - public function setFileId(int $fileId) { + public function setFileId(int $fileId): void { $this->fileId = $fileId; } - public function getType() { + public function getType(): string { return $this->type; } - public function setType($type) { + public function setType(string $type): void { $this->type = $type; } - public function getName() { + public function getName(): string { return $this->name; } - public function setName(string $name) { + public function setName(string $name): void { $this->name = $name; } - public function getValue() { + public function getValue(): string { return $this->value; } - public function setValue(string $value) { + public function setValue(string $value): void { $this->value = $value; } - public function getContent() { + public function getContent(): string { return $this->content; } - public function setContent($content) { + public function setContent(string $content): void { $this->content = $content; } - - public function getLastModified() { - return $this->lastModified; - } - - public function setLastModified($lastModified) { - $this->lastModified = $lastModified; - } } diff --git a/lib/Db/BookmarkMapper.php b/lib/Db/BookmarkMapper.php index 2578cc9..479c3a1 100644 --- a/lib/Db/BookmarkMapper.php +++ b/lib/Db/BookmarkMapper.php @@ -11,31 +11,31 @@ namespace OCA\Epubreader\Db; use OCA\Epubreader\Utility\Time; -use OCP\AppFramework\Db\Entity; use OCP\IDBConnection; class BookmarkMapper extends ReaderMapper { - private $userId; + private string $userId; /** * @param IDbConnection $db - * @param $UserId + * @param string $UserId * @param Time $time */ - public function __construct(IDBConnection $db, $UserId, Time $time) { + public function __construct(IDBConnection $db, string $UserId, Time $time) { parent::__construct($db, 'reader_bookmarks', Bookmark::class, $time); - /** @var int $UserId */ $this->userId = $UserId; } /** * @brief get bookmarks for $fileId+$userId(+$name) - * @param $fileId - * @param string $name - * @return array + * @param int $fileId + * @param ?string $name + * @param ?string $type + * + * @return ReaderEntity[] */ - public function get(int $fileId, $name, $type = null) { + public function get(int $fileId, ?string $name = null, ?string $type = null): array { $query = $this->db->getQueryBuilder(); $query->select('*') ->from($this->getTableName()) @@ -57,17 +57,18 @@ class BookmarkMapper extends ReaderMapper { * @brief write bookmark to database * * @param int $fileId - * @param string $name + * @param ?string $name * @param string $value + * @param ?string $type + * @param ?string $content * - * @return Entity the newly created or updated bookmark + * @return ReaderEntity the newly created or updated bookmark */ - public function set($fileId, $name, $value, $type, $content = null) { - + public function set(int $fileId, ?string $name = null, string $value, ?string $type = null, ?string $content = null): ReaderEntity { + /** @var Bookmark[] $result */ $result = $this->get($fileId, $name); if(empty($result)) { - // anonymous bookmarks are named after their contents if (null === $name) { $name = $value; @@ -84,13 +85,13 @@ class BookmarkMapper extends ReaderMapper { $bookmark->setType($type); $bookmark->setName($name); $bookmark->setValue($value); - $bookmark->setContent($content); + $bookmark->setContent($content ?? ''); $this->insert($bookmark); } else { $bookmark = $result[0]; $bookmark->setValue($value); - $bookmark->setContent($content); + $bookmark->setContent($content ?? ''); $this->update($bookmark); } diff --git a/lib/Db/Preference.php b/lib/Db/Preference.php index b994a12..d19520c 100644 --- a/lib/Db/Preference.php +++ b/lib/Db/Preference.php @@ -12,12 +12,11 @@ namespace OCA\Epubreader\Db; class Preference extends ReaderEntity implements \JsonSerializable { - protected $userId; // user for whom this preference is valid - protected $scope; // scope (default or specific renderer) - protected $fileId; // file for which this preference is set - protected $name; // preference name - protected $value; // preference value - protected $lastModified; // modification timestamp + protected string $userId; // user for whom this preference is valid + protected string $scope; // scope (default or specific renderer) + protected int $fileId; // file for which this preference is set + protected string $name; // preference name + protected string $value; // preference value public function jsonSerialize(): array { return [ @@ -30,58 +29,50 @@ class Preference extends ReaderEntity implements \JsonSerializable { ]; } - public function toService() { + public function toService(): array { return [ 'name' => $this->getName(), 'value' => $this->conditional_json_decode($this->getValue()), ]; } - public function getUserId() { + public function getUserId(): string { return $this->userId; } - public function setUserId($userId) { + public function setUserId(string $userId): void { $this->userId = $userId; } - public function getScope() { + public function getScope(): string { return $this->scope; } - public function setScope(string $scope) { + public function setScope(string $scope): void { $this->scope = $scope; } - public function getFileId() { + public function getFileId(): int { return $this->fileId; } - public function setFileId(int $fileId) { + public function setFileId(int $fileId): void { $this->fileId = $fileId; } - public function getName() { + public function getName(): string { return $this->name; } - public function setName(string $name) { + public function setName(string $name): void { $this->name = $name; } - public function getValue() { + public function getValue(): string { return $this->value; } - public function setValue(string $value) { + public function setValue(string $value): void { $this->value = $value; } - - public function getLastModified() { - return $this->lastModified; - } - - public function setLastModified($lastModified) { - $this->lastModified = $lastModified; - } } diff --git a/lib/Db/PreferenceMapper.php b/lib/Db/PreferenceMapper.php index 60485eb..d213397 100644 --- a/lib/Db/PreferenceMapper.php +++ b/lib/Db/PreferenceMapper.php @@ -11,14 +11,13 @@ namespace OCA\Epubreader\Db; use OCA\Epubreader\Utility\Time; -use OCP\AppFramework\Db\Entity; use OCP\IDBConnection; class PreferenceMapper extends ReaderMapper { - private $userId; + private string $userId; - public function __construct(IDBConnection $db, $UserId, Time $time) { + public function __construct(IDBConnection $db, string $UserId, Time $time) { parent::__construct($db, 'reader_prefs', Preference::class, $time); $this->userId = $UserId; } @@ -28,10 +27,11 @@ class PreferenceMapper extends ReaderMapper { * * @param string $scope * @param int $fileId - * @param string $name - * @return array + * @param ?string $name + * + * @return ReaderEntity[] */ - public function get($scope, $fileId, $name = null) { + public function get(string $scope, int $fileId, ?string $name = null): array { $query = $this->db->getQueryBuilder(); $query->select('*') ->from($this->getTableName()) @@ -54,14 +54,13 @@ class PreferenceMapper extends ReaderMapper { * @param string $name * @param string $value * - * @return Entity the newly created or updated preference + * @return ReaderEntity the newly created or updated preference */ - public function set($scope, $fileId, $name, $value) { - + public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity { + /** @var Preference[] $result */ $result = $this->get($scope, $fileId, $name); if(empty($result)) { - $preference = new Preference(); $preference->setScope($scope); $preference->setFileId($fileId); diff --git a/lib/Db/ReaderEntity.php b/lib/Db/ReaderEntity.php index 80723b4..a95bc9f 100644 --- a/lib/Db/ReaderEntity.php +++ b/lib/Db/ReaderEntity.php @@ -12,12 +12,12 @@ namespace OCA\Epubreader\Db; use OCP\AppFramework\Db\Entity; -class ReaderEntity extends Entity { +abstract class ReaderEntity extends Entity { - private $lastModified; + protected int $lastModified; // modification timestamp /* returns decoded json if input is json, otherwise returns input */ - public static function conditional_json_decode($el) { + public static function conditional_json_decode(string $el): mixed { $result = json_decode($el); if (json_last_error() === JSON_ERROR_NONE) { return $result; @@ -26,12 +26,13 @@ class ReaderEntity extends Entity { } } - public function getLastModified() { + public function getLastModified(): int { return $this->lastModified; } - public function setLastModified(string $lastModified) { + public function setLastModified(int $lastModified): void { $this->lastModified = $lastModified; } + abstract public function toService(): array; } diff --git a/lib/Db/ReaderMapper.php b/lib/Db/ReaderMapper.php index da131f7..1f6af8d 100644 --- a/lib/Db/ReaderMapper.php +++ b/lib/Db/ReaderMapper.php @@ -13,7 +13,6 @@ namespace OCA\Epubreader\Db; use OCA\Epubreader\Utility\Time; use OCP\AppFramework\Db\Entity; use OCP\AppFramework\Db\QBMapper; - use OCP\IDBConnection; /** @@ -21,12 +20,15 @@ use OCP\IDBConnection; */ abstract class ReaderMapper extends QBMapper { - /** - * @var Time - */ - private $time; + private Time $time; - public function __construct(IDBConnection $db, $table, $entity, Time $time) { + /** + * @param IDBConnection $db Instance of the Db abstraction layer + * @param string $table the name of the table. set this to allow entity + * @param class-string|null $entity the name of the entity that the sql should be mapped to queries without using sql + * @param Time $time + */ + public function __construct(IDBConnection $db, string $table, ?string $entity = null, Time $time) { parent::__construct($db, $table, $entity); $this->time = $time; } diff --git a/lib/Hooks.php b/lib/Hooks.php index a3979d8..19bc86e 100644 --- a/lib/Hooks.php +++ b/lib/Hooks.php @@ -17,7 +17,7 @@ use OCP\Util; class Hooks { - public static function register() { + public static function register(): void { Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings'); \OC::$server->getRootFolder()->listen('\OC\Files', 'preDelete', function (Node $node) { @@ -32,7 +32,7 @@ class Hooks { }); } - public static function announce_settings(array $settings) { + public static function announce_settings(array $settings): void { // Nextcloud encodes this as JSON, Owncloud does not (yet) (#75) // TODO: rmeove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties $isJson = self::isJson($settings['array']['oc_appconfig']); @@ -43,27 +43,27 @@ class Hooks { $settings['array']['oc_appconfig'] = ($isJson) ? json_encode($array) : $array; } - protected static function deleteFile(IDBConnection $connection, $fileId) { + protected static function deleteFile(IDBConnection $connection, int $fileId): void { $queryBuilder = $connection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('file_id = file_id')->setParameter('file_id', $fileId); - $queryBuilder->execute(); + $queryBuilder->executeStatement(); $queryBuilder = $connection->getQueryBuilder(); $queryBuilder->delete('reader_prefs')->where('file_id = file_id')->setParameter('file_id', $fileId); - $queryBuilder->execute(); + $queryBuilder->executeStatement(); } - protected static function deleteUser(IDBConnection $connection, $userId) { + protected static function deleteUser(IDBConnection $connection, string $userId): void { $queryBuilder = $connection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('user_id = user_id')->setParameter('user_id', $userId); - $queryBuilder->execute(); + $queryBuilder->executeStatement(); $queryBuilder = $connection->getQueryBuilder(); $queryBuilder->delete('reader_prefs')->where('user_id = user_id')->setParameter('user_id', $userId); - $queryBuilder->execute(); + $queryBuilder->executeStatement(); } - private static function isJson($string) { + private static function isJson(mixed $string): bool { return is_string($string) && is_array(json_decode($string, true)) && (json_last_error() == JSON_ERROR_NONE) ? true : false; } } diff --git a/lib/Migration/Version010404Date20201030180941.php b/lib/Migration/Version010404Date20201030180941.php index 98b2c49..8b56db3 100644 --- a/lib/Migration/Version010404Date20201030180941.php +++ b/lib/Migration/Version010404Date20201030180941.php @@ -19,16 +19,15 @@ class Version010404Date20201030180941 extends SimpleMigrationStep { * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` * @param array $options */ - public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { } /** * @param IOutput $output * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` * @param array $options - * @return null|ISchemaWrapper */ - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); @@ -138,6 +137,6 @@ class Version010404Date20201030180941 extends SimpleMigrationStep { * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` * @param array $options */ - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { } } diff --git a/lib/Service/BookmarkService.php b/lib/Service/BookmarkService.php index edb6986..19e6654 100644 --- a/lib/Service/BookmarkService.php +++ b/lib/Service/BookmarkService.php @@ -11,6 +11,7 @@ namespace OCA\Epubreader\Service; use OCA\Epubreader\Db\BookmarkMapper; +use OCA\Epubreader\Db\ReaderEntity; class BookmarkService extends Service { @@ -18,13 +19,11 @@ class BookmarkService extends Service { public const CURSOR = '__CURSOR__'; public const bookmark_type = 'bookmark'; - private $bookmarkMapper; - private $userId; + private BookmarkMapper $bookmarkMapper; - public function __construct(BookmarkMapper $bookmarkMapper, $UserId) { + public function __construct(BookmarkMapper $bookmarkMapper) { parent::__construct($bookmarkMapper); $this->bookmarkMapper = $bookmarkMapper; - $this->userId = $UserId; } /** @@ -33,14 +32,13 @@ class BookmarkService extends Service { * bookmark type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc * * @param int $fileId - * @param string $name - * - * @return array + * @param ?string $name + * @param ?string $type */ - public function get($fileId, $name = null, $type = null) { + public function get($fileId, ?string $name = null, ?string $type = null): array { $result = $this->bookmarkMapper->get($fileId, $name, $type); return array_map( - function ($entity) { + function (ReaderEntity $entity) { return $entity->toService(); }, $result); } @@ -51,12 +49,12 @@ class BookmarkService extends Service { * position type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc * * @param int $fileId - * @param string $name + * @param ?string $name * @param string $value - * - * @return array + * @param ?string $type + * @param ?string $content */ - public function set($fileId, $name, $value, $type = null, $content = null) { + public function set(int $fileId, ?string $name = null, string $value, ?string $type = null, ?string $content = null): ReaderEntity { return $this->bookmarkMapper->set($fileId, $name, $value, $type, $content); } @@ -64,14 +62,13 @@ class BookmarkService extends Service { * @brief get cursor (current position in book) * * @param int $fileId - * - * @return array */ - public function getCursor($fileId) { + public function getCursor(int $fileId): array { $result = $this->get($fileId, static::CURSOR); if (count($result) === 1) { return $result[0]; } + return []; } /** @@ -79,10 +76,8 @@ class BookmarkService extends Service { * * @param int $fileId * @param string $value - * - * @return array */ - public function setCursor($fileId, $value) { + public function setCursor(int $fileId, string $value): ReaderEntity { return $this->bookmarkMapper->set($fileId, static::CURSOR, $value, static::bookmark_type); } @@ -90,10 +85,10 @@ class BookmarkService extends Service { * @brief delete bookmark * * @param int $fileId - * @param string $name - * + * @param ?string $name + * @param ?string $type */ - public function delete($fileId, $name, $type = null) { + public function delete($fileId, ?string $name = null, ?string $type = null): void { foreach ($this->bookmarkMapper->get($fileId, $name, $type) as $bookmark) { $this->bookmarkMapper->delete($bookmark); } @@ -103,9 +98,8 @@ class BookmarkService extends Service { * @brief delete cursor * * @param int $fileId - * */ - public function deleteCursor($fileId) { + public function deleteCursor(int $fileId): void { $this->delete($fileId, static::CURSOR, static::bookmark_type); } } diff --git a/lib/Service/MetadataService.php b/lib/Service/MetadataService.php deleted file mode 100644 index f7981a9..0000000 --- a/lib/Service/MetadataService.php +++ /dev/null @@ -1,64 +0,0 @@ -appManager = $appManager; - } - - /** - * @brief get metadata item(s) - * - * @param int $fileId - * @param string $name - * - * @return array - */ - public function get($fileId, $name = null) { - return []; - } - - /** - * @brief write metadata to database - * - * @param int $fileId - * @param array $value - * - * @return array - */ - public function setAll($fileId, $value) { - // no-op for now - return []; - } - - /** - * @brief write metadata item to database - * - * @param int $fileId - * @param string $name - * @param array $value - * - * @return array - */ - public function set($fileId, $name, $value) { - // no-op for now - return []; - } -} diff --git a/lib/Service/PreferenceService.php b/lib/Service/PreferenceService.php index 4e70e7d..e3133d1 100644 --- a/lib/Service/PreferenceService.php +++ b/lib/Service/PreferenceService.php @@ -11,6 +11,7 @@ namespace OCA\Epubreader\Service; use OCA\Epubreader\Db\PreferenceMapper; +use OCA\Epubreader\Db\ReaderEntity; class PreferenceService extends Service { @@ -18,7 +19,7 @@ class PreferenceService extends Service { // value 0 to indicate a default preference public const DEFAULTS = 0; - private $preferenceMapper; + private PreferenceMapper $preferenceMapper; /** * @param PreferenceMapper $preferenceMapper @@ -36,11 +37,11 @@ class PreferenceService extends Service { * * @param string $scope * @param int $fileId - * @param string $name + * @param ?string $name * * @return array */ - public function get($scope, $fileId, $name = null) { + public function get(string $scope, int $fileId, ?string $name = null): array { $result = $this->preferenceMapper->get($scope, $fileId, $name); return array_map( function ($entity) { @@ -58,10 +59,8 @@ class PreferenceService extends Service { * @param int $fileId * @param string $name * @param string $value - * - * @return array */ - public function set($scope, $fileId, $name, $value) { + public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity { return $this->preferenceMapper->set($scope, $fileId, $name, $value); } @@ -69,11 +68,9 @@ class PreferenceService extends Service { * @brief get default preference * * @param string $scope - * @param string $name - * - * @return array + * @param ?string $name */ - public function getDefault($scope, $name = null) { + public function getDefault(string $scope, ?string $name = null): array { return $this->get($scope, static::DEFAULTS, $name); } @@ -83,10 +80,8 @@ class PreferenceService extends Service { * @param string $scope * @param string $name * @param string $value - * - * @return array */ - public function setDefault($scope, $name, $value) { + public function setDefault($scope, $name, $value): ReaderEntity { return $this->preferenceMapper->set($scope, static::DEFAULTS, $name, $value); } @@ -95,10 +90,10 @@ class PreferenceService extends Service { * * @param string $scope * @param int $fileId - * @param string $name + * @param ?string $name * */ - public function delete($scope, $fileId, $name) { + public function delete(string $scope, int $fileId, ?string $name = null): void { foreach($this->preferenceMapper->get($scope, $fileId, $name) as $preference) { $this->preferenceMapper->delete($preference); } @@ -108,10 +103,10 @@ class PreferenceService extends Service { * @brief delete default * * @param string $scope - * @param string $name + * @param ?string $name * */ - public function deleteDefault($scope, $name) { + public function deleteDefault(string $scope, ?string $name = null): void { $this->delete($scope, static::DEFAULTS, $name); } } diff --git a/lib/Service/Service.php b/lib/Service/Service.php index 1d49070..2b861e1 100644 --- a/lib/Service/Service.php +++ b/lib/Service/Service.php @@ -13,8 +13,7 @@ namespace OCA\Epubreader\Service; use OCA\Epubreader\Db\ReaderMapper; abstract class Service { - - protected $mapper; + protected ReaderMapper $mapper; public function __construct(ReaderMapper $mapper) { $this->mapper = $mapper; diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index eeb2ad8..0a582b3 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -17,11 +17,11 @@ use OCP\Settings\ISettings; class Personal implements ISettings { - private $userId; - private $configManager; + private string $userId; + private IConfig $configManager; public function __construct( - $userId, + string $userId, IConfig $configManager ) { $this->userId = $userId; @@ -32,13 +32,13 @@ class Personal implements ISettings { * @return TemplateResponse returns the instance with all parameters set, ready to be rendered * @since 9.1 */ - public function getForm() { - + public function getForm(): TemplateResponse { $parameters = [ 'EpubEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'epub_enable'), 'PdfEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'pdf_enable'), 'CbxEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'cbx_enable'), ]; + return new TemplateResponse('epubreader', 'settings-personal', $parameters, ''); } @@ -47,7 +47,7 @@ class Personal implements ISettings { * * @return TemplateResponse */ - public function getPanel() { + public function getPanel(): TemplateResponse { return $this->getForm(); } @@ -55,7 +55,7 @@ class Personal implements ISettings { * @return string the section ID, e.g. 'sharing' * @since 9.1 */ - public function getSection() { + public function getSection(): string { return 'epubreader'; } @@ -64,7 +64,7 @@ class Personal implements ISettings { * * @return string */ - public function getSectionID() { + public function getSectionID(): string { return 'epubreader'; } @@ -76,7 +76,7 @@ class Personal implements ISettings { * E.g.: 70 * @since 9.1 */ - public function getPriority() { + public function getPriority(): int { return 10; } } diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php index 2b25063..014c3b9 100644 --- a/lib/Settings/PersonalSection.php +++ b/lib/Settings/PersonalSection.php @@ -16,10 +16,9 @@ use OCP\IURLGenerator; use OCP\Settings\IIconSection; class PersonalSection implements IIconSection { - /** @var IURLGenerator */ - private $urlGenerator; - /** @var IL10N */ - private $l; + + private IURLGenerator $urlGenerator; + private IL10N $l; public function __construct(IURLGenerator $urlGenerator, IL10N $l) { $this->urlGenerator = $urlGenerator; @@ -29,18 +28,18 @@ class PersonalSection implements IIconSection { /** * returns the relative path to an 16*16 icon describing the section. * - * @returns string + * @return string */ - public function getIcon() { + public function getIcon(): string { return $this->urlGenerator->imagePath('epubreader', 'app.svg'); } /** * returns the ID of the section. It is supposed to be a lower case string, * - * @returns string + * @return string */ - public function getID() { + public function getID(): string { return 'epubreader'; } @@ -49,7 +48,7 @@ class PersonalSection implements IIconSection { * * @return string */ - public function getName() { + public function getName(): string { return $this->l->t('EPUB/CBZ/PDF ebook reader'); } @@ -58,7 +57,7 @@ class PersonalSection implements IIconSection { * * @return int */ - public function getPriority() { + public function getPriority(): int { return 20; } } diff --git a/lib/Utility/Time.php b/lib/Utility/Time.php index 71c3e17..b4cbf7f 100644 --- a/lib/Utility/Time.php +++ b/lib/Utility/Time.php @@ -11,16 +11,16 @@ namespace OCA\Epubreader\Utility; class Time { - public function getTime() { + + public function getTime(): int { return time(); } /** - * @return string the current unix time in miliseconds + * @return int the current unix time in miliseconds */ - public function getMicroTime(): string { + public function getMicroTime(): int { list($millisecs, $secs) = explode(" ", microtime()); - return $secs . substr($millisecs, 2, 6); + return (int) ($secs . substr($millisecs, 2, 6)); } - } diff --git a/lib/config.php b/lib/config.php index 7cccf6a..5d88bce 100644 --- a/lib/config.php +++ b/lib/config.php @@ -16,6 +16,7 @@ namespace OCA\Epubreader; * Config class for Reader */ class Config { + /** * @brief get user config value * @@ -23,7 +24,7 @@ class Config { * @param string $default default value to use * @return string retrieved value or default */ - public static function get($key, $default) { + public static function get(string $key, string $default): string { return \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'epubreader', $key, $default); } @@ -34,7 +35,7 @@ class Config { * @param string $value value to use * @return bool success */ - public static function set($key, $value) { + public static function set(string $key, string $value): bool { return \OC::$server->getConfig()->setUserValue(\OC_User::getUser(), 'epubreader', $key, $value); } @@ -45,7 +46,7 @@ class Config { * @param string $default default value to use * @return string retrieved value or default */ - public static function getApp($key, $default) { + public static function getApp(string $key, string $default): string { return \OC::$server->getConfig()->getAppValue('epubreader', $key, $default); } @@ -56,7 +57,7 @@ class Config { * @param string $value value to use * @return bool success */ - public static function setApp($key, $value) { + public static function setApp(string $key, string $value): bool { return \OC::$server->getConfig()->setAppValue('epubreader', $key, $value); } } diff --git a/psalm.xml b/psalm.xml index f8ce4f9..c79ee1e 100644 --- a/psalm.xml +++ b/psalm.xml @@ -34,5 +34,11 @@ + + + + + +