diff --git a/src/Console/Command/.AuthCommand.php.swp b/src/Console/Command/.AuthLoginCommand.php.swp similarity index 79% rename from src/Console/Command/.AuthCommand.php.swp rename to src/Console/Command/.AuthLoginCommand.php.swp index 7fd81e8..7e576d6 100644 Binary files a/src/Console/Command/.AuthCommand.php.swp and b/src/Console/Command/.AuthLoginCommand.php.swp differ diff --git a/src/Console/Command/AuthLoginCommand.php b/src/Console/Command/AuthLoginCommand.php new file mode 100644 index 0000000..1d1cb3b --- /dev/null +++ b/src/Console/Command/AuthLoginCommand.php @@ -0,0 +1,67 @@ +setName('auth:login') + ->setDescription('Login on t411') + ->setHelp("The %command.name% "); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $configLoader = new ConfigLoader(); + $dialog = $this->getHelperSet()->get('dialog'); + $client = new Client(); + + if ($configLoader->configExists()) { + $continue = $dialog->ask( + $output, + 'The configuration file already exists. Do you want to continue? (y/n, default: y) ', + 'y' + ); + + if (!in_array($continue, ['y', 'yes'])) { + $output->writeln('Aborded.'); + + return; + } + } + + $username = $dialog->ask($output, 'Username: ', null); + $password = $dialog->ask($output, 'Password: ', null); + + try { + $response = $client->getAuthorization($username, $password); + + if ($response->hasError()) { + $output->writeln(sprintf( + 'Login failed: %s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); + + return; + } + + $configLoader->save(array( + 'auth' => array( + 'uid' => $response->getData()['uid'], + 'token' => $response->getData()['token'], + ) + )); + } catch (ClientException $e) { + $output->writeln(sprintf('An error occured. %s', $e->getMessage())); + } + } +}