1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class CreateOAuthClientCommand extends ContainerAwareCommand |
11
|
|
|
{ |
12
|
|
|
protected function configure() |
13
|
|
|
{ |
14
|
|
|
$this |
15
|
|
|
->setName('app:oauth-client:create') |
16
|
|
|
->setDescription('Create OAuth Client.') |
17
|
|
|
->addArgument('redirectUri', InputArgument::REQUIRED, 'The redirect uri') |
18
|
|
|
->addArgument('grantType', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The grant type') |
19
|
|
|
->setHelp(<<<EOT |
20
|
|
|
The <info>app:oauth:create</info> command creates a OAuth Client: |
21
|
|
|
|
22
|
|
|
<info>php app/console app:oauth:create</info> |
23
|
|
|
|
24
|
|
|
This interactive shell will ask you for an client name, a redirect uri and then a grant type. |
25
|
|
|
|
26
|
|
|
EOT |
27
|
|
|
); |
28
|
|
|
; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
32
|
|
|
{ |
33
|
|
|
$redirectUri = $input->getArgument('redirectUri'); |
34
|
|
|
$grantType = $input->getArgument('grantType'); |
35
|
|
|
$grantTypes = explode(' ', $grantType); |
36
|
|
|
|
37
|
|
|
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default'); |
38
|
|
|
$client = $clientManager->createClient(); |
39
|
|
|
$client->setRedirectUris([$redirectUri]); |
40
|
|
|
$client->setAllowedGrantTypes($grantTypes); |
41
|
|
|
$clientManager->updateClient($client); |
42
|
|
|
|
43
|
|
|
$output->writeln(sprintf('Created OAuth Client')); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
View Code Duplication |
if (!$input->getArgument('redirectUri')) { |
|
|
|
|
51
|
|
|
$redirectUri = $this->getHelper('dialog')->askAndValidate( |
|
|
|
|
52
|
|
|
$output, |
53
|
|
|
'Please choose an Redirect Uri:', |
54
|
|
|
function($redirectUri) { |
55
|
|
|
if (empty($redirectUri)) { |
56
|
|
|
throw new \Exception('Redirect Uri can not be empty'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $redirectUri; |
60
|
|
|
} |
61
|
|
|
); |
62
|
|
|
$input->setArgument('redirectUri', $redirectUri); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
if (!$input->getArgument('grantType')) { |
|
|
|
|
66
|
|
|
$grantType = $this->getHelper('dialog')->askAndValidate( |
|
|
|
|
67
|
|
|
$output, |
68
|
|
|
'Please choose a Grant Types (separate multiple grant type with a space):', |
69
|
|
|
function($grantType) { |
70
|
|
|
if (empty($grantType)) { |
71
|
|
|
throw new \Exception('Grant Type can not be empty'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $grantType; |
75
|
|
|
} |
76
|
|
|
); |
77
|
|
|
$input->setArgument('grantType', $grantType); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.