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\CentrifugoChecker; |
16
|
|
|
use Fresh\CentrifugoBundle\Service\CentrifugoInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* PresenceCommand. |
25
|
|
|
* |
26
|
|
|
* @author Artem Henvald <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class PresenceCommand extends AbstractCommand |
29
|
|
|
{ |
30
|
|
|
use ArgumentChannelTrait; |
31
|
|
|
|
32
|
|
|
protected static $defaultName = 'centrifugo:presence'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param CentrifugoInterface $centrifugo |
36
|
|
|
* @param CentrifugoChecker $centrifugoChecker |
37
|
|
|
*/ |
38
|
|
|
public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker) |
39
|
|
|
{ |
40
|
|
|
$this->centrifugoChecker = $centrifugoChecker; |
41
|
|
|
|
42
|
|
|
parent::__construct($centrifugo); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
protected function configure(): void |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this |
51
|
|
|
->setDescription('Get channel presence information') |
52
|
|
|
->setDefinition( |
53
|
|
|
new InputDefinition([ |
54
|
|
|
new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'), |
55
|
|
|
]) |
56
|
|
|
) |
57
|
|
|
->setHelp( |
58
|
|
|
<<<'HELP' |
59
|
|
|
The <info>%command.name%</info> command allows to get channel presence information: |
60
|
|
|
|
61
|
|
|
<info>%command.full_name%</info> <comment>channelAbc</comment> |
62
|
|
|
|
63
|
|
|
Read more at https://centrifugal.github.io/centrifugo/server/http_api/#presence |
64
|
|
|
HELP |
65
|
|
|
) |
66
|
|
|
; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output): void |
73
|
|
|
{ |
74
|
|
|
parent::initialize($input, $output); |
75
|
|
|
|
76
|
|
|
$this->initializeChannelArgument($input); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
83
|
|
|
{ |
84
|
|
|
$io = new SymfonyStyle($input, $output); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$data = $this->centrifugo->presence($this->channel); |
88
|
|
|
|
89
|
|
|
if (!empty($data['presence'])) { |
90
|
|
|
$io->title('Presence'); |
91
|
|
|
|
92
|
|
|
foreach ($data['presence'] as $id => $info) { |
93
|
|
|
$io->text(\sprintf('<info>%s</info>', $id)); |
94
|
|
|
$io->text(\sprintf(' ├ client: <comment>%s</comment>', $info['client'])); |
95
|
|
|
if (isset($info['conn_info'])) { |
96
|
|
|
$io->text(' ├ conn_info:'); |
97
|
|
|
$io->write($this->formatConnInfo($info['conn_info'])); |
98
|
|
|
} |
99
|
|
|
$io->text(\sprintf(' └ user: <comment>%s</comment>', $info['user'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$io->newLine(); |
103
|
|
|
} else { |
104
|
|
|
$io->success('NO DATA'); |
105
|
|
|
} |
106
|
|
|
} catch (\Throwable $e) { |
107
|
|
|
$io->error($e->getMessage()); |
108
|
|
|
|
109
|
|
|
return $e->getCode(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $connInfo |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function formatConnInfo(array $connInfo): string |
121
|
|
|
{ |
122
|
|
|
$json = \json_encode($connInfo, \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR); |
123
|
|
|
|
124
|
|
|
return array_reduce( |
125
|
|
|
\explode("\n", $json), |
126
|
|
|
static function (string $jsonWithPadding, string $line) { |
127
|
|
|
return \sprintf("%s │ <comment>%s</comment>\n", $jsonWithPadding, $line); |
128
|
|
|
}, |
129
|
|
|
'' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.