From 553e8e871cd302e0a1a7b548746c1e60b8d995f7 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Feb 2015 02:15:42 +0100 Subject: [PATCH] categories tree command --- src/Console/Command/CategoriesTreeCommand.php | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Console/Command/CategoriesTreeCommand.php diff --git a/src/Console/Command/CategoriesTreeCommand.php b/src/Console/Command/CategoriesTreeCommand.php new file mode 100644 index 0000000..0a61eed --- /dev/null +++ b/src/Console/Command/CategoriesTreeCommand.php @@ -0,0 +1,66 @@ +setName('categories:tree') + ->setDescription('Show categories') + ->setHelp("The %command.name% show the categories"); + } + + 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 { + $response = $client->getCategoriesTree(); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); + + return; + } + + foreach ($response->getData() as $category) { + if (isset($category['name'])) { + $output->writeln(sprintf('%s', $category['name'])); + } + + if (!empty($category['cats'])) { + foreach ($category['cats'] as $subCategory) { + if (isset($subCategory['name'])) { + $output->writeln(sprintf('> %s', $subCategory['name'])); + } + } + } + + $output->writeln(''); + } + } catch (ClientException $e) { + $output->writeln(sprintf('An error occured. %s', $e->getMessage())); + } + } +}