Completed
Pull Request — master (#47)
by Wachter
11:23
created

DebugTasksCommand::execute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 9.52
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
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\Helper\Table;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Task\Storage\TaskExecutionRepositoryInterface;
20
21
/**
22
 * Run pending tasks.
23
 */
24
class DebugTasksCommand extends Command
25
{
26
    /**
27
     * @var TaskExecutionRepositoryInterface
28
     */
29
    private $taskExecutionRepository;
30
31
    /**
32
     * @param string $name
33
     * @param TaskExecutionRepositoryInterface $taskExecutionRepository
34
     */
35
    public function __construct($name, TaskExecutionRepositoryInterface $taskExecutionRepository)
36
    {
37
        parent::__construct($name);
38
39
        $this->taskExecutionRepository = $taskExecutionRepository;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function configure()
46
    {
47
        $this->setDescription('Debug tasks')
48
            ->setHelp(<<<'EOT'
49
The <info>%command.name%</info> command dumps the information about tasks.
50
51
    $ %command.full_name% -p 1 --page-size 10
52
53
Pagination is possible with the optional options 'p' and 'page-size'.
54
EOT
55
            )
56
            ->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'Specifies page for pagination', 1)
57
            ->addOption('page-size', null, InputOption::VALUE_REQUIRED, 'Specifies page-size for pagination', null);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected function execute(InputInterface $input, OutputInterface $output)
64
    {
65
        $page = $input->getOption('page');
66
        $pageSize = $input->getOption('page-size');
67
68
        $executions = $this->taskExecutionRepository->findAll($page, $pageSize);
69
70
        $table = new Table($output);
71
        $table->setHeaders(['uuid', 'status', 'handler', 'schedule time', 'end time', 'duration']);
72
73
        foreach ($executions as $execution) {
74
            $table->addRow(
75
                [
76
                    $execution->getUuid(),
77
                    $execution->getStatus(),
78
                    $execution->getHandlerClass(),
79
                    $execution->getScheduleTime()->format(\DateTime::RFC3339),
80
                    !$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTime::RFC3339),
81
                    (round($execution->getDuration(), 6) * 1000000) . 'ms',
82
                ]
83
            );
84
        }
85
86
        $table->render();
87
    }
88
}
89