Completed
Pull Request — master (#32)
by Wachter
11:41 queued 09:20
created

ScheduleTaskCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Task\Scheduler\TaskSchedulerInterface;
20
21
/**
22
 * Schedule task.
23
 */
24
class ScheduleTaskCommand extends Command
25
{
26
    /**
27
     * @var TaskSchedulerInterface
28
     */
29
    private $scheduler;
30
31
    /**
32
     * @param string $name
33
     * @param TaskSchedulerInterface $runner
34
     */
35 12
    public function __construct($name, TaskSchedulerInterface $runner)
36
    {
37 12
        parent::__construct($name);
38
39 12
        $this->scheduler = $runner;
40 12
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 12
    protected function configure()
46
    {
47 12
        $this
48 12
            ->setDescription('Schedule task')
49 12
            ->setHelp(<<<'EOT'
50
The <info>%command.name%</info> command schedules given handler.
51
52
    $ %command.full_name% AppBundle\\Handler\\ImageHandler
53
54
Execute without any arguments in order to see schedule a single task, use the
55
<comment>--workload</comment> option in order to specify a workload or
56
<comment>--cron-expression</comment> to create a recurring task.
57
EOT
58 12
            )
59 12
            ->addArgument('handlerClass', InputArgument::REQUIRED, 'Handler which will be called')
60 12
            ->addArgument('workload', InputArgument::OPTIONAL, 'This will be passed to the handler')
61 12
            ->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED, 'Specifies interval for recurring task')
62 12
            ->addOption('execution-date', null, InputOption::VALUE_REQUIRED, 'Specifies execution-date for single task')
63 12
            ->addOption('end-date', null, InputOption::VALUE_REQUIRED, 'Specifies last run date for recurring tasks');
64 12
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 5
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71 5
        $handlerClass = $input->getArgument('handlerClass');
72 5
        $workload = $input->getArgument('workload');
73 5
        $cronExpression = $input->getOption('cron-expression');
74 5
        $executionDateString = $input->getOption('execution-date');
75 5
        $endDateString = $input->getOption('end-date');
76
77 5
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
78
            $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload));
79
        }
80
81 5
        $taskBuilder = $this->scheduler->createTask($handlerClass, $workload);
82
83 5
        if ($cronExpression !== null) {
84 2
            $endDate = null;
85 2
            if ($endDateString !== null) {
86 1
                $endDate = new \DateTime($endDateString);
87 1
            }
88
89 2
            $taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
90 2
        }
91
92 5
        if ($executionDateString !== null) {
93 1
            $taskBuilder->executeAt(new \DateTime($executionDateString));
94 1
        }
95
96 5
        $taskBuilder->schedule();
97 5
    }
98
}
99