forked from deblan/t411-console
categories tree command
This commit is contained in:
parent
5618c77b77
commit
553e8e871c
66
src/Console/Command/CategoriesTreeCommand.php
Normal file
66
src/Console/Command/CategoriesTreeCommand.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Console\Command;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Api\Client;
|
||||
use Api\ConfigLoader;
|
||||
|
||||
class CategoriesTreeCommand extends Command
|
||||
{
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setName('categories:tree')
|
||||
->setDescription('Show categories')
|
||||
->setHelp("The <info>%command.name%</info> 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(
|
||||
'<error>%s</error> <comment>(%d)</comment>',
|
||||
$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. <error>%s</error>', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue