Completed
Push — master ( b3f90f...c5536c )
by Daniel
03:02
created

ShowCommand::renderTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
ccs 16
cts 16
cp 1
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander\Console\Command;
9
10
use GravityMedia\Commander\Commander\Task;
11
use Monolog\Formatter\NormalizerFormatter;
12
use Symfony\Component\Console\Helper\Table;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Show command class.
18
 *
19
 * @package GravityMedia\Commander\Console\Command
20
 */
21
class ShowCommand extends Command
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        parent::configure();
29
30
        $this
31
            ->setName('show')
32
            ->setDescription('Show information about all joined tasks');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 4
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40 4
        $commander = $this->getCommander()->initialize();
41 4
        $tasks = $commander->getTaskManager()->findAllTasks();
42
43 4
        if (0 === count($tasks)) {
44 2
            $output->writeln('No tasks found');
45 2
            return;
46
        }
47
48 2
        $this->renderTable($tasks, $output);
49 2
    }
50
51
    /**
52
     * Render table.
53
     *
54
     * @param Task[]          $tasks
55
     * @param OutputInterface $output
56
     *
57
     * @return void
58
     */
59 2
    protected function renderTable(array $tasks, OutputInterface $output)
60
    {
61 2
        $table = new Table($output);
62 2
        $table->setHeaders(['ID', 'Priority', 'Commandline', 'PID', 'Exit Code', 'Created At', 'Updated At']);
63
64 2
        foreach ($tasks as $task) {
65 2
            $table->addRow([
66 2
                $task->getEntity()->getId(),
67 2
                $task->getEntity()->getPriority(),
68 2
                $task->getEntity()->getCommandline(),
69 2
                $task->getEntity()->getPid(),
70 2
                $task->getEntity()->getExitCode(),
71 2
                $task->getEntity()->getCreatedAt()->format(NormalizerFormatter::SIMPLE_DATE),
72 2
                $task->getEntity()->getUpdatedAt()->format(NormalizerFormatter::SIMPLE_DATE)
73 1
            ]);
74 1
        }
75
76 2
        $table->render();
77 2
    }
78
}
79