Completed
Push — master ( e48163...a19c3c )
by Artem
01:18
created

InfoCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the FreshCentrifugoBundle.
4
 *
5
 * (c) Artem Henvald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Fresh\CentrifugoBundle\Command;
14
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
19
/**
20
 * InfoCommand.
21
 *
22
 * @author Artem Henvald <[email protected]>
23
 */
24
final class InfoCommand extends AbstractCommand
25
{
26
    protected static $defaultName = 'centrifugo:info';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function configure(): void
32
    {
33
        $this
34
            ->setDescription('Get information about running Centrifugo nodes')
35
            ->setHelp(
36
                <<<'HELP'
37
The <info>%command.name%</info> command allows to get information about running Centrifugo nodes:
38
39
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#info
40
HELP
41
            )
42
        ;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output): int
49
    {
50
        $io = new SymfonyStyle($input, $output);
51
52
        try {
53
            $data = $this->centrifugo->info();
54
55
            if (!empty($data['nodes'])) {
56
                $io->title('Info');
57
58
                foreach ($data['nodes'] as $nodeInfo) {
59
                    $io->section(\sprintf('<comment>Node</comment> <info>%s</info>', $nodeInfo['name']));
60
                    foreach ($nodeInfo as $key => $value) {
61
                        $this->writeParameter($io, $key, $value);
62
                    }
63
                }
64
65
                $io->newLine();
66
            } else {
67
                $io->success('NO DATA');
68
            }
69
        } catch (\Exception $e) {
70
            $io->error($e->getMessage());
71
        }
72
73
        return 0;
74
    }
75
76
    /**
77
     * @param SymfonyStyle $io
78
     * @param string       $key
79
     * @param array|mixed  $value
80
     * @param int          $padding
81
     * @param bool         $last
82
     */
83
    private function writeParameter(SymfonyStyle $io, string $key, $value, int $padding = 0, bool $last = false): void
84
    {
85
        $formattedKey = $key;
86
        if ($padding > 0) {
87
            $formattedKey = $last ? '└ ' : '├ ';
88
            $formattedKey .= $key;
89
            $formattedKey = \str_pad($formattedKey, \strlen($formattedKey) + $padding, ' ', \STR_PAD_LEFT);
90
        }
91
92
        if (!\is_array($value)) {
93
            $text = \sprintf('<info>%s</info>: %s', $formattedKey, (string) $value);
94
            $io->text($text);
95
        } else {
96
            $io->text(\sprintf('<info>%s</info>', $formattedKey));
97
98
            $total = \count($value);
99
            $i = 0;
100
            foreach ($value as $innerKey => $innerValue) {
101
                ++$i;
102
                $this->writeParameter($io, $innerKey, $innerValue, $padding + 2, $total === $i);
103
            }
104
        }
105
    }
106
}
107