Completed
Push — coding-style-checks ( b80fd2...3cea25 )
by Kamil
51:47 queued 28:55
created

CreateClientCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Bundle\AdminApiBundle\Command;
15
16
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
17
use Sylius\Bundle\AdminApiBundle\Model\Client;
18
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Arnaud Langlade <[email protected]>
25
 */
26
final class CreateClientCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setName('sylius:oauth-server:create-client')
35
            ->setDescription('Creates a new client')
36
            ->addOption(
37
                'redirect-uri',
38
                null,
39
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
40
                'Sets redirect uri for client.'
41
            )
42
            ->addOption(
43
                'grant-type',
44
                null,
45
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
46
                'Sets allowed grant type for client.'
47
            )
48
            ->setHelp(<<<EOT
49
The <info>%command.name%</info>command creates a new client.
50
<info>php %command.full_name% [--redirect-uri=...] [--grant-type=...] name</info>
51
EOT
52
            );
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function execute(InputInterface $input, OutputInterface $output): void
59
    {
60
        $clientManager = $this->getClientManager();
61
62
        /** @var Client $client */
63
        $client = $clientManager->createClient();
64
        $client->setRedirectUris($input->getOption('redirect-uri'));
65
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
66
        $clientManager->updateClient($client);
67
68
        $output->writeln(
69
            sprintf(
70
                'A new client with public id <info>%s</info>, secret <info>%s</info> has been added',
71
                $client->getPublicId(),
72
                $client->getSecret()
73
            )
74
        );
75
    }
76
77
    /**
78
     * @return ClientManagerInterface
79
     */
80
    private function getClientManager(): ClientManagerInterface
81
    {
82
        return $this->getContainer()->get('fos_oauth_server.client_manager.default');
83
    }
84
}
85