setName('streams') ->setDescription('List available streams.') ->addOption('language', null, InputOption::VALUE_REQUIRED, '') ->addOption('difficulty', null, InputOption::VALUE_REQUIRED, '') ->setHelp('The %command.name% 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('language available values:'); foreach ($liveCoding->getLanguages() as $key => $value) { $output->writeln(sprintf( ' %s or %s', $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('difficulty available values:'); foreach ($liveCoding->getDifficulties() as $key => $value) { $output->writeln(sprintf( ' %s or %s', $key, $value )); } $output->writeln(''); } $difficulty = null; } try { $streams = $liveCoding->getStreams($language, $difficulty); foreach ($streams as $stream) { $output->writeln(sprintf( '%s', $stream->getTitle() )); $output->writeln(sprintf( '%s', str_repeat('-', mb_strlen($stream->getTitle())) )); $output->writeln(sprintf( 'Author: %s', $stream->getAuthor() )); if (count($stream->getLanguages())) { $output->writeln(sprintf( 'Languages: %s', implode(', ', $stream->getLanguages()) )); $output->writeln(sprintf( 'Difficulty: %s', $stream->getDifficulty() )); } $output->writeln(sprintf( 'URL: %s', $stream->getUrl() )); $output->writeln(sprintf( 'Views: %d', $stream->getViews() )); $output->writeln(''); } } catch (Exception $e) { $output->writeln(sprintf('%s', $e->getMessage())); } } }