aboutsummaryrefslogtreecommitdiff
path: root/cli/Commands/Cronjobs/CronjobExecute.php
blob: 93b5ddbfc0b066fe141c0b4634763fd295741f8f (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php

namespace Studip\Cli\Commands\Cronjobs;

use Symfony\Component\Console\Command\Command;
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 Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class CronjobExecute extends Command
{
    protected static $defaultName = 'cronjobs:execute';

    protected function configure(): void
    {
        $this->setDescription('Execute cronjob task.');
        $this->setHelp('This command will execute a cronjob task.');

        $this->addArgument(
            'task_id',
            InputArgument::OPTIONAL,
            'Id of the desired cron job'
        );

        $this->addOption(
            'input',
            'i',
            InputOption::VALUE_NONE,
            'Interactively input values (defaults to true if no task_id is given)'
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $helper = $this->getHelper('question');
        $task_id = $input->getArgument('task_id');

        $input_values = $task_id === null || $input->getOption('input');

        if ($task_id === null) {
            $question = new ChoiceQuestion(
                "\nWhich cronjob should be executed:\n",
                $this->getCronjobTaskList()
            );
            $task_id = $helper->ask($input, $output, $question);
        }

        $task = \CronjobTask::find($task_id);
        if (!$task) {
            $output->writeln('<error>Unknown task id</error>');
            return Command::FAILURE;
        }
        if (!$task->valid) {
            $output->writeln(sprintf(
                '<error>Invalid task, unknown filename %s or invalid class %s</error>',
                $task->filename,
                $task->class
            ));
            return Command::FAILURE;
        }

        $parameters = $this->getDefaultTaskParameters($task);

        if ($input_values && count($parameters) > 0) {
            $output->writeln("\nParameters:\n");

            foreach ($task->parameters as $key => $definition) {
                $description = trim($definition['description'], ' ?');
                $default = trim(json_encode($definition['default'] ?? null), "'");
                $label = " > {$description} [<comment>{$default}</comment>] : ";

                if ($definition['type'] === 'boolean') {
                    $question = new ConfirmationQuestion(
                        $label,
                        $definition['default'],
                        '/^(y|j|1)/i'
                    );
                } elseif ($definition['type'] === 'select' && !empty($definition['values'])) {
                    $question = new ChoiceQuestion(
                        $label,
                        $definition['values']
                    );
                } else {
                    $question = new Question(
                        $label,
                        $definition['default']
                    );
                    if ($definition['type'] === 'integer') {
                        $question->setNormalizer(function ($value) {
                            return $value ? trim($value) : '';
                        })->setValidator(function ($value): int {
                            if (strlen($value) && !ctype_digit($value)) {
                                throw new \RuntimeException('Number is invalid.');
                            }
                            return (int) $value;
                        });
                    }
                }
                $parameters[$key] = $helper->ask($input, $output, $question);
            }
        }

        $task->engage('', $parameters);

        return Command::SUCCESS;
    }

    protected function getCronjobTaskList(): array
    {
        $result = [];
        \CronjobTask::findEachBySQL(
            function (\CronjobTask $task) use (&$result): void
            {
                $result[$task->id] = $task->name;
            },
            '1'
        );
        return $result;
    }

    private function getDefaultTaskParameters(\CronjobTask $task): array
    {
        $parameters = [];
        foreach ($task->parameters as $key => $definition) {
            $parameters[$key] = $definition['default'] ?? null;
        }
        return $parameters;
    }
}