Completed
Push — pull-request/9563 ( 4792b4 )
by Kamil
171:04 queued 152:13
created

CreateClientCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 23 1
A execute() 0 21 2
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
 * @deprecated Fetching dependencies directly from container is not recommended from Symfony 3.4. Extending `ContainerAwareCommand` will be removed in 2.0
25
 */
26
final class CreateClientCommand extends ContainerAwareCommand
27
{
28
    /**
29
     * @var ClientManagerInterface
30
     */
31
    private $clientManager;
32
33
    public function __construct(?string $name = null, ClientManagerInterface $clientManager = null)
34
    {
35
        parent::__construct($name);
36
37
        $this->clientManager = $clientManager;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected function configure(): void
44
    {
45
        $this
46
            ->setName('sylius:oauth-server:create-client')
47
            ->setDescription('Creates a new client')
48
            ->addOption(
49
                'redirect-uri',
50
                null,
51
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
52
                'Sets redirect uri for client.'
53
            )
54
            ->addOption(
55
                'grant-type',
56
                null,
57
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
58
                'Sets allowed grant type for client.'
59
            )
60
            ->setHelp(<<<EOT
61
The <info>%command.name%</info>command creates a new client.
62
<info>php %command.full_name% [--redirect-uri=...] [--grant-type=...] name</info>
63
EOT
64
            );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output): void
71
    {
72
        if (null === $this->clientManager) {
73
            @trigger_error(sprintf('Fetching services directly from the container is deprecated since Sylius 1.2 and will be removed in 2.0.'), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
74
            $this->clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
75
        }
76
77
        /** @var Client $client */
78
        $client = $this->clientManager->createClient();
79
        $client->setRedirectUris($input->getOption('redirect-uri'));
80
        $client->setAllowedGrantTypes($input->getOption('grant-type'));
81
        $this->clientManager->updateClient($client);
82
83
        $output->writeln(
84
            sprintf(
85
                'A new client with public id <info>%s</info>, secret <info>%s</info> has been added',
86
                $client->getPublicId(),
87
                $client->getSecret()
88
            )
89
        );
90
    }
91
}
92