InfoCommand   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 85
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A execute() 0 29 5
A writeParameter() 0 23 5
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 (\Throwable $e) {
70
            $io->error($e->getMessage());
71
72
            return $e->getCode();
73
        }
74
75
        return 0;
76
    }
77
78
    /**
79
     * @param SymfonyStyle $io
80
     * @param string       $key
81
     * @param array|mixed  $value
82
     * @param int          $padding
83
     * @param bool         $last
84
     */
85
    private function writeParameter(SymfonyStyle $io, string $key, $value, int $padding = 0, bool $last = false): void
86
    {
87
        $formattedKey = $key;
88
        if ($padding > 0) {
89
            $formattedKey = $last ? '└ ' : '├ ';
90
            $formattedKey .= $key;
91
            $formattedKey = \str_pad($formattedKey, \strlen($formattedKey) + $padding, ' ', \STR_PAD_LEFT);
92
        }
93
94
        if (!\is_array($value)) {
95
            $text = \sprintf('<info>%s</info>: %s', $formattedKey, (string) $value);
96
            $io->text($text);
97
        } else {
98
            $io->text(\sprintf('<info>%s</info>', $formattedKey));
99
100
            $total = \count($value);
101
            $i = 0;
102
            foreach ($value as $innerKey => $innerValue) {
103
                ++$i;
104
                $this->writeParameter($io, $innerKey, $innerValue, $padding + 2, $total === $i);
105
            }
106
        }
107
    }
108
}
109