Completed
Pull Request — master (#47)
by Wachter
07:18
created

ScheduleTaskCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 10
cp 0
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
    public function __construct($name, TaskSchedulerInterface $runner)
36
    {
37
        parent::__construct($name);
38
39
        $this->scheduler = $runner;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function configure()
46
    {
47
        $this
48
            ->setDescription('Schedule task')
49
            ->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
            )
59
            ->addArgument('handlerClass', InputArgument::REQUIRED, 'Handler which will be called')
60
            ->addArgument('workload', InputArgument::OPTIONAL, 'This will be passed to the handler')
61
            ->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED, 'Specifies interval for recurring task')
62
            ->addOption('execution-date', null, InputOption::VALUE_REQUIRED, 'Specifies execution-date for single task')
63
            ->addOption('end-date', null, InputOption::VALUE_REQUIRED, 'Specifies last run date for recurring tasks');
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function execute(InputInterface $input, OutputInterface $output)
70
    {
71
        $handlerClass = $input->getArgument('handlerClass');
72
        $workload = $input->getArgument('workload');
73
        $cronExpression = $input->getOption('cron-expression');
74
        $executionDateString = $input->getOption('execution-date');
75
        $endDateString = $input->getOption('end-date');
76
77
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
78
            $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload));
79
        }
80
81
        $taskBuilder = $this->scheduler->createTask($handlerClass, $workload);
0 ignored issues
show
Bug introduced by
It seems like $workload defined by $input->getArgument('workload') on line 72 can also be of type array<integer,string>; however, Task\Scheduler\TaskSched...Interface::createTask() does only seem to accept string|object<Serializable>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
83
        if (null !== $cronExpression) {
84
            $endDate = null;
85
            if (null !== $endDateString) {
86
                $endDate = new \DateTime($endDateString);
87
            }
88
89
            $taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
90
        }
91
92
        if (null !== $executionDateString) {
93
            $taskBuilder->executeAt(new \DateTime($executionDateString));
94
        }
95
96
        $taskBuilder->schedule();
97
    }
98
}
99