ContainerList::render()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
namespace Dock\Cli\Helper;
4
5
use Dock\Docker\Containers\Container;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Helper\TableSeparator;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class ContainerList
11
{
12
    /**
13
     * @var OutputInterface
14
     */
15
    private $output;
16
17
    /**
18
     * @param OutputInterface $output
19
     */
20
    public function __construct(OutputInterface $output)
21
    {
22
        $this->output = $output;
23
    }
24
25
    /**
26
     * @param Container[] $containers
27
     */
28
    public function render(array $containers)
29
    {
30
        $table = new Table($this->output);
31
        $table->setHeaders(['Component', 'Container', 'DNS addresses', 'Port(s)', 'Status']);
32
33
        foreach ($containers as $index => $container) {
34
            if ($index > 0) {
35
                $table->addRow(new TableSeparator());
36
            }
37
38
            $table->addRow([
39
                $container->getComponentName(),
40
                $container->getName(),
41
                implode("\n", $container->getHosts()),
42
                implode("\n", $container->getPorts()),
43
                $this->getDecoratedState($container),
44
            ]);
45
        }
46
47
        $table->render();
48
    }
49
50
    /**
51
     * @param Container $container
52
     *
53
     * @return string
54
     */
55
    private function getDecoratedState(Container $container)
56
    {
57
        if ($container->getState() === Container::STATE_EXITED) {
58
            return '<error>'.$container->getState().'</error>';
59
        }
60
61
        return $container->getState();
62
    }
63
}
64