From 32838d8f0a99393dafb1ba0844c55fc257cc7a84 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Tue, 4 Jul 2023 12:34:36 +0330 Subject: [PATCH 1/2] Refactors files_external app commands. To improve code readability. Signed-off-by: Faraz Samapoor Signed-off-by: Faraz Samapoor --- .../files_external/lib/Command/Applicable.php | 24 +++--- apps/files_external/lib/Command/Backends.php | 17 ++--- apps/files_external/lib/Command/Config.php | 20 ++--- apps/files_external/lib/Command/Create.php | 55 ++++++-------- apps/files_external/lib/Command/Delete.php | 23 +++--- apps/files_external/lib/Command/Export.php | 2 +- apps/files_external/lib/Command/Import.php | 63 +++++++--------- .../lib/Command/ListCommand.php | 37 +++++----- apps/files_external/lib/Command/Notify.php | 73 +++++++------------ apps/files_external/lib/Command/Option.php | 8 +- apps/files_external/lib/Command/Verify.php | 21 +++--- 11 files changed, 137 insertions(+), 206 deletions(-) diff --git a/apps/files_external/lib/Command/Applicable.php b/apps/files_external/lib/Command/Applicable.php index a3708602dff8e..dbedc72825cf5 100644 --- a/apps/files_external/lib/Command/Applicable.php +++ b/apps/files_external/lib/Command/Applicable.php @@ -34,21 +34,15 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\HttpFoundation\Response; class Applicable extends Base { - protected GlobalStoragesService $globalService; - private IUserManager $userManager; - private IGroupManager $groupManager; - public function __construct( - GlobalStoragesService $globalService, - IUserManager $userManager, - IGroupManager $groupManager + protected GlobalStoragesService $globalService, + private IUserManager $userManager, + private IGroupManager $groupManager, ) { parent::__construct(); - $this->globalService = $globalService; - $this->userManager = $userManager; - $this->groupManager = $groupManager; } protected function configure(): void { @@ -94,12 +88,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int $mount = $this->globalService->getStorage($mountId); } catch (NotFoundException $e) { $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts'); - return 404; + return Response::HTTP_NOT_FOUND; } if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { $output->writeln('Can\'t change applicables on personal mounts'); - return 1; + return self::FAILURE; } $addUsers = $input->getOption('add-user'); @@ -114,13 +108,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($addUsers as $addUser) { if (!$this->userManager->userExists($addUser)) { $output->writeln('User "' . $addUser . '" not found'); - return 404; + return Response::HTTP_NOT_FOUND; } } foreach ($addGroups as $addGroup) { if (!$this->groupManager->groupExists($addGroup)) { $output->writeln('Group "' . $addGroup . '" not found'); - return 404; + return Response::HTTP_NOT_FOUND; } } @@ -142,6 +136,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'users' => $applicableUsers, 'groups' => $applicableGroups ]); - return 0; + return self::SUCCESS; } } diff --git a/apps/files_external/lib/Command/Backends.php b/apps/files_external/lib/Command/Backends.php index faf6209bd74b8..3a0f26a28035e 100644 --- a/apps/files_external/lib/Command/Backends.php +++ b/apps/files_external/lib/Command/Backends.php @@ -33,13 +33,10 @@ use Symfony\Component\Console\Output\OutputInterface; class Backends extends Base { - private BackendService $backendService; - - public function __construct(BackendService $backendService + public function __construct( + private BackendService $backendService, ) { parent::__construct(); - - $this->backendService = $backendService; } protected function configure(): void { @@ -72,24 +69,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($type) { if (!isset($data[$type])) { $output->writeln('Invalid type "' . $type . '". Possible values are "authentication" or "storage"'); - return 1; + return self::FAILURE; } $data = $data[$type]; if ($backend) { if (!isset($data[$backend])) { $output->writeln('Unknown backend "' . $backend . '" of type "' . $type . '"'); - return 1; + return self::FAILURE; } $data = $data[$backend]; } } $this->writeArrayInOutputFormat($input, $output, $data); - return 0; + return self::SUCCESS; } - private function serializeAuthBackend(\JsonSerializable $backend) { + private function serializeAuthBackend(\JsonSerializable $backend): array { $data = $backend->jsonSerialize(); $result = [ 'name' => $data['name'], @@ -112,7 +109,7 @@ private function serializeAuthBackend(\JsonSerializable $backend) { * @param DefinitionParameter[] $parameters * @return string[] */ - private function formatConfiguration(array $parameters) { + private function formatConfiguration(array $parameters): array { $configuration = array_filter($parameters, function (DefinitionParameter $parameter) { return $parameter->getType() !== DefinitionParameter::VALUE_HIDDEN; }); diff --git a/apps/files_external/lib/Command/Config.php b/apps/files_external/lib/Command/Config.php index 1107bfb4d423c..3b905cc74fb6f 100644 --- a/apps/files_external/lib/Command/Config.php +++ b/apps/files_external/lib/Command/Config.php @@ -31,13 +31,13 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\HttpFoundation\Response; class Config extends Base { - protected GlobalStoragesService $globalService; - - public function __construct(GlobalStoragesService $globalService) { + public function __construct( + protected GlobalStoragesService $globalService, + ) { parent::__construct(); - $this->globalService = $globalService; } protected function configure(): void { @@ -67,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $mount = $this->globalService->getStorage($mountId); } catch (NotFoundException $e) { $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; + return Response::HTTP_NOT_FOUND; } $value = $input->getArgument('value'); @@ -76,15 +76,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int } else { $this->getOption($mount, $key, $output); } - return 0; + return self::SUCCESS; } /** - * @param StorageConfig $mount * @param string $key - * @param OutputInterface $output */ - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { + protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void { if ($key === 'mountpoint' || $key === 'mount_point') { $value = $mount->getMountPoint(); } else { @@ -97,12 +95,10 @@ protected function getOption(StorageConfig $mount, $key, OutputInterface $output } /** - * @param StorageConfig $mount * @param string $key * @param string $value - * @param OutputInterface $output */ - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void { $decoded = json_decode($value, true); if (!is_null($decoded) && json_encode($decoded) === $value) { $value = $decoded; diff --git a/apps/files_external/lib/Command/Create.php b/apps/files_external/lib/Command/Create.php index 6208ac0da07ff..ea0d98fe4670c 100644 --- a/apps/files_external/lib/Command/Create.php +++ b/apps/files_external/lib/Command/Create.php @@ -42,26 +42,17 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\HttpFoundation\Response; class Create extends Base { - private GlobalStoragesService $globalService; - private UserStoragesService $userService; - private IUserManager $userManager; - private BackendService $backendService; - private IUserSession $userSession; - - public function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserManager $userManager, - IUserSession $userSession, - BackendService $backendService + public function __construct( + private GlobalStoragesService $globalService, + private UserStoragesService $userService, + private IUserManager $userManager, + private IUserSession $userSession, + private BackendService $backendService, ) { parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userManager = $userManager; - $this->userSession = $userSession; - $this->backendService = $backendService; } protected function configure(): void { @@ -116,32 +107,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (!Filesystem::isValidPath($mountPoint)) { $output->writeln('Invalid mountpoint "' . $mountPoint . '"'); - return 1; + return self::FAILURE; } if (is_null($storageBackend)) { $output->writeln('Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)'); - return 404; + return Response::HTTP_NOT_FOUND; } if (is_null($authBackend)) { $output->writeln('Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)'); - return 404; + return Response::HTTP_NOT_FOUND; } $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); if (!in_array($authBackend->getScheme(), $supportedSchemes)) { $output->writeln('Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)'); - return 1; + return self::FAILURE; } $config = []; foreach ($configInput as $configOption) { if (!str_contains($configOption, '=')) { $output->writeln('Invalid mount configuration option "' . $configOption . '"'); - return 1; + return self::FAILURE; } [$key, $value] = explode('=', $configOption, 2); if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { $output->writeln('Unknown configuration for backends "' . $key . '"'); - return 1; + return self::FAILURE; } $config[$key] = $value; } @@ -155,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($user) { if (!$this->userManager->userExists($user)) { $output->writeln('User "' . $user . '" not found'); - return 1; + return self::FAILURE; } $mount->setApplicableUsers([$user]); } @@ -170,7 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $output->writeln((string)$mount->getId()); } } - return 0; + return self::SUCCESS; } private function validateParam(string $key, &$value, Backend $storageBackend, AuthMechanism $authBackend): bool { @@ -196,15 +187,15 @@ private function showMount(string $user, StorageConfig $mount, InputInterface $i } protected function getStorageService(string $userId): StoragesService { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { + if (empty($userId)) { return $this->globalService; } + + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; } } diff --git a/apps/files_external/lib/Command/Delete.php b/apps/files_external/lib/Command/Delete.php index cf09e3907b72c..6b6ae29977303 100644 --- a/apps/files_external/lib/Command/Delete.php +++ b/apps/files_external/lib/Command/Delete.php @@ -35,19 +35,16 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\HttpFoundation\Response; class Delete extends Base { - protected GlobalStoragesService $globalService; - protected UserStoragesService $userService; - protected IUserSession $userSession; - protected IUserManager $userManager; - - public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + public function __construct( + protected GlobalStoragesService $globalService, + protected UserStoragesService $userService, + protected IUserSession $userSession, + protected IUserManager $userManager, + ) { parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; } protected function configure(): void { @@ -73,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $mount = $this->globalService->getStorage($mountId); } catch (NotFoundException $e) { $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; + return Response::HTTP_NOT_FOUND; } $noConfirm = $input->getOption('yes'); @@ -88,11 +85,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $question = new ConfirmationQuestion('Delete this mount? [y/N] ', false); if (!$questionHelper->ask($input, $output, $question)) { - return 1; + return self::FAILURE; } } $this->globalService->removeStorage($mountId); - return 0; + return self::SUCCESS; } } diff --git a/apps/files_external/lib/Command/Export.php b/apps/files_external/lib/Command/Export.php index 98cfb68306021..b5f7b67eafb80 100644 --- a/apps/files_external/lib/Command/Export.php +++ b/apps/files_external/lib/Command/Export.php @@ -54,6 +54,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int $listInput->setOption('show-password', true); $listInput->setOption('full', true); $listCommand->execute($listInput, $output); - return 0; + return self::SUCCESS; } } diff --git a/apps/files_external/lib/Command/Import.php b/apps/files_external/lib/Command/Import.php index eab7dd9a6bedb..534c0a2d9d867 100644 --- a/apps/files_external/lib/Command/Import.php +++ b/apps/files_external/lib/Command/Import.php @@ -30,6 +30,7 @@ use OCA\Files_External\Service\BackendService; use OCA\Files_External\Service\GlobalStoragesService; use OCA\Files_External\Service\ImportLegacyStoragesService; +use OCA\Files_External\Service\StoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\IUserManager; use OCP\IUserSession; @@ -40,27 +41,15 @@ use Symfony\Component\Console\Output\OutputInterface; class Import extends Base { - private GlobalStoragesService $globalService; - private UserStoragesService $userService; - private IUserSession $userSession; - private IUserManager $userManager; - private ImportLegacyStoragesService $importLegacyStorageService; - private BackendService $backendService; - - public function __construct(GlobalStoragesService $globalService, - UserStoragesService $userService, - IUserSession $userSession, - IUserManager $userManager, - ImportLegacyStoragesService $importLegacyStorageService, - BackendService $backendService + public function __construct( + private GlobalStoragesService $globalService, + private UserStoragesService $userService, + private IUserSession $userSession, + private IUserManager $userManager, + private ImportLegacyStoragesService $importLegacyStorageService, + private BackendService $backendService, ) { parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; - $this->importLegacyStorageService = $importLegacyStorageService; - $this->backendService = $backendService; } protected function configure(): void { @@ -95,18 +84,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int } else { if (!file_exists($path)) { $output->writeln('File not found: ' . $path . ''); - return 1; + return self::FAILURE; } $json = file_get_contents($path); } if (!is_string($json) || strlen($json) < 2) { $output->writeln('Error while reading json'); - return 1; + return self::FAILURE; } $data = json_decode($json, true); if (!is_array($data)) { $output->writeln('Error while parsing json'); - return 1; + return self::FAILURE; } $isLegacy = isset($data['user']) || isset($data['group']); @@ -116,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int foreach ($mounts as $mount) { if ($mount->getBackendOption('password') === false) { $output->writeln('Failed to decrypt password'); - return 1; + return self::FAILURE; } } } else { @@ -147,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $existingMount->getBackendOptions() === $mount->getBackendOptions() ) { $output->writeln("Duplicate mount (" . $mount->getMountPoint() . ")"); - return 1; + return self::FAILURE; } } } @@ -155,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($input->getOption('dry')) { if (count($mounts) === 0) { $output->writeln('No mounts to be imported'); - return 1; + return self::FAILURE; } $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); $listInput = new ArrayInput([], $listCommand->getDefinition()); @@ -167,7 +156,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $storageService->addStorage($mount); } } - return 0; + return self::SUCCESS; } private function parseData(array $data): StorageConfig { @@ -178,8 +167,8 @@ private function parseData(array $data): StorageConfig { $mount->setAuthMechanism($authBackend); $mount->setBackendOptions($data['configuration']); $mount->setMountOptions($data['options']); - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); + $mount->setApplicableUsers($data['applicable_users'] ?? []); + $mount->setApplicableGroups($data['applicable_groups'] ?? []); return $mount; } @@ -192,16 +181,16 @@ private function getBackendByClass(string $className) { } } - protected function getStorageService($userId) { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { + protected function getStorageService($userId): StoragesService { + if (empty($userId)) { return $this->globalService; } + + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; } } diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php index b2a4baf366b8a..929e9d4f5151c 100644 --- a/apps/files_external/lib/Command/ListCommand.php +++ b/apps/files_external/lib/Command/ListCommand.php @@ -29,6 +29,7 @@ use OC\User\NoUserException; use OCA\Files_External\Lib\StorageConfig; use OCA\Files_External\Service\GlobalStoragesService; +use OCA\Files_External\Service\StoragesService; use OCA\Files_External\Service\UserStoragesService; use OCP\IUserManager; use OCP\IUserSession; @@ -39,19 +40,15 @@ use Symfony\Component\Console\Output\OutputInterface; class ListCommand extends Base { - protected GlobalStoragesService $globalService; - protected UserStoragesService $userService; - protected IUserSession $userSession; - protected IUserManager $userManager; - public const ALL = -1; - public function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { + public function __construct( + protected GlobalStoragesService $globalService, + protected UserStoragesService $userService, + protected IUserSession $userSession, + protected IUserManager $userManager, + ) { parent::__construct(); - $this->globalService = $globalService; - $this->userService = $userService; - $this->userSession = $userSession; - $this->userManager = $userManager; } protected function configure(): void { @@ -93,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $this->listMounts($userId, $mounts, $input, $output); - return 0; + return self::SUCCESS; } /** @@ -245,16 +242,16 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output } } - protected function getStorageService($userId) { - if (!empty($userId)) { - $user = $this->userManager->get($userId); - if (is_null($user)) { - throw new NoUserException("user $userId not found"); - } - $this->userSession->setUser($user); - return $this->userService; - } else { + protected function getStorageService($userId): StoragesService { + if (empty($userId)) { return $this->globalService; } + + $user = $this->userManager->get($userId); + if (is_null($user)) { + throw new NoUserException("user $userId not found"); + } + $this->userSession->setUser($user); + return $this->userService; } } diff --git a/apps/files_external/lib/Command/Notify.php b/apps/files_external/lib/Command/Notify.php index 5013d88539b3c..fd3a66a756e9b 100644 --- a/apps/files_external/lib/Command/Notify.php +++ b/apps/files_external/lib/Command/Notify.php @@ -50,23 +50,13 @@ use Symfony\Component\Console\Output\OutputInterface; class Notify extends Base { - private GlobalStoragesService $globalService; - private IDBConnection $connection; - private LoggerInterface $logger; - /** @var IUserManager */ - private $userManager; - public function __construct( - GlobalStoragesService $globalService, - IDBConnection $connection, - LoggerInterface $logger, - IUserManager $userManager + private GlobalStoragesService $globalService, + private IDBConnection $connection, + private LoggerInterface $logger, + private IUserManager $userManager ) { parent::__construct(); - $this->globalService = $globalService; - $this->connection = $connection; - $this->logger = $logger; - $this->userManager = $userManager; } protected function configure(): void { @@ -110,32 +100,24 @@ protected function configure(): void { private function getUserOption(InputInterface $input): ?string { if ($input->getOption('user')) { return (string)$input->getOption('user'); - } elseif (isset($_ENV['NOTIFY_USER'])) { - return $_ENV['NOTIFY_USER']; - } elseif (isset($_SERVER['NOTIFY_USER'])) { - return $_SERVER['NOTIFY_USER']; - } else { - return null; } + + return $_ENV['NOTIFY_USER'] ?? $_SERVER['NOTIFY_USER'] ?? null; } private function getPasswordOption(InputInterface $input): ?string { if ($input->getOption('password')) { return (string)$input->getOption('password'); - } elseif (isset($_ENV['NOTIFY_PASSWORD'])) { - return $_ENV['NOTIFY_PASSWORD']; - } elseif (isset($_SERVER['NOTIFY_PASSWORD'])) { - return $_SERVER['NOTIFY_PASSWORD']; - } else { - return null; } + + return $_ENV['NOTIFY_PASSWORD'] ?? $_SERVER['NOTIFY_PASSWORD'] ?? null; } protected function execute(InputInterface $input, OutputInterface $output): int { $mount = $this->globalService->getStorage($input->getArgument('mount_id')); if (is_null($mount)) { $output->writeln('Mount not found'); - return 1; + return self::FAILURE; } $noAuth = false; @@ -178,11 +160,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int if ($noAuth) { $output->writeln('Username and/or password required'); } - return 1; + return self::FAILURE; } if (!$storage instanceof INotifyStorage) { $output->writeln('Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications'); - return 1; + return self::FAILURE; } $dryRun = $input->getOption('dry-run'); @@ -204,7 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $this->markParentAsOutdated($mount->getId(), $change->getPath(), $output, $dryRun); }); - return 0; + return self::SUCCESS; } private function createStorage(StorageConfig $mount): IStorage { @@ -212,7 +194,7 @@ private function createStorage(StorageConfig $mount): IStorage { return new $class($mount->getBackendOptions()); } - private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $dryRun) { + private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $dryRun): void { $parent = ltrim(dirname($path), '/'); if ($parent === '.') { $parent = ''; @@ -253,22 +235,17 @@ private function markParentAsOutdated($mountId, $path, OutputInterface $output, } } - private function logUpdate(IChange $change, OutputInterface $output) { - switch ($change->getType()) { - case INotifyStorage::NOTIFY_ADDED: - $text = 'added'; - break; - case INotifyStorage::NOTIFY_MODIFIED: - $text = 'modified'; - break; - case INotifyStorage::NOTIFY_REMOVED: - $text = 'removed'; - break; - case INotifyStorage::NOTIFY_RENAMED: - $text = 'renamed'; - break; - default: - return; + private function logUpdate(IChange $change, OutputInterface $output): void { + $text = match ($change->getType()) { + INotifyStorage::NOTIFY_ADDED => 'added', + INotifyStorage::NOTIFY_MODIFIED => 'modified', + INotifyStorage::NOTIFY_REMOVED => 'removed', + INotifyStorage::NOTIFY_RENAMED => 'renamed', + default => '', + }; + + if ($text === '') { + return; } $text .= ' ' . $change->getPath(); @@ -324,7 +301,7 @@ private function reconnectToDatabase(IDBConnection $connection, OutputInterface } - private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, OutputInterface $output) { + private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, OutputInterface $output): void { usleep(100 * 1000); //give time for the notify to start if (!$storage->file_put_contents('/.nc_test_file.txt', 'test content')) { $output->writeln("Failed to create test file for self-test"); diff --git a/apps/files_external/lib/Command/Option.php b/apps/files_external/lib/Command/Option.php index 30390ebabee47..8ef423e26997a 100644 --- a/apps/files_external/lib/Command/Option.php +++ b/apps/files_external/lib/Command/Option.php @@ -47,11 +47,9 @@ protected function configure(): void { } /** - * @param StorageConfig $mount * @param string $key - * @param OutputInterface $output */ - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { + protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void { $value = $mount->getMountOption($key); if (!is_string($value)) { // show bools and objects correctly $value = json_encode($value); @@ -60,12 +58,10 @@ protected function getOption(StorageConfig $mount, $key, OutputInterface $output } /** - * @param StorageConfig $mount * @param string $key * @param string $value - * @param OutputInterface $output */ - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void { $decoded = json_decode($value, true); if (!is_null($decoded)) { $value = $decoded; diff --git a/apps/files_external/lib/Command/Verify.php b/apps/files_external/lib/Command/Verify.php index f8079ec4d6500..625d0c1d515b1 100644 --- a/apps/files_external/lib/Command/Verify.php +++ b/apps/files_external/lib/Command/Verify.php @@ -36,13 +36,13 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\HttpFoundation\Response; class Verify extends Base { - protected GlobalStoragesService $globalService; - - public function __construct(GlobalStoragesService $globalService) { + public function __construct( + protected GlobalStoragesService $globalService, + ) { parent::__construct(); - $this->globalService = $globalService; } protected function configure(): void { @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $mount = $this->globalService->getStorage($mountId); } catch (NotFoundException $e) { $output->writeln('Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"'); - return 404; + return Response::HTTP_NOT_FOUND; } $this->updateStorageStatus($mount, $configInput, $output); @@ -80,19 +80,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'code' => $mount->getStatus(), 'message' => $mount->getStatusMessage() ]); - return 0; + return self::SUCCESS; } - private function manipulateStorageConfig(StorageConfig $storage) { - /** @var AuthMechanism */ + private function manipulateStorageConfig(StorageConfig $storage): void { $authMechanism = $storage->getAuthMechanism(); $authMechanism->manipulateStorageConfig($storage); - /** @var Backend */ $backend = $storage->getBackend(); $backend->manipulateStorageConfig($storage); } - private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) { + private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output): void { try { try { $this->manipulateStorageConfig($storage); @@ -111,7 +109,6 @@ private function updateStorageStatus(StorageConfig &$storage, $configInput, Outp $storage->setBackendOption($key, $value); } - /** @var Backend */ $backend = $storage->getBackend(); // update status (can be time-consuming) $storage->setStatus( @@ -122,7 +119,7 @@ private function updateStorageStatus(StorageConfig &$storage, $configInput, Outp ) ); } catch (InsufficientDataForMeaningfulAnswerException $e) { - $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; + $status = $e->getCode() ?: StorageNotAvailableException::STATUS_INDETERMINATE; $storage->setStatus( $status, $e->getMessage() From eab7818f0eff6d804258c30aa6ffd98ddb6ede03 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Mon, 17 Jul 2023 21:18:56 +0330 Subject: [PATCH 2/2] Adds parameter type hints. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Côme Chilliet Signed-off-by: Faraz Samapoor Signed-off-by: Faraz Samapoor --- apps/files_external/lib/Command/Import.php | 2 +- apps/files_external/lib/Command/ListCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_external/lib/Command/Import.php b/apps/files_external/lib/Command/Import.php index 534c0a2d9d867..a4976b21cb7b3 100644 --- a/apps/files_external/lib/Command/Import.php +++ b/apps/files_external/lib/Command/Import.php @@ -181,7 +181,7 @@ private function getBackendByClass(string $className) { } } - protected function getStorageService($userId): StoragesService { + protected function getStorageService(string $userId): StoragesService { if (empty($userId)) { return $this->globalService; } diff --git a/apps/files_external/lib/Command/ListCommand.php b/apps/files_external/lib/Command/ListCommand.php index 929e9d4f5151c..e4a33a99a922d 100644 --- a/apps/files_external/lib/Command/ListCommand.php +++ b/apps/files_external/lib/Command/ListCommand.php @@ -242,7 +242,7 @@ public function listMounts($userId, array $mounts, InputInterface $input, Output } } - protected function getStorageService($userId): StoragesService { + protected function getStorageService(string $userId): StoragesService { if (empty($userId)) { return $this->globalService; }