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 | * BroadcastCommand. |
||
27 | * |
||
28 | * @author Artem Henvald <[email protected]> |
||
29 | */ |
||
30 | final class BroadcastCommand extends Command |
||
31 | { |
||
32 | protected static $defaultName = 'centrifugo:broadcast'; |
||
33 | |||
34 | /** @var Centrifugo */ |
||
35 | private $centrifugo; |
||
36 | |||
37 | /** @var CentrifugoChecker */ |
||
38 | private $centrifugoChecker; |
||
39 | |||
40 | /** @var mixed[] */ |
||
41 | private $data = []; |
||
42 | |||
43 | /** @var string[] */ |
||
44 | private $channels; |
||
45 | |||
46 | /** |
||
47 | * @param Centrifugo $centrifugo |
||
48 | * @param CentrifugoChecker $centrifugoChecker |
||
49 | */ |
||
50 | public function __construct(Centrifugo $centrifugo, CentrifugoChecker $centrifugoChecker) |
||
51 | { |
||
52 | $this->centrifugo = $centrifugo; |
||
53 | $this->centrifugoChecker = $centrifugoChecker; |
||
54 | |||
55 | parent::__construct(); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | View Code Duplication | protected function configure(): void |
|
0 ignored issues
–
show
|
|||
62 | { |
||
63 | $this |
||
64 | ->setDescription('Publish same data into many channels') |
||
65 | ->setDefinition( |
||
66 | new InputDefinition([ |
||
67 | new InputArgument('data', InputArgument::REQUIRED, 'Data in JSON format'), |
||
68 | new InputArgument('channels', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Channel names'), |
||
69 | ]) |
||
70 | ) |
||
71 | ->setHelp( |
||
72 | <<<'EOT' |
||
73 | The <info>%command.name%</info> command allows to publish same data into many channels: |
||
74 | |||
75 | <info>%command.full_name%</info> <comment>'{"foo":"bar"}'</comment> </comment>channelAbc</comment> </comment>channelDef</comment> |
||
76 | |||
77 | Read more at https://centrifugal.github.io/centrifugo/server/http_api/#broadcast |
||
78 | EOT |
||
79 | ) |
||
80 | ; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | protected function initialize(InputInterface $input, OutputInterface $output): void |
||
87 | { |
||
88 | parent::initialize($input, $output); |
||
89 | |||
90 | try { |
||
91 | $json = (string) $input->getArgument('data'); |
||
92 | $data = \json_decode($json, true, 512, \JSON_THROW_ON_ERROR); |
||
93 | $this->data = $data; |
||
94 | } catch (\Exception $e) { |
||
95 | throw new InvalidArgumentException('Data is not a valid JSON.'); |
||
96 | } |
||
97 | |||
98 | try { |
||
99 | $channels = (array) $input->getArgument('channels'); |
||
100 | foreach ($channels as $channel) { |
||
101 | $this->centrifugoChecker->assertValidChannelName($channel); |
||
102 | } |
||
103 | $this->channels = $channels; |
||
104 | } catch (\Exception $e) { |
||
105 | throw new InvalidArgumentException($e->getMessage()); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | View Code Duplication | protected function execute(InputInterface $input, OutputInterface $output): int |
|
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...
|
|||
113 | { |
||
114 | $io = new SymfonyStyle($input, $output); |
||
115 | |||
116 | try { |
||
117 | $this->centrifugo->broadcast($this->data, $this->channels); |
||
118 | $io->success('DONE'); |
||
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.