Completed
Push — master ( e2c326...ee1def )
by Wachter
06:24
created

ScheduleTaskCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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 Doctrine\ORM\EntityManagerInterface;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Task\Scheduler\TaskSchedulerInterface;
21
22
/**
23
 * Schedule task.
24
 */
25
class ScheduleTaskCommand extends Command
26
{
27
    /**
28
     * @var TaskSchedulerInterface
29
     */
30
    private $scheduler;
31
32
    /**
33
     * @var EntityManagerInterface
34
     */
35
    private $entityManager;
36
37
    /**
38
     * @param string $name
39
     * @param TaskSchedulerInterface $runner
40
     * @param EntityManagerInterface $entityManager
41
     */
42 12
    public function __construct($name, TaskSchedulerInterface $runner, EntityManagerInterface $entityManager = null)
43
    {
44 12
        parent::__construct($name);
45
46 12
        $this->scheduler = $runner;
47 12
        $this->entityManager = $entityManager;
48 12
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 12
    protected function configure()
54
    {
55 12
        $this
56 12
            ->setDescription('Run pending tasks')
57 12
            ->addArgument('handlerClass', InputArgument::REQUIRED)
58 12
            ->addArgument('workload', InputArgument::OPTIONAL)
59 12
            ->addOption('cron-expression', 'c', InputOption::VALUE_REQUIRED)
60 12
            ->addOption('execution-date', null, InputOption::VALUE_REQUIRED)
61 12
            ->addOption('end-date', null, InputOption::VALUE_REQUIRED);
62 12
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 5
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69 5
        $handlerClass = $input->getArgument('handlerClass');
70 5
        $workload = $input->getArgument('workload');
71 5
        $cronExpression = $input->getOption('cron-expression');
72 5
        $executionDateString = $input->getOption('execution-date');
73 5
        $endDateString = $input->getOption('end-date');
74
75 5
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
76
            $output->writeln(sprintf('Schedule task "%s" with workload "%s"', $handlerClass, $workload));
77
        }
78
79 5
        $taskBuilder = $this->scheduler->createTask($handlerClass, $workload);
80
81 5
        if ($cronExpression !== null) {
82 2
            $endDate = null;
83 2
            if ($endDateString !== null) {
84 1
                $endDate = new \DateTime($endDateString);
85 1
            }
86
87 2
            $taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
88 2
        }
89
90 5
        if ($executionDateString !== null) {
91 1
            $taskBuilder->executeAt(new \DateTime($executionDateString));
92 1
        }
93
94 5
        $taskBuilder->schedule();
95
96 5
        if ($this->entityManager) {
97 5
            $this->entityManager->flush();
98 5
        }
99 5
    }
100
}
101