Skip to content

Commit

Permalink
Uses early returns.
Browse files Browse the repository at this point in the history
To improve code readability.

Signed-off-by: Faraz Samapoor <f.samapoor@gmail.com>
Signed-off-by: Faraz Samapoor <fsa@adlas.at>
  • Loading branch information
Faraz Samapoor authored and blizzz committed Jan 19, 2024
1 parent 1d18d70 commit e46d750
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
8 changes: 5 additions & 3 deletions apps/user_ldap/lib/Command/CheckGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}
return self::SUCCESS;
} elseif ($wasMapped) {
}

if ($wasMapped) {
$output->writeln('The group does not exist on LDAP anymore.');
if ($input->getOption('update')) {
$this->backend->getLDAPAccess($gid)->connection->clearCache();
$this->service->handleRemovedGroups([$gid]);
}
return self::SUCCESS;
} else {
throw new \Exception('The given group is not a recognized LDAP group.');
}

throw new \Exception('The given group is not a recognized LDAP group.');
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage(). '</error>');
return self::FAILURE;
Expand Down
8 changes: 5 additions & 3 deletions apps/user_ldap/lib/Command/CheckUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->updateUser($uid, $output);
}
return self::SUCCESS;
} elseif ($wasMapped) {
}

if ($wasMapped) {
$this->dui->markUser($uid);
$output->writeln('The user does not exists on LDAP anymore.');
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
. $uid . '"');
return self::SUCCESS;
} else {
throw new \Exception('The given user is not a recognized LDAP user.');
}

throw new \Exception('The given user is not a recognized LDAP user.');
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage(). '</error>');
return self::FAILURE;
Expand Down
9 changes: 4 additions & 5 deletions apps/user_ldap/lib/Command/DeleteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ protected function configure(): void {
;
}


protected function execute(InputInterface $input, OutputInterface $output): int {
$configPrefix = $input->getArgument('configID');

$success = $this->helper->deleteServerConfiguration($configPrefix);

if ($success) {
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
return self::SUCCESS;
} else {
if (!$success) {
$output->writeln("Cannot delete configuration with configID '{$configPrefix}'");
return self::FAILURE;
}

$output->writeln("Deleted configuration with configID '{$configPrefix}'");
return self::SUCCESS;
}
}
26 changes: 12 additions & 14 deletions apps/user_ldap/lib/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,20 @@ protected function configure(): void {

/**
* Tests whether the offset and limit options are valid
* @param int $offset
* @param int $limit
*
* @throws \InvalidArgumentException
*/
protected function validateOffsetAndLimit($offset, $limit): void {
if ($limit < 0) {
throw new \InvalidArgumentException('limit must be 0 or greater');
}
if ($offset < 0) {
throw new \InvalidArgumentException('offset must be 0 or greater');
}
if ($limit === 0 && $offset !== 0) {
throw new \InvalidArgumentException('offset must be 0 if limit is also set to 0');
}
if ($offset > 0 && ($offset % $limit !== 0)) {
throw new \InvalidArgumentException('offset must be a multiple of limit');
protected function validateOffsetAndLimit(int $offset, int $limit): void {
$message = match (true) {
($limit < 0) => 'limit must be 0 or greater',
($offset < 0) => 'offset must be 0 or greater',
($limit === 0 && $offset !== 0) => 'offset must be 0 if limit is also set to 0',
($offset > 0 && ($offset % $limit !== 0)) => 'offset must be a multiple of limit',
default => '',
};

if (!empty($message)) {
throw new \InvalidArgumentException($message);
}
}

Expand Down
5 changes: 1 addition & 4 deletions apps/user_ldap/lib/Command/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* save the configuration value as provided
* @param string $configID
* @param string $configKey
* @param string $configValue
*/
protected function setValue($configID, $key, $value): void {
protected function setValue(string $configID, string $key, string $value): void {
$configHolder = new Configuration($configID);
$configHolder->$key = $value;
$configHolder->saveConfiguration();
Expand Down
28 changes: 16 additions & 12 deletions apps/user_ldap/lib/Command/ShowConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* prints the LDAP configuration(s)
* @param string[] configID(s)
* @param InputInterface $input
* @param OutputInterface $output
*
* @param string[] $configIDs
*/
protected function renderConfigs($configIDs, $input, $output): void {
protected function renderConfigs(
array $configIDs,
InputInterface $input,
OutputInterface $output,
): void {
$renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
$showPassword = $input->getOption('show-password');

Expand All @@ -116,16 +119,17 @@ protected function renderConfigs($configIDs, $input, $output): void {
$table->setHeaders(['Configuration', $id]);
$table->setRows($rows);
$table->render();
} else {
foreach ($configuration as $key => $value) {
if ($key === 'ldapAgentPassword' && !$showPassword) {
$rows[$key] = '***';
} else {
$rows[$key] = $value;
}
continue;
}

foreach ($configuration as $key => $value) {
if ($key === 'ldapAgentPassword' && !$showPassword) {
$rows[$key] = '***';
} else {
$rows[$key] = $value;
}
$configs[$id] = $rows;
}
$configs[$id] = $rows;
}
if (!$renderTable) {
$this->writeArrayInOutputFormat($input, $output, $configs);
Expand Down

0 comments on commit e46d750

Please sign in to comment.