Completed
Pull Request — master (#13)
by Wachter
07:38
created

ScheduleTaskCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 29
ccs 2
cts 2
cp 1
rs 8.439
cc 5
eloc 17
nc 12
nop 2
crap 5
1
<?php
2
3
namespace Task\TaskBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Task\SchedulerInterface;
11
12
/**
13
 * Schedule task.
14
 *
15
 * @author @wachterjohannes <[email protected]>
16
 */
17
class ScheduleTaskCommand extends Command
18
{
19
    /**
20
     * @var SchedulerInterface
21
     */
22
    private $scheduler;
23 18
24
    public function __construct($name, SchedulerInterface $scheduler)
25 18
    {
26
        parent::__construct($name);
27 18
28 18
        $this->scheduler = $scheduler;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33 18
     */
34
    protected function configure()
35 12
    {
36 18
        $this
37 18
            ->setDescription('Run pending tasks')
38 18
            ->addArgument('handler', InputArgument::REQUIRED)
39 18
            ->addArgument('workload', InputArgument::OPTIONAL)
40
            ->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
41
            ->addOption('end-date', 'e', InputOption::VALUE_REQUIRED)
42
            ->addOption('key', 'k', InputOption::VALUE_REQUIRED);
43
    }
44 12
45
    /**
46 12
     * {@inheritdoc}
47 12
     */
48 12
    protected function execute(InputInterface $input, OutputInterface $output)
49 12
    {
50
        $handler = $input->getArgument('handler');
51
        $workload = $input->getArgument('workload');
52
        $cronExpression = $input->getOption('cron-expression');
53
        $endDateString = $input->getOption('end-date');
54
        $key = $input->getOption('key');
55
56
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
57
            $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handler, $workload));
58
        }
59
60
        $taskBuilder = $this->scheduler->createTask($input->getArgument('handler'), $input->getArgument('workload'));
61
62
        if ($cronExpression !== null) {
63
            $endDate = null;
64
            if ($endDateString !== null) {
65
                $endDate = new \DateTime($endDateString);
66
            }
67
68
            $taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
69
        }
70
71
        if ($key !== null) {
72
            $taskBuilder->setKey($key);
73
        }
74
75
        $taskBuilder->schedule();
76
    }
77
}
78