Completed
Push — master ( 144290...6b4abf )
by Artem
01:09
created

Command/PresenceCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Fresh\CentrifugoBundle\Service\Centrifugo;
16
use Fresh\CentrifugoBundle\Service\CentrifugoChecker;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Exception\InvalidArgumentException;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputDefinition;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Console\Style\SymfonyStyle;
24
25
/**
26
 * PresenceCommand.
27
 *
28
 * @author Artem Henvald <[email protected]>
29
 */
30
final class PresenceCommand extends Command
31
{
32
    protected static $defaultName = 'centrifugo:presence';
33
34
    /** @var Centrifugo */
35
    private $centrifugo;
36
37
    /** @var CentrifugoChecker */
38
    private $centrifugoChecker;
39
40
    /** @var string */
41
    private $channel;
42
43
    /**
44
     * @param Centrifugo        $centrifugo
45
     * @param CentrifugoChecker $centrifugoChecker
46
     */
47
    public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker)
48
    {
49
        $this->centrifugo = $centrifugo;
50
        $this->centrifugoChecker = $centrifugoChecker;
51
52
        parent::__construct();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 View Code Duplication
    protected function configure(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $this
61
            ->setDescription('Get channel presence information')
62
            ->setDefinition(
63
                new InputDefinition([
64
                    new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'),
65
                ])
66
            )
67
            ->setHelp(
68
                <<<EOT
69
The <info>%command.name%</info> command allows to get channel presence information:
70
71
<info>%command.full_name%</info> <comment>channelAbc</comment>
72
73
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#presence
74
EOT
75
            )
76
        ;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected function initialize(InputInterface $input, OutputInterface $output): void
83
    {
84
        parent::initialize($input, $output);
85
86
        try {
87
            $channel = (string) $input->getArgument('channel');
88
            $this->centrifugoChecker->assertValidChannelName($channel);
89
            $this->channel = $channel;
90
        } catch (\Exception $e) {
91
            throw new InvalidArgumentException($e->getMessage());
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    protected function execute(InputInterface $input, OutputInterface $output): int
99
    {
100
        $io = new SymfonyStyle($input, $output);
101
102
        try {
103
            $data = $this->centrifugo->presence($this->channel);
104
105
            if (!empty($data['presence'])) {
106
                $io->title('Presence');
107
108
                foreach ($data['presence'] as $id => $info) {
109
                    $io->text(\sprintf('<info>%s</info>', $id));
110
                    $io->text(\sprintf('  ├ client: <comment>%s</comment>', $info['client']));
111
                    $io->text(\sprintf('  └ user: <comment>%s</comment>', $info['user']));
112
                }
113
114
                $io->newLine();
115
            } else {
116
                $io->success('NO DATA');
117
            }
118
        } catch (\Exception $e) {
119
            $io->error($e->getMessage());
120
        }
121
122
        return 0;
123
    }
124
}
125