Skip to content

Commit 781f307

Browse files
committed
Fix UpdateClientCommand options
1 parent 6d65806 commit 781f307

1 file changed

Lines changed: 75 additions & 31 deletions

File tree

Command/UpdateClientCommand.php

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,30 @@ protected function configure(): void
3939
->addOption(
4040
'redirect-uri',
4141
null,
42-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
43-
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
44-
[]
42+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
43+
'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs. Use it without value to remove existing values.',
44+
[0]
4545
)
4646
->addOption(
4747
'grant-type',
4848
null,
49-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
50-
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types.',
51-
[]
49+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
50+
'Sets allowed grant type for client. Use this option multiple times to set multiple grant types. Use it without value to remove existing values.',
51+
[0]
5252
)
5353
->addOption(
5454
'scope',
5555
null,
56-
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
57-
'Sets allowed scope for client. Use this option multiple times to set multiple scopes.',
58-
[]
56+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
57+
'Sets allowed scope for client. Use this option multiple times to set multiple scopes. Use it without value to remove existing values.',
58+
[0]
5959
)
6060
->addOption(
61-
'deactivated',
61+
'active',
6262
null,
63-
InputOption::VALUE_NONE,
64-
'If provided, it will deactivate the given client.'
63+
InputOption::VALUE_REQUIRED,
64+
'Client active state, 1 for active, 0 for inactive',
65+
null
6566
)
6667
->addArgument(
6768
'identifier',
@@ -90,26 +91,69 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9091

9192
private function updateClientFromInput(Client $client, InputInterface $input): Client
9293
{
93-
$client->setActive(!$input->getOption('deactivated'));
94-
95-
$redirectUris = array_map(
96-
static function (string $redirectUri): RedirectUri { return new RedirectUri($redirectUri); },
97-
$input->getOption('redirect-uri')
98-
);
99-
$client->setRedirectUris(...$redirectUris);
100-
101-
$grants = array_map(
102-
static function (string $grant): Grant { return new Grant($grant); },
103-
$input->getOption('grant-type')
104-
);
105-
$client->setGrants(...$grants);
106-
107-
$scopes = array_map(
108-
static function (string $scope): Scope { return new Scope($scope); },
109-
$input->getOption('scope')
110-
);
111-
$client->setScopes(...$scopes);
94+
$active = $input->getOption('active');
95+
96+
if (null !== $active) {
97+
$client->setActive((bool) $active);
98+
}
99+
100+
$redirectUrisArray = $this->getNullableOption($input, 'redirect-uri');
101+
102+
if (null !== $redirectUrisArray) {
103+
$redirectUris = array_map(
104+
static function (string $redirectUri): RedirectUri {
105+
return new RedirectUri($redirectUri);
106+
},
107+
$redirectUrisArray
108+
);
109+
$client->setRedirectUris(...$redirectUris);
110+
}
111+
112+
$grantsArray = $this->getNullableOption($input, 'grant-type');
113+
114+
if (null !== $grantsArray) {
115+
$grants = array_map(
116+
static function (string $grant): Grant {
117+
return new Grant($grant);
118+
},
119+
$grantsArray
120+
);
121+
$client->setGrants(...$grants);
122+
}
123+
124+
$scopesArray = $this->getNullableOption($input, 'scope');
125+
126+
if (null !== $scopesArray) {
127+
$scopes = array_map(
128+
static function (string $scope): Scope {
129+
return new Scope($scope);
130+
},
131+
$scopesArray
132+
);
133+
$client->setScopes(...$scopes);
134+
}
112135

113136
return $client;
114137
}
138+
139+
private function getNullableOption(InputInterface $input, string $name): ?array
140+
{
141+
$value = $input->getOption($name);
142+
143+
if (
144+
array_key_exists(0, $value)
145+
&& 0 === $value[0] //if user has entered some value it will always be string so it is fine to rely on 0
146+
) {
147+
return null;
148+
}
149+
150+
if (
151+
array_key_exists(0, $value)
152+
&& null === $value[0] //when option has mode InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY and no value is sent, option will have value [null]
153+
) {
154+
return [];
155+
}
156+
157+
return $value;
158+
}
115159
}

0 commit comments

Comments
 (0)