Quellcode durchsuchen

refactory oauth:client:create command

Guillermo Espinoza vor 8 Jahren
Ursprung
Commit
dae5e9cf92
1 geänderte Dateien mit 44 neuen und 5 gelöschten Zeilen
  1. 44 5
      Command/OAuthClientCreateCommand.php

+ 44 - 5
Command/OAuthClientCreateCommand.php

@@ -3,6 +3,7 @@
 namespace Base\OAuthServerBundle\Command;
 
 use \Base\OAuthServerBundle\Entity\OAuthClient;
+use \FOS\OAuthServerBundle\Entity\ClientManager;
 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
@@ -19,10 +20,22 @@ class OAuthClientCreateCommand extends ContainerAwareCommand
             ->setHelp('This command allows you to create an OAuth client')
             ->addOption(
                 'redirect_uri',
-                'r',
+                null,
                 InputOption::VALUE_REQUIRED,
                 'OAuth Redirect URI',
                 'http://127.0.0.1/ftth/app_dev.php/login_check'
+            )
+            ->addOption(
+                'client_id',
+                null,
+                InputOption::VALUE_OPTIONAL,
+                'OAuth Client random Id'
+            )
+            ->addOption(
+                'client_secret',
+                null,
+                InputOption::VALUE_OPTIONAL,
+                'OAuth Client random Secret'
             );
     }
 
@@ -32,18 +45,44 @@ class OAuthClientCreateCommand extends ContainerAwareCommand
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
+        $redirectUri = $input->getOption('redirect_uri');
+        $random = $input->getOption('client_id');
+        $secret = $input->getOption('client_secret');
+        
+        $client = $this->createClient($redirectUri, $random, $secret);
+        
+        $output->writeln('OAuth client successfully generated!');
+        $output->writeln('<info>client_id:</info> '.$client->getPublicId());
+        $output->writeln('<info>client_secret:</info> '.$client->getSecret());
+    }
+    
+    /**
+     * @param string $redirectUri
+     * @param string $random
+     * @param string $secret
+     * 
+     * @return OAuthClient
+     */
+    protected function createClient($redirectUri, $random = null, $secret = null)
+    {
+        /* @var $clientManager ClientManager */
         $clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
         
+        /* @var $client OAuthClient */
         $client = $clientManager->createClient();
+        if ($random) {
+            $client->setRandomId($random);
+        }
+        if ($secret) {
+            $client->setSecret($secret);
+        }
         $client->setRedirectUris(array(
-            $input->getOption('redirect_uri'),
+            $redirectUri,
         ));
         $client->setAllowedGrantTypes(array_keys(OAuthClient::getGrantTypesChoices()));
         $clientManager->updateClient($client);
         
-        $output->writeln('OAuth client successfully generated!');
-        $output->writeln('<info>client_id:</info> '.$client->getPublicId());
-        $output->writeln('<info>client_secret:</info> '.$client->getSecret());
+        return $client;
     }
 
 }