Completed
Push — master ( 817fef...b9c500 )
by Allan
06:04
created

CreateClientCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 8.9714
cc 2
eloc 16
nc 2
nop 2
crap 6
1
<?php
2
3
namespace SMG\OauthBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CreateClientCommand extends ContainerAwareCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('acme:oauth-server:client:create')
16
            ->setDescription('Creates a new client')
17
            ->addOption(
18
                'redirect-uri',
19
                null,
20
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
21
                'Sets redirect uri for client. Use this option multiple times to set multiple redirect URIs.',
22
                null
23
            )
24
            ->addOption(
25
                'grant-type',
26
                null,
27
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
28
                'Sets allowed grant type for client. Use this option multiple times to set multiple grant types..',
29
                null
30
            )
31
            ->addOption(
32
                'client-type',
33
                null,
34
                InputOption::VALUE_OPTIONAL,
35
                'Set type of client. Use this option to know from which client an user token comes from and assign privileges to this user.'
36
            )
37
            ->addOption(
38
                'meta',
39
                null,
40
                InputOption::VALUE_OPTIONAL,
41
                'Set meta information on the client, useful to put application specific information'
42
            )
43
            ->setHelp(
44
                <<<EOT
45
                    The <info>%command.name%</info>command creates a new client.
46
47
<info>php %command.full_name% [--redirect-uri=...] [--grant-type=...] [--meta=...] name</info>
48
49
EOT
50
            );
51
    }
52
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
56
        $client = $clientManager->createClient();
57
        $client->setRedirectUris($input->getOption('redirect-uri'));
58
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
59
        $client->setType($input->getOption('client-type'));
60
61
        $metaStr = $input->getOption('meta');
62
63
        if (!is_null($metaStr)) {
64
            $meta = json_decode($metaStr, true);
65
            $client->setMeta($meta);
66
        }
67
68
        $clientManager->updateClient($client);
69
        $output->writeln(
70
            sprintf(
71
                'Added a new client with public id <info>%s</info>, secret <info>%s</info>',
72
                $client->getPublicId(),
73
                $client->getSecret()
74
            )
75
        );
76
    }
77
}
78