aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Course/GetCourse.php
blob: 97a777234336f0c07d64918220d2cf8c995cc213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

namespace Studip\Cli\Commands\Course;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Course;

class GetCourse extends Command
{
    protected static $defaultName = 'course:get';

    protected function configure(): void
    {
        $this->setDescription('Get data of Stud.IP courses.');
        $this->setHelp('This command will return the course data.');
        $this->addOption(
            'field',
            'f',
            InputOption::VALUE_OPTIONAL,
            'In which database field should be searched',
            'name'
        );
        $this->addArgument('needle', InputArgument::REQUIRED, 'Value to search for a course.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $needle = $input->getArgument('needle');
        $field = $input->getOption('field');

        if ($field === 'id') {
            $courses = array_filter([Course::find($needle)]);
        } else {
            $courses = Course::findBySQL($field . " LIKE :needle ORDER BY $field", [':needle' => "%$needle%"]);
        }

        if (empty($courses)) {
            $output->writeln('<error>Could not find courses</error>');
            return Command::FAILURE;
        }
        $data = [];

        foreach ($courses as $i =>  $course) {
            $data[] = [
                $course->id,
                $course->veranstaltungsnummer,
                $course->getFullName(),
                $course->getTextualSemester()
            ];
            if ($i + 1 < count($courses)) {
                $data[] = [new TableSeparator(), new TableSeparator()];
            }
        }

        $table = new Table($output);
        $table->setHeaders(['Id', 'Course-number', 'Name', 'Semester'])
            ->setRows($data)
            ->setStyle('box')
            ->render();

        return Command::SUCCESS;
    }
}