Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 12 additions & 37 deletions lib/Command/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\Core\Command\Base;
use OCA\LDAPContactsBackend\Service\Configuration;
use OCA\LDAPContactsBackend\Service\ConnectionImporter;
use RuntimeException;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -37,10 +38,8 @@
use Symfony\Component\Console\Question\Question;

class Add extends Base {
/** @var Configuration */
private $configurationService;
/** @var ConnectionImporter|null */
private $connectionImporter;
private Configuration $configurationService;
private ?ConnectionImporter $connectionImporter;

public function __construct(Configuration $configurationService, ?ConnectionImporter $connectionImporter = null) {
parent::__construct();
Expand Down Expand Up @@ -147,7 +146,7 @@ protected function interact(InputInterface $input, OutputInterface $output) {
$helper = $this->getHelper('question');

$q = new Question('Address book display name: ');
$q->setNormalizer(function ($input) {
$q->setNormalizer(function (string $input) {
return $this->stringNormalizer($input);
});

Expand All @@ -173,16 +172,11 @@ protected function interact(InputInterface $input, OutputInterface $output) {
$q = new Question('Transport encryption: ');
$q->setAutocompleterValues(['StartTLS', 'LDAPS', 'none']);

switch ($helper->ask($input, $output, $q)) {
case 'StartTLS':
$v = 'tls';
break;
case 'LDAPS':
$v = 'ssl';
break;
default:
$v = 'none';
}
$v = match ($helper->ask($input, $output, $q)) {
'StartTLS' => 'tls',
'LDAPS' => 'ssl',
default => 'none',
};

$input->setOption('trans_enc', $v);
}
Expand Down Expand Up @@ -306,18 +300,6 @@ protected function importConnection(InputInterface $input) {
$wasRun = true;
}

protected function askArrayOfString(string $subject, string $label, InputInterface $input, OutputInterface $output): void {
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');

$q = new Question($label);
$q->setNormalizer(function ($input) {
return $this->arrayOfStringNormalizer($input);
});

$input->setOption($subject, $helper->ask($input, $output, $q));
}

protected function askImport(InputInterface $input, OutputInterface $output): void {
$availableConnections = $this->connectionImporter ? $this->connectionImporter->getAvailableConnections() : [];
if (count($availableConnections) === 0) {
Expand Down Expand Up @@ -393,15 +375,8 @@ protected function askUnsignedInt(string $subject, string $label, InputInterface
$input->setOption($subject, $helper->ask($input, $output, $q));
}

protected function stringNormalizer($input) {
return $input ? trim($input) : '';
}

protected function arrayOfStringNormalizer(string $input) {
foreach ($input as &$item) {
$item = $this->stringNormalizer($item);
}
return $input;
protected function stringNormalizer(?string $input): string {
return ($input !== null) ? trim($input) : '';
}

protected function askStrings(string $subject, string $label, string $followUpLabel, InputInterface $input, OutputInterface $output): void {
Expand Down Expand Up @@ -435,7 +410,7 @@ protected function posNumberNormalizer(?string $input): ?int {
$input = (int)$input;
}
if (is_int($input) && $input < 0) {
throw new \RuntimeException('Port must not be negative');
throw new RuntimeException('Port must not be negative');
}
return $input;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Command/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
use Symfony\Component\Console\Output\OutputInterface;

class Delete extends Base {
/** @var Configuration */
private $configurationService;
private Configuration $configurationService;

public function __construct(Configuration $configurationService) {
parent::__construct();
Expand Down
29 changes: 12 additions & 17 deletions lib/Command/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@

namespace OCA\LDAPContactsBackend\Command;

use Generator;
use OC\Core\Command\Base;
use OCA\LDAPContactsBackend\Model\Configuration as ConfigurationModel;
use OCA\LDAPContactsBackend\Service\Configuration;
use RuntimeException;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -38,8 +40,7 @@
class Edit extends Base {
use TConfigurationDetail;

/** @var Configuration */
private $configurationService;
private Configuration $configurationService;

public function __construct(Configuration $configurationService) {
parent::__construct();
Expand All @@ -64,7 +65,7 @@ protected function configure() {
$this->configureOptions();
}

protected function getListOfOptions(ConfigurationModel $model): \Generator {
protected function getListOfOptions(ConfigurationModel $model): Generator {
yield [
'key' => 'addressBookName',
'type' => 'string',
Expand Down Expand Up @@ -200,16 +201,10 @@ public function execute(InputInterface $input, OutputInterface $output): int {

foreach ($this->getListOfOptions($config) as $optionData) {
if (!empty($input->getOption($optionData['key']))) {
switch ($optionData['type']) {
case 'uint':
$v = max((int)$input->getOption($optionData['key']), 0);
break;
case 'string':
case 'array-string':
case 'cs-string':
default:
$v = $input->getOption($optionData['key']);
}
$v = match ($optionData['type']) {
'uint' => max((int)$input->getOption($optionData['key']), 0),
default => $input->getOption($optionData['key']),
};
$optionData['setter']($v);
}
}
Expand All @@ -233,20 +228,20 @@ private function yesOrNoNormalizer(string $input): ?bool {
return null;
}

private function stringNormalizer($input) {
return $input ? trim($input) : '';
private function stringNormalizer(?string $input): string {
return ($input !== null) ? trim($input) : '';
}

private function autoCompleteNormalizer($input, array $autoComplete) {
return $v = array_change_key_case(array_flip($autoComplete))[strtolower($input)] ?? array_pop($autoComplete);
return array_change_key_case(array_flip($autoComplete))[strtolower($input)] ?? array_pop($autoComplete);
}

private function uIntNormalizer(?string $input): ?int {
if (is_string($input)) {
$input = (int)$input;
}
if (is_int($input) && $input < 0) {
throw new \RuntimeException('Port must not be negative');
throw new RuntimeException('Port must not be negative');
}
return $input;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Command/ListConfigs.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
use Symfony\Component\Console\Output\OutputInterface;

class ListConfigs extends Base {
/** @var Configuration */
private $configurationService;
private Configuration $configurationService;

public function __construct(Configuration $configurationService) {
parent::__construct();
Expand Down
145 changes: 72 additions & 73 deletions lib/Command/TConfigurationDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,85 +25,84 @@

namespace OCA\LDAPContactsBackend\Command;

use LogicException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;

trait TConfigurationDetail {
public function configureOptions() {
public function configureOptions(): void {
if (!$this instanceof Command) {
throw new \LogicException('Trait applied on wrong base class');
throw new LogicException('Trait applied on wrong base class');
}

if ($this instanceof Command) {
$this
->addOption(
'host',
null,
InputOption::VALUE_REQUIRED,
'LDAP host name'
)
->addOption(
'port',
null,
InputOption::VALUE_REQUIRED,
'LDAP Port'
)
->addOption(
'trans_enc',
null,
InputOption::VALUE_REQUIRED,
'Transport encryption'
)
->addOption(
'bindDN',
null,
InputOption::VALUE_REQUIRED,
'LDAP Bind DN'
)
->addOption(
'bindPwd',
null,
InputOption::VALUE_REQUIRED,
'LDAP Bind Password'
)
->addOption(
'filter',
null,
InputOption::VALUE_REQUIRED,
'LDAP filter'
)
->addOption(
'base',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP contacts bases'
)
->addOption(
'attrs',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP Search Attributes',
null
)
->addOption(
'mapping',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP to VCard mapping (e.g. --mapping=EMAIL:mail --mapping=TEL:mobile,homePhone)',
null
)
->addOption(
'disable',
null,
InputOption::VALUE_NONE,
'keep configuration disabled'
)
->addOption(
'interactive',
'i',
InputOption::VALUE_NONE,
'Interactive mode'
);
}
$this
->addOption(
'host',
null,
InputOption::VALUE_REQUIRED,
'LDAP host name'
)
->addOption(
'port',
null,
InputOption::VALUE_REQUIRED,
'LDAP Port'
)
->addOption(
'trans_enc',
null,
InputOption::VALUE_REQUIRED,
'Transport encryption'
)
->addOption(
'bindDN',
null,
InputOption::VALUE_REQUIRED,
'LDAP Bind DN'
)
->addOption(
'bindPwd',
null,
InputOption::VALUE_REQUIRED,
'LDAP Bind Password'
)
->addOption(
'filter',
null,
InputOption::VALUE_REQUIRED,
'LDAP filter'
)
->addOption(
'base',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP contacts bases'
)
->addOption(
'attrs',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP Search Attributes',
null
)
->addOption(
'mapping',
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'LDAP to VCard mapping (e.g. --mapping=EMAIL:mail --mapping=TEL:mobile,homePhone)',
null
)
->addOption(
'disable',
null,
InputOption::VALUE_NONE,
'keep configuration disabled'
)
->addOption(
'interactive',
'i',
InputOption::VALUE_NONE,
'Interactive mode'
);
}
}
Loading