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); |
|
|
|
|
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
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: