Skip to content

Commit e085779

Browse files
Faraz SamapoorAndyScherzinger
authored andcommitted
Uses early returns.
To improve code readability. Signed-off-by: Faraz Samapoor <fsa@adlas.at>
1 parent ea7f88e commit e085779

File tree

4 files changed

+124
-101
lines changed

4 files changed

+124
-101
lines changed

apps/files_trashbin/lib/Command/CleanUp.php

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -65,40 +65,48 @@ protected function configure(): void {
6565
protected function execute(InputInterface $input, OutputInterface $output): int {
6666
$users = $input->getArgument('user_id');
6767
$verbose = $input->getOption('verbose');
68+
69+
if ((empty($users)) and (!$input->getOption('all-users'))) {
70+
throw new InvalidOptionException('Either specify a user_id or --all-users');
71+
}
72+
6873
if ((!empty($users)) and ($input->getOption('all-users'))) {
6974
throw new InvalidOptionException('Either specify a user_id or --all-users');
70-
} elseif (!empty($users)) {
75+
}
76+
77+
if (!empty($users)) {
7178
foreach ($users as $user) {
72-
if ($this->userManager->userExists($user)) {
73-
$output->writeln("Remove deleted files of <info>$user</info>");
74-
$this->removeDeletedFiles($user, $output, $verbose);
75-
} else {
79+
if (!$this->userManager->userExists($user)) {
7680
$output->writeln("<error>Unknown user $user</error>");
7781
return self::FAILURE;
7882
}
83+
84+
$output->writeln("Remove deleted files of <info>$user</info>");
85+
$this->removeDeletedFiles($user, $output, $verbose);
7986
}
80-
} elseif ($input->getOption('all-users')) {
81-
$output->writeln('Remove deleted files for all users');
82-
foreach ($this->userManager->getBackends() as $backend) {
83-
$name = get_class($backend);
84-
if ($backend instanceof IUserBackend) {
85-
$name = $backend->getBackendName();
86-
}
87-
$output->writeln("Remove deleted files for users on backend <info>$name</info>");
88-
$limit = 500;
89-
$offset = 0;
90-
do {
91-
$users = $backend->getUsers('', $limit, $offset);
92-
foreach ($users as $user) {
93-
$output->writeln(" <info>$user</info>");
94-
$this->removeDeletedFiles($user, $output, $verbose);
95-
}
96-
$offset += $limit;
97-
} while (count($users) >= $limit);
87+
88+
return self::SUCCESS;
89+
}
90+
91+
$output->writeln('Remove deleted files for all users');
92+
foreach ($this->userManager->getBackends() as $backend) {
93+
$name = get_class($backend);
94+
if ($backend instanceof IUserBackend) {
95+
$name = $backend->getBackendName();
9896
}
99-
} else {
100-
throw new InvalidOptionException('Either specify a user_id or --all-users');
97+
$output->writeln("Remove deleted files for users on backend <info>$name</info>");
98+
$limit = 500;
99+
$offset = 0;
100+
do {
101+
$users = $backend->getUsers('', $limit, $offset);
102+
foreach ($users as $user) {
103+
$output->writeln(" <info>$user</info>");
104+
$this->removeDeletedFiles($user, $output, $verbose);
105+
}
106+
$offset += $limit;
107+
} while (count($users) >= $limit);
101108
}
109+
102110
return self::SUCCESS;
103111
}
104112

@@ -109,26 +117,29 @@ protected function removeDeletedFiles(string $uid, OutputInterface $output, bool
109117
\OC_Util::tearDownFS();
110118
\OC_Util::setupFS($uid);
111119
$path = '/' . $uid . '/files_trashbin';
112-
if ($this->rootFolder->nodeExists($path)) {
113-
$node = $this->rootFolder->get($path);
114120

115-
if ($verbose) {
116-
$output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
117-
}
118-
$node->delete();
119-
if ($this->rootFolder->nodeExists($path)) {
120-
$output->writeln("<error>Trash folder sill exists after attempting to delete it</error>");
121-
return;
122-
}
123-
$query = $this->dbConnection->getQueryBuilder();
124-
$query->delete('files_trash')
125-
->where($query->expr()->eq('user', $query->createParameter('uid')))
126-
->setParameter('uid', $uid);
127-
$query->execute();
128-
} else {
121+
if (!$this->rootFolder->nodeExists($path)) {
129122
if ($verbose) {
130123
$output->writeln("No trash found for <info>$uid</info>");
131124
}
125+
126+
return;
127+
}
128+
129+
$node = $this->rootFolder->get($path);
130+
131+
if ($verbose) {
132+
$output->writeln("Deleting <info>" . \OC_Helper::humanFileSize($node->getSize()) . "</info> in trash for <info>$uid</info>.");
133+
}
134+
$node->delete();
135+
if ($this->rootFolder->nodeExists($path)) {
136+
$output->writeln("<error>Trash folder sill exists after attempting to delete it</error>");
137+
return;
132138
}
139+
$query = $this->dbConnection->getQueryBuilder();
140+
$query->delete('files_trash')
141+
->where($query->expr()->eq('user', $query->createParameter('uid')))
142+
->setParameter('uid', $uid);
143+
$query->execute();
133144
}
134145
}

apps/files_trashbin/lib/Command/ExpireTrash.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6565
$users = $input->getArgument('user_id');
6666
if (!empty($users)) {
6767
foreach ($users as $user) {
68-
if ($this->userManager->userExists($user)) {
69-
$output->writeln("Remove deleted files of <info>$user</info>");
70-
$userObject = $this->userManager->get($user);
71-
$this->expireTrashForUser($userObject);
72-
} else {
68+
if (! $this->userManager->userExists($user)) {
7369
$output->writeln("<error>Unknown user $user</error>");
7470
return self::FAILURE;
7571
}
72+
73+
$output->writeln("Remove deleted files of <info>$user</info>");
74+
$userObject = $this->userManager->get($user);
75+
$this->expireTrashForUser($userObject);
7676
}
77-
} else {
78-
$p = new ProgressBar($output);
79-
$p->start();
80-
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
81-
$p->advance();
82-
$this->expireTrashForUser($user);
83-
});
84-
$p->finish();
85-
$output->writeln('');
77+
78+
return self::SUCCESS;
8679
}
80+
81+
$p = new ProgressBar($output);
82+
$p->start();
83+
$this->userManager->callForSeenUsers(function (IUser $user) use ($p) {
84+
$p->advance();
85+
$this->expireTrashForUser($user);
86+
});
87+
$p->finish();
88+
$output->writeln('');
89+
8790
return self::SUCCESS;
8891
}
8992

apps/files_trashbin/lib/Command/RestoreAllFiles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
136136
} else {
137137
throw new InvalidOptionException('Either specify a user_id or --all-users');
138138
}
139+
139140
return self::SUCCESS;
140141
}
141142

apps/files_trashbin/lib/Command/Size.php

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6161
$user = $input->getOption('user');
6262
$size = $input->getArgument('size');
6363

64-
if ($size) {
65-
$parsedSize = \OC_Helper::computerFileSize($size);
66-
if ($parsedSize === false) {
67-
$output->writeln("<error>Failed to parse input size</error>");
68-
return self::FAILURE;
69-
}
70-
if ($user) {
71-
$this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize);
72-
$this->commandBus->push(new Expire($user));
73-
} else {
74-
$this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize);
75-
$output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>");
76-
$output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>");
77-
}
78-
} else {
64+
if (!$size) {
7965
$this->printTrashbinSize($input, $output, $user);
66+
return self::SUCCESS;
67+
}
68+
69+
$parsedSize = \OC_Helper::computerFileSize($size);
70+
if ($parsedSize === false) {
71+
$output->writeln("<error>Failed to parse input size</error>");
72+
return self::FAILURE;
8073
}
8174

75+
if ($user) {
76+
$this->config->setUserValue($user, 'files_trashbin', 'trashbin_size', (string)$parsedSize);
77+
$this->commandBus->push(new Expire($user));
78+
return self::SUCCESS;
79+
}
80+
81+
$this->config->setAppValue('files_trashbin', 'trashbin_size', (string)$parsedSize);
82+
$output->writeln("<info>Warning: changing the default trashbin size will automatically trigger cleanup of existing trashbins,</info>");
83+
$output->writeln("<info>a users trashbin can exceed the configured size until they move a new file to the trashbin.</info>");
84+
8285
return self::SUCCESS;
8386
}
8487

@@ -101,40 +104,45 @@ private function printTrashbinSize(InputInterface $input, OutputInterface $outpu
101104

102105
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
103106
$output->writeln($userHumanSize);
104-
} else {
105-
$userValue = ($userSize < 0) ? 'default' : $userSize;
106-
$globalValue = ($globalSize < 0) ? 'default' : $globalSize;
107-
$this->writeArrayInOutputFormat($input, $output, [
108-
'user_size' => $userValue,
109-
'global_size' => $globalValue,
110-
'effective_size' => ($userSize < 0) ? $globalValue : $userValue,
111-
]);
107+
return;
112108
}
113-
} else {
114-
$users = [];
115-
$this->userManager->callForSeenUsers(function (IUser $user) use (&$users) {
116-
$users[] = $user->getUID();
117-
});
118-
$userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users);
119109

120-
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
121-
$output->writeln("Default size: $globalHumanSize");
122-
$output->writeln("");
123-
if (count($userValues)) {
124-
$output->writeln("Per-user sizes:");
125-
$this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
126-
return \OC_Helper::humanFileSize($size);
127-
}, $userValues));
128-
} else {
129-
$output->writeln("No per-user sizes configured");
130-
}
131-
} else {
132-
$globalValue = ($globalSize < 0) ? 'default' : $globalSize;
133-
$this->writeArrayInOutputFormat($input, $output, [
134-
'global_size' => $globalValue,
135-
'user_sizes' => $userValues,
136-
]);
110+
$userValue = ($userSize < 0) ? 'default' : $userSize;
111+
$globalValue = ($globalSize < 0) ? 'default' : $globalSize;
112+
$this->writeArrayInOutputFormat($input, $output, [
113+
'user_size' => $userValue,
114+
'global_size' => $globalValue,
115+
'effective_size' => ($userSize < 0) ? $globalValue : $userValue,
116+
]);
117+
return;
118+
}
119+
120+
$users = [];
121+
$this->userManager->callForSeenUsers(function (IUser $user) use (&$users) {
122+
$users[] = $user->getUID();
123+
});
124+
$userValues = $this->config->getUserValueForUsers('files_trashbin', 'trashbin_size', $users);
125+
126+
if ($input->getOption('output') == self::OUTPUT_FORMAT_PLAIN) {
127+
$output->writeln("Default size: $globalHumanSize");
128+
$output->writeln("");
129+
130+
if (!count($userValues)) {
131+
$output->writeln("No per-user sizes configured");
132+
return;
137133
}
134+
135+
$output->writeln("Per-user sizes:");
136+
$this->writeArrayInOutputFormat($input, $output, array_map(function ($size) {
137+
return \OC_Helper::humanFileSize($size);
138+
}, $userValues));
139+
return;
138140
}
141+
142+
$globalValue = ($globalSize < 0) ? 'default' : $globalSize;
143+
$this->writeArrayInOutputFormat($input, $output, [
144+
'global_size' => $globalValue,
145+
'user_sizes' => $userValues,
146+
]);
139147
}
140148
}

0 commit comments

Comments
 (0)