From a9435906fe813abaf68a0c7021a05d977f3c96fe Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 25 Jun 2017 18:52:27 +0200 Subject: [PATCH] API refactoring --- src/Gist/Api/Client.php | 2 +- src/Gist/Command/CreateCommand.php | 43 ++++++++++++++------------- src/Gist/Command/UpdateCommand.php | 40 +++++++++++++------------ src/Gist/Controller/ApiController.php | 29 ++++++++++++++++++ src/Gist/Form/ApiCreateGistForm.php | 4 ++- src/Gist/Form/ApiUpdateGistForm.php | 1 + 6 files changed, 78 insertions(+), 41 deletions(-) diff --git a/src/Gist/Api/Client.php b/src/Gist/Api/Client.php index 29c26a0..b3055a5 100644 --- a/src/Gist/Api/Client.php +++ b/src/Gist/Api/Client.php @@ -19,7 +19,7 @@ class Client extends BaseClient const CREATE = '/en/api/create'; /** - * URI of updating + * URI of update * * @const string */ diff --git a/src/Gist/Command/CreateCommand.php b/src/Gist/Command/CreateCommand.php index 7aa7027..14fdd46 100644 --- a/src/Gist/Command/CreateCommand.php +++ b/src/Gist/Command/CreateCommand.php @@ -7,6 +7,8 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +use Propel\Runtime\Parser\YamlParser; +use Symfony\Component\Yaml\Yaml; /** * class CreateCommand. @@ -27,8 +29,9 @@ class CreateCommand extends Command ->addArgument('input', InputArgument::REQUIRED, 'Input') ->addArgument('type', InputArgument::OPTIONAL, 'Type', 'text') ->addOption('title', 't', InputOption::VALUE_REQUIRED, 'Title of the gist') - ->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url') - ->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id') + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response') + ->addOption('id', 'i', InputOption::VALUE_NONE, 'Display only the id of the gist') + ->addOption('json', 'j', InputOption::VALUE_NONE, 'Format the response to json') ->setHelp(<<--title, -t Defines a title - - --show-id, -i - Display only the Id of the gist - --show-url, -u - Display only the url of the gist + --id, -i + Display only the id of the gist + + --all, -a + Display all the response + + --json, -j + Format the response to json EOF ); } @@ -58,11 +64,10 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { - //$output->writeln(sprintf('%s bar.', 'test')); - $file = $input->getArgument('input'); $type = $input->getArgument('type'); $title = $input->getOption('title'); + $json = $input->getOption('json'); if ($file === '-') { $content = file_get_contents('php://stdin'); @@ -94,19 +99,17 @@ EOF $gist = $this->getSilexApplication()['api_client']->create($title, $type, $content); - if ($input->getOption('show-url')) { - $output->writeln($gist['url']); - - return true; + if ($input->getOption('id')) { + $id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id']; + $result = $json ? json_encode(array('id' => $id)) : $id; + } elseif ($input->getOption('all')) { + $result = $json ? json_encode($gist) : Yaml::dump($gist); + } else { + $url = $gist['url']; + $result = $json ? json_encode(array('url' => $url)) : $url; } - if ($input->getOption('show-id')) { - $output->writeln($gist['gist']['Id']); - - return true; - } - - $output->writeln(json_encode($gist)); + $output->writeln($result); } /** diff --git a/src/Gist/Command/UpdateCommand.php b/src/Gist/Command/UpdateCommand.php index ef09a00..a48ec14 100644 --- a/src/Gist/Command/UpdateCommand.php +++ b/src/Gist/Command/UpdateCommand.php @@ -7,6 +7,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Yaml\Yaml; /** * class UpdateCommand. @@ -26,8 +27,9 @@ class UpdateCommand extends Command ->setDescription('Update a gist using the API') ->addArgument('input', InputArgument::REQUIRED, 'Input') ->addOption('gist', null, InputOption::VALUE_REQUIRED, 'Id or File of the gist') - ->addOption('show-url', 'u', InputOption::VALUE_NONE, 'Display only the gist url') - ->addOption('show-id', 'i', InputOption::VALUE_NONE, 'Display only the gist Id') + ->addOption('all', 'a', InputOption::VALUE_NONE, 'Display all the response') + ->addOption('id', 'i', InputOption::VALUE_NONE, 'Display only the id of the gist') + ->addOption('json', 'j', InputOption::VALUE_NONE, 'Format the response to json') ->setHelp(<<--gist Defines the Gist to update by using its Id or its File - --show-id, -i - Display only the Id of the gist + --id, -i + Display only the id of the gist - --show-url, -u - Display only the url of the gist + --all, -a + Display all the response + + --json, -j + Format the response to json EOF ); } @@ -57,10 +62,9 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { - //$output->writeln(sprintf('%s bar.', 'test')); - $file = $input->getArgument('input'); $gist = $input->getOption('gist'); + $json = $input->getOption('json'); if ($file === '-') { $content = file_get_contents('php://stdin'); @@ -86,19 +90,17 @@ EOF $gist = $this->getSilexApplication()['api_client']->update($gist, $content); - if ($input->getOption('show-url')) { - $output->writeln($gist['url']); - - return true; + if ($input->getOption('id')) { + $id = isset($gist['gist']['id']) ? $gist['gist']['id'] : $gist['gist']['Id']; + $result = $json ? json_encode(array('id' => $id)) : $id; + } elseif ($input->getOption('all')) { + $result = $json ? json_encode($gist) : Yaml::dump($gist); + } else { + $url = $gist['url']; + $result = $json ? json_encode(array('url' => $url)) : $url; } - if ($input->getOption('show-id')) { - $output->writeln($gist['gist']['Id']); - - return true; - } - - $output->writeln(json_encode($gist)); + $output->writeln($result); } /** diff --git a/src/Gist/Controller/ApiController.php b/src/Gist/Controller/ApiController.php index 5b86bbe..1fd08d9 100644 --- a/src/Gist/Controller/ApiController.php +++ b/src/Gist/Controller/ApiController.php @@ -16,6 +16,13 @@ use Gist\Form\ApiUpdateGistForm; */ class ApiController extends Controller { + /** + * Creates a gist. + * + * @param Request $request + * + * @return JsonResponse + */ public function createAction(Request $request) { $app = $this->getApp(); @@ -56,6 +63,14 @@ class ApiController extends Controller return $this->invalidRequestResponse('Invalid field(s)'); } + /** + * Updates a gist. + * + * @param Request $request + * @param string $gist + * + * @return JsonResponse + */ public function updateAction(Request $request, $gist) { $app = $this->getApp(); @@ -106,6 +121,13 @@ class ApiController extends Controller return $this->invalidRequestResponse('Invalid field(s)'); } + /** + * Builds an invalid method response. + * + * @param mixed $message + * + * @return JsonResponse + */ protected function invalidMethodResponse($message = null) { $data = [ @@ -116,6 +138,13 @@ class ApiController extends Controller return new JsonResponse($data, 405); } + /** + * Builds an invalid request response. + * + * @param mixed $message + * + * @return JsonResponse + */ protected function invalidRequestResponse($message = null) { $data = [ diff --git a/src/Gist/Form/ApiCreateGistForm.php b/src/Gist/Form/ApiCreateGistForm.php index 27f3e4b..436f78a 100644 --- a/src/Gist/Form/ApiCreateGistForm.php +++ b/src/Gist/Form/ApiCreateGistForm.php @@ -16,7 +16,9 @@ class ApiCreateGistForm extends CreateGistForm { parent::build($options); - $this->builder->remove('cipher'); + $this->builder + ->remove('cipher') + ->remove('file'); return $this->builder; } diff --git a/src/Gist/Form/ApiUpdateGistForm.php b/src/Gist/Form/ApiUpdateGistForm.php index 2594d59..bc058e3 100644 --- a/src/Gist/Form/ApiUpdateGistForm.php +++ b/src/Gist/Form/ApiUpdateGistForm.php @@ -18,6 +18,7 @@ class ApiUpdateGistForm extends ApiCreateGistForm $this->builder ->remove('title') + ->remove('file') ->remove('type'); return $this->builder;