From cadc2c6eddc3e831ea9b1997facffd3f1624ae89 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Feb 2015 02:49:39 +0100 Subject: [PATCH] terms tree --- src/Console/Command/TermsTreeCommand.php | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/Console/Command/TermsTreeCommand.php diff --git a/src/Console/Command/TermsTreeCommand.php b/src/Console/Command/TermsTreeCommand.php new file mode 100644 index 0000000..d9a3ff5 --- /dev/null +++ b/src/Console/Command/TermsTreeCommand.php @@ -0,0 +1,76 @@ +setName('terms:tree') + ->setDescription('Show terms') + ->setHelp("The %command.name% show the terms"); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $client = new Client(); + $configLoader = new ConfigLoader(); + + if (!isset($configLoader->getConfig()['auth']['token'])) { + $output->writeln('You must login.'); + + return; + } + + $client->setAuthorization($configLoader->getConfig()['auth']['token']); + + try { + $categoriesReponse = $client->getCategoriesTree(); + $termsResponse = $client->getTermsTree(); + + foreach (array($categoriesReponse, $termsResponse) as $response) { + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); + + return; + } + } + + foreach ($termsResponse->getData() as $categoryId => $termTypes) { + foreach ($categoriesReponse->getData() as $id => $category) { + if ($categoryId === $id) { + if (isset($category['name'])) { + $output->writeln(sprintf('`- %s', $category['name'])); + } else { + $output->writeln(sprintf('`- %s', 'Not defined')); + } + } + + foreach ($termTypes as $termType) { + if (isset($termType['type'])) { + $output->writeln(sprintf(' +- %s', $termType['type'])); + } + + foreach ($termType['terms'] as $term) { + $output->writeln(sprintf(' | %s', $term)); + } + } + $output->writeln(''); + } + } + } catch (ClientException $e) { + $output->writeln(sprintf('An error occured. %s', $e->getMessage())); + } + } +}