Refacto all project #1

Merged
Xefir merged 18 commits from test into master 2023-06-17 23:48:37 +00:00
10 changed files with 95 additions and 57 deletions
Showing only changes of commit 174ffd462e - Show all commits

View File

@ -1,20 +0,0 @@
<?php
/**
* ownCloud - Epubreader App
*
* @author Frank de Lange
* @copyright 2015 - 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Epubreader\AppInfo;
use OCP\Util;
$l = \OC::$server->getL10N('epubreader');
\OCA\Epubreader\Hooks::register();
Util::addscript('epubreader', 'plugin');

View File

@ -0,0 +1,30 @@
<?php
/**
* ownCloud - Epubreader App
*
* @author Frank de Lange
* @copyright 2015 - 2017 Frank de Lange
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Epubreader\AppInfo;
use OCA\Epubreader\Hooks;
use OCP\AppFramework\App;
use OCP\Util;
class Application extends App {
public const APP_ID = 'epubreader';
public function __construct() {
parent::__construct(self::APP_ID);
$l = \OC::$server->getL10N('epubreader');
Hooks::register();
Util::addscript('epubreader', 'plugin');
}
}

View File

@ -75,15 +75,15 @@ class PageController extends Controller {
];
/**
* $fileInfo = [
* fileId => null,
* fileName => null,
* fileType => null
* ];
* @var array{
* fileId: int,
* fileName: string,
* fileType: string
* } $fileInfo
*/
$fileInfo = $this->getFileInfo($this->request->getParam('file'));
$fileInfo = $this->getFileInfo((string) $this->request->getParam('file'));
$fileId = $fileInfo['fileId'];
$type = $this->request->getParam('type');
$type = (string) $this->request->getParam('type');
$scope = $template = $templates[$type];
$params = [
@ -139,7 +139,7 @@ class PageController extends Controller {
if ($type == FileInfo::TYPE_FOLDER && $node instanceof Folder) {
$query = [];
parse_str(parse_url($path, PHP_URL_QUERY), $query);
if (isset($query['path'])) {
if (isset($query['path']) && is_string($query['path'])) {
$node = $node->get($query['path']);
} else {
throw new NotFoundException('Shared file path or name not set');
@ -150,11 +150,11 @@ class PageController extends Controller {
} else {
$filePath = $path;
$fileId = $this->rootFolder->getUserFolder($this->userId)
->get(preg_replace("/.*\/remote.php\/webdav(.*)/", "$1", rawurldecode($this->request->getParam('file'))))
->get(preg_replace("/.*\/remote.php\/webdav(.*)/", "$1", rawurldecode((string) $this->request->getParam('file'))))
->getId();
}
/** @var array<string> $pathInfo */
/** @var string[] $pathInfo */
$pathInfo = pathInfo($filePath);
return [

View File

@ -57,10 +57,8 @@ class PreferenceController extends Controller {
* @param int $fileId
* @param string $name
* @param string $value
*
* @return array|\OCP\AppFramework\Http\JSONResponse
*/
public function set(string $scope, int $fileId, string $name, string $value) {
public function set(string $scope, int $fileId, string $name, string $value): JSONResponse {
return new JSONResponse($this->preferenceService->set($scope, $fileId, $name, $value));
}

View File

@ -34,10 +34,10 @@ class SettingsController extends Controller {
Config::set('pdf_enable', $PdfEnable);
Config::set('cbx_enable', $CbxEnable);
$response = array(
'data' => array('message' => $l->t('Settings updated successfully.')),
$response = [
'data' => ['message' => $l->t('Settings updated successfully.')],
'status' => 'success'
);
];
return new JSONResponse($response);
}

View File

@ -12,12 +12,20 @@ namespace OCA\Epubreader\Db;
use OCP\AppFramework\Db\Entity;
/**
* @psalm-type SerializedEntity = array<string|int|array>
*/
abstract class ReaderEntity extends Entity {
protected int $lastModified; // modification timestamp
/* returns decoded json if input is json, otherwise returns input */
/**
* returns decoded json if input is json, otherwise returns input
*
* @return string|array
*/
public static function conditional_json_decode(string $el): mixed {
/** @var array $result */
$result = json_decode($el);
if (json_last_error() === JSON_ERROR_NONE) {
return $result;
@ -34,5 +42,13 @@ abstract class ReaderEntity extends Entity {
$this->lastModified = $lastModified;
}
/**
* @psalm-return SerializedEntity
*/
abstract public function toService(): array;
/**
* @psalm-return SerializedEntity
*/
abstract public function jsonSerialize(): array;
}

View File

@ -18,6 +18,7 @@ use OCP\Util;
class Hooks {
public static function register(): void {
/** @psalm-suppress DeprecatedMethod */
Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings');
\OC::$server->getRootFolder()->listen('\OC\Files', 'preDelete', function (Node $node) {
@ -35,13 +36,21 @@ class Hooks {
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
if (array_key_exists('array', $settings) &&
is_array($settings['array']) &&
array_key_exists('oc_appconfig', $settings['array'])
) {
$isJson = self::isJson($settings['array']['oc_appconfig']);
$array = ($isJson) ? json_decode($settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig'];
$array['filesReader']['enableEpub'] = Config::get('epub_enable', 'true');
$array['filesReader']['enablePdf'] = Config::get('pdf_enable', 'true');
$array['filesReader']['enableCbx'] = Config::get('cbx_enable', 'true');
/** @var array $array */
$array = ($isJson) ? json_decode((string) $settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig'];
$array['filesReader'] = [
'enableEpub' => Config::get('epub_enable', 'true'),
'enablePdf' => Config::get('pdf_enable', 'true'),
'enableCbx' => Config::get('cbx_enable', 'true'),
];
$settings['array']['oc_appconfig'] = ($isJson) ? json_encode($array) : $array;
}
}
protected static function deleteFile(IDBConnection $connection, int $fileId): void {
$queryBuilder = $connection->getQueryBuilder();

View File

@ -13,6 +13,9 @@ namespace OCA\Epubreader\Service;
use OCA\Epubreader\Db\BookmarkMapper;
use OCA\Epubreader\Db\ReaderEntity;
/**
* @psalm-import-type SerializedEntity from ReaderEntity
*/
class BookmarkService extends Service {
// "bookmark" name to use for the cursor (current reading position)
@ -34,11 +37,13 @@ class BookmarkService extends Service {
* @param int $fileId
* @param ?string $name
* @param ?string $type
*
* @psalm-return SerializedEntity[]
*/
public function get($fileId, ?string $name = null, ?string $type = null): array {
$result = $this->bookmarkMapper->get($fileId, $name, $type);
return array_map(
function (ReaderEntity $entity) {
function (ReaderEntity $entity): array {
return $entity->toService();
}, $result);
}
@ -62,9 +67,11 @@ class BookmarkService extends Service {
* @brief get cursor (current position in book)
*
* @param int $fileId
*
* @psalm-return SerializedEntity
*/
public function getCursor(int $fileId): array {
$result = $this->get($fileId, static::CURSOR);
$result = $this->get($fileId, self::CURSOR);
if (count($result) === 1) {
return $result[0];
}
@ -78,7 +85,7 @@ class BookmarkService extends Service {
* @param string $value
*/
public function setCursor(int $fileId, string $value): ReaderEntity {
return $this->bookmarkMapper->set($fileId, static::CURSOR, $value, static::bookmark_type);
return $this->bookmarkMapper->set($fileId, self::CURSOR, $value, self::bookmark_type);
}
/**
@ -100,6 +107,6 @@ class BookmarkService extends Service {
* @param int $fileId
*/
public function deleteCursor(int $fileId): void {
$this->delete($fileId, static::CURSOR, static::bookmark_type);
$this->delete($fileId, self::CURSOR, self::bookmark_type);
}
}

View File

@ -13,6 +13,9 @@ 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
@ -39,12 +42,12 @@ class PreferenceService extends Service {
* @param int $fileId
* @param ?string $name
*
* @return array
* @psalm-return SerializedEntity
*/
public function get(string $scope, int $fileId, ?string $name = null): array {
$result = $this->preferenceMapper->get($scope, $fileId, $name);
return array_map(
function ($entity) {
function (ReaderEntity $entity): array {
return $entity->toService();
}, $result);
}
@ -71,7 +74,7 @@ class PreferenceService extends Service {
* @param ?string $name
*/
public function getDefault(string $scope, ?string $name = null): array {
return $this->get($scope, static::DEFAULTS, $name);
return $this->get($scope, self::DEFAULTS, $name);
}
/**
@ -82,7 +85,7 @@ class PreferenceService extends Service {
* @param string $value
*/
public function setDefault($scope, $name, $value): ReaderEntity {
return $this->preferenceMapper->set($scope, static::DEFAULTS, $name, $value);
return $this->preferenceMapper->set($scope, self::DEFAULTS, $name, $value);
}
/**
@ -107,6 +110,6 @@ class PreferenceService extends Service {
*
*/
public function deleteDefault(string $scope, ?string $name = null): void {
$this->delete($scope, static::DEFAULTS, $name);
$this->delete($scope, self::DEFAULTS, $name);
}
}

View File

@ -34,11 +34,6 @@
<referencedClass name="OC" />
</errorLevel>
</UndefinedClass>
<DeprecatedMethod>
<errorLevel type="info">
<referencedMethod name="OCP\Util::connectHook" />
</errorLevel>
</DeprecatedMethod>
<PropertyNotSetInConstructor errorLevel="suppress" />
</issueHandlers>
</psalm>