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

ScheduleTaskCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 29
ccs 0
cts 22
cp 0
rs 8.439
cc 5
eloc 17
nc 12
nop 2
crap 30
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
24 60
    public function __construct($name, SchedulerInterface $scheduler)
25
    {
26 60
        parent::__construct($name);
27
28 60
        $this->scheduler = $scheduler;
29 60
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 60
    protected function configure()
35
    {
36 40
        $this
37 60
            ->setDescription('Run pending tasks')
38 60
            ->addArgument('handler', InputArgument::REQUIRED)
39 60
            ->addArgument('workload', InputArgument::OPTIONAL)
40 60
            ->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
41 60
            ->addOption('end-date', 'e', InputOption::VALUE_REQUIRED)
42 60
            ->addOption('key', 'k', InputOption::VALUE_REQUIRED);
43 60
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
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);
0 ignored issues
show
Bug introduced by
The method cron() does not seem to exist on object<Task\TaskBuilderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        }
70
71
        if ($key !== null) {
72
            $taskBuilder->setKey($key);
73
        }
74
75
        $taskBuilder->schedule();
76
    }
77
}
78