livecoding-console/src/Deblan/Command/StreamsCommand.php

120 lines
3.8 KiB
PHP
Raw Normal View History

2015-07-14 00:19:59 +02:00
<?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;
2015-07-14 01:28:36 +02:00
class StreamsCommand extends Command
2015-07-14 00:19:59 +02:00
{
protected function configure()
{
$this
->setName('streams')
->setDescription('List available streams.')
->addOption('language', null, InputOption::VALUE_REQUIRED, '')
->addOption('difficulty', null, InputOption::VALUE_REQUIRED, '')
2015-07-14 01:09:51 +02:00
->setHelp('The <info>%command.name%</info> lists live streams');
2015-07-14 00:19:59 +02:00
}
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))) {
2015-07-14 01:09:51 +02:00
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)) {
2015-07-14 01:09:51 +02:00
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;
}
2015-07-14 00:19:59 +02:00
try {
$streams = $liveCoding->getStreams($language, $difficulty);
2015-07-14 00:19:59 +02:00
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(
2015-07-14 00:19:59 +02:00
'<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()));
}
}
}