livecoding-console/src/Deblan/Command/StreamsCommand.php
2015-07-14 01:28:36 +02:00

120 lines
3.8 KiB
PHP

<?php
namespace Deblan\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Exception;
class StreamsCommand extends Command
{
protected function configure()
{
$this
->setName('streams')
->setDescription('List available streams.')
->addOption('language', null, InputOption::VALUE_REQUIRED, '')
->addOption('difficulty', null, InputOption::VALUE_REQUIRED, '')
->setHelp('The <info>%command.name%</info> lists live streams');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$liveCoding = $this->getApplication()->getContainer()['livecoding'];
$languages = $liveCoding->getLanguages();
$difficulties = $liveCoding->getDifficulties();
$language = $input->getOption('language');
$difficulty = $input->getOption('difficulty');
if (in_array($language, $languages)) {
$language = array_keys($languages, $language)[0];
} elseif (!in_array($language, array_keys($languages))) {
if ($language !== null) {
$output->writeln('<info>language</info> available values:');
foreach ($liveCoding->getLanguages() as $key => $value) {
$output->writeln(sprintf(
' <comment>%s</comment> or <comment>%s</comment>',
$key,
$value
));
}
$output->writeln('');
}
$language = null;
}
if (in_array($difficulty, array_keys($difficulties))) {
$difficulty = $difficulties[$difficulty];
} elseif (!in_array($difficulty, $difficulties)) {
if ($difficulty !== null) {
$output->writeln('<info>difficulty</info> available values:');
foreach ($liveCoding->getDifficulties() as $key => $value) {
$output->writeln(sprintf(
' <comment>%s</comment> or <comment>%s</comment>',
$key,
$value
));
}
$output->writeln('');
}
$difficulty = null;
}
try {
$streams = $liveCoding->getStreams($language, $difficulty);
foreach ($streams as $stream) {
$output->writeln(sprintf(
'<comment>%s</comment>',
$stream->getTitle()
));
$output->writeln(sprintf(
'<comment>%s</comment>',
str_repeat('-', mb_strlen($stream->getTitle()))
));
$output->writeln(sprintf(
'<info>Author:</info> %s',
$stream->getAuthor()
));
if (count($stream->getLanguages())) {
$output->writeln(sprintf(
'<info>Languages:</info> %s',
implode(', ', $stream->getLanguages())
));
$output->writeln(sprintf(
'<info>Difficulty:</info> %s',
$stream->getDifficulty()
));
}
$output->writeln(sprintf(
'<info>URL:</info> %s',
$stream->getUrl()
));
$output->writeln(sprintf(
'<info>Views:</info> %d',
$stream->getViews()
));
$output->writeln('');
}
} catch (Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
}
}