setName('categories:tree')
->setDescription('Show categories and sub-categories')
->setHelp("%command.name%
List all categories and sub-categories with IDs.
Usage: categories:tree");
}
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'])) {
$category['name'] = 'Category name not defined';
}
$output->writeln(sprintf('%3d %s', isset($category['id']) ? $category['id'] : null, $category['name']));
if (!empty($category['cats'])) {
$isFirst = true;
foreach ($category['cats'] as $subCategoryId => $subCategory) {
$char = '|';
if ($isFirst) {
$isFirst = false;
$char = '`';
}
$output->writeln(sprintf(' %s- %4d %s', $char, $subCategoryId, $subCategory['name']));
}
}
$output->writeln('');
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. %s', $e->getMessage()));
}
}
}