Completed
Push — master ( 0c7a1a...95af33 )
by Wachter
12:11 queued 07:39
created

ScheduleTaskCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 54
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50 54
        $handler = $input->getArgument('handler');
51 54
        $workload = $input->getArgument('workload');
52 54
        $cronExpression = $input->getOption('cron-expression');
53 54
        $endDateString = $input->getOption('end-date');
54 54
        $key = $input->getOption('key');
55
56 54
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
57
            $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handler, $workload));
58
        }
59
60 54
        $taskBuilder = $this->scheduler->createTask($input->getArgument('handler'), $input->getArgument('workload'));
61
62 54
        if ($cronExpression !== null) {
63 24
            $endDate = null;
64 24
            if ($endDateString !== null) {
65 12
                $endDate = new \DateTime($endDateString);
66 8
            }
67
68 24
            $taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
69 15
        }
70
71 53
        if ($key !== null) {
72 24
            $taskBuilder->setKey($key);
73 16
        }
74
75 53
        $taskBuilder->schedule();
76 53
    }
77
}
78