Completed
Pull Request — master (#5)
by Valentyn
07:26
created

FosoClientCommand::execute()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 11
cp 0
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 2
crap 2
1
<?php // https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/issues/544
2
3
namespace App\Command;
4
5
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
class FosoClientCommand extends Command
13
{
14
    /**
15
     * @var ClientManagerInterface
16
     */
17
    private $clientManager;
18
19
    /**
20
     * ClientCommand constructor.
21
     *
22
     * @param ClientManagerInterface $clientManager
23
     */
24
    public function __construct(ClientManagerInterface $clientManager)
25
    {
26
        parent::__construct();
27
28
        $this->clientManager = $clientManager;
29
    }
30
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('oauth:create-client')
35
            ->setDescription('Creates a new OAuth2 client')
36
            ->addOption(
37
                'redirect-uri',
38
                null,
39
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
40
                'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
41
                null
42
            )
43
            ->addOption(
44
                'grant-type',
45
                null,
46
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
47
                'Sets allowed grant type for client. Use this option multiple times to set multiple grant types..',
48
                null
49
            )
50
            ->setHelp(<<<EOT
51
The <info>%command.name%</info> command creates a new client.
52
53
<info>php %command.full_name% [--redirect-uri=...] [--grant-type=...]</info>
54
55
EOT
56
            )
57
        ;
58
    }
59
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $io = new SymfonyStyle($input, $output);
63
64
        $io->title('Client Credentials');
65
66
        // Create a new client
67
        $client = $this->clientManager->createClient();
68
        $client->setRedirectUris($input->getOption('redirect-uri'));
69
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
70
71
        // Save the client
72
        $this->clientManager->updateClient($client);
73
74
        // Give the credentials back to the user
75
        $headers = ['Client ID', 'Client Secret'];
76
        $rows = [
77
            [$client->getPublicId(), $client->getSecret()],
78
        ];
79
80
        $io->table($headers, $rows);
81
82
        return 0;
83
    }
84
}