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