| 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 |  |  | namespace Sylius\Bundle\AdminApiBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use FOS\OAuthServerBundle\Model\ClientManagerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Sylius\Bundle\AdminApiBundle\Model\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Arnaud Langlade <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class CreateClientCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ->setName('sylius:oauth-server:create-client') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ->setDescription('Creates a new client') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 'redirect-uri', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'Sets redirect uri for client.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->addOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'grant-type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'Sets allowed grant type for client.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ->setHelp(<<<EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | The <info>%command.name%</info>command creates a new client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | <info>php %command.full_name% [--redirect-uri=...] [--grant-type=...] name</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $clientManager = $this->getClientManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         /** @var Client $client */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $client = $clientManager->createClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $client->setRedirectUris($input->getOption('redirect-uri')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $client->setAllowedGrantTypes($input->getOption('grant-type')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $clientManager->updateClient($client); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'A new client with public id <info>%s</info>, secret <info>%s</info> has been added', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $client->getPublicId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $client->getSecret() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return ClientManagerInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |     private function getClientManager() | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |         return $this->getContainer()->get('fos_oauth_server.client_manager.default'); | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  |