| @@ 28-100 (lines=73) @@ | ||
| 25 | * |
|
| 26 | * @author Artem Henvald <[email protected]> |
|
| 27 | */ |
|
| 28 | final class PublishCommand extends AbstractCommand |
|
| 29 | { |
|
| 30 | use ArgumentChannelTrait; |
|
| 31 | use ArgumentDataTrait; |
|
| 32 | ||
| 33 | protected static $defaultName = 'centrifugo:publish'; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @param CentrifugoInterface $centrifugo |
|
| 37 | * @param CentrifugoChecker $centrifugoChecker |
|
| 38 | */ |
|
| 39 | public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker) |
|
| 40 | { |
|
| 41 | $this->centrifugoChecker = $centrifugoChecker; |
|
| 42 | ||
| 43 | parent::__construct($centrifugo); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * {@inheritdoc} |
|
| 48 | */ |
|
| 49 | protected function configure(): void |
|
| 50 | { |
|
| 51 | $this |
|
| 52 | ->setDescription('Publish data into channel') |
|
| 53 | ->setDefinition( |
|
| 54 | new InputDefinition([ |
|
| 55 | new InputArgument('data', InputArgument::REQUIRED, 'Data in JSON format'), |
|
| 56 | new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'), |
|
| 57 | ]) |
|
| 58 | ) |
|
| 59 | ->setHelp( |
|
| 60 | <<<'HELP' |
|
| 61 | The <info>%command.name%</info> command allows to publish data into channel: |
|
| 62 | ||
| 63 | <info>%command.full_name%</info> <comment>'{"foo":"bar"}'</comment> <comment>channelAbc</comment> |
|
| 64 | ||
| 65 | Read more at https://centrifugal.github.io/centrifugo/server/http_api/#publish |
|
| 66 | HELP |
|
| 67 | ) |
|
| 68 | ; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * {@inheritdoc} |
|
| 73 | */ |
|
| 74 | protected function initialize(InputInterface $input, OutputInterface $output): void |
|
| 75 | { |
|
| 76 | parent::initialize($input, $output); |
|
| 77 | ||
| 78 | $this->initializeDataArgument($input); |
|
| 79 | $this->initializeChannelArgument($input); |
|
| 80 | } |
|
| 81 | ||
| 82 | /** |
|
| 83 | * {@inheritdoc} |
|
| 84 | */ |
|
| 85 | protected function execute(InputInterface $input, OutputInterface $output): int |
|
| 86 | { |
|
| 87 | $io = new SymfonyStyle($input, $output); |
|
| 88 | ||
| 89 | try { |
|
| 90 | $this->centrifugo->publish($this->data, $this->channel); |
|
| 91 | $io->success('DONE'); |
|
| 92 | } catch (\Throwable $e) { |
|
| 93 | $io->error($e->getMessage()); |
|
| 94 | ||
| 95 | return $e->getCode(); |
|
| 96 | } |
|
| 97 | ||
| 98 | return 0; |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||
| @@ 28-100 (lines=73) @@ | ||
| 25 | * |
|
| 26 | * @author Artem Henvald <[email protected]> |
|
| 27 | */ |
|
| 28 | final class UnsubscribeCommand extends AbstractCommand |
|
| 29 | { |
|
| 30 | use ArgumentChannelTrait; |
|
| 31 | use ArgumentUserTrait; |
|
| 32 | ||
| 33 | protected static $defaultName = 'centrifugo:unsubscribe'; |
|
| 34 | ||
| 35 | /** |
|
| 36 | * @param CentrifugoInterface $centrifugo |
|
| 37 | * @param CentrifugoChecker $centrifugoChecker |
|
| 38 | */ |
|
| 39 | public function __construct(CentrifugoInterface $centrifugo, CentrifugoChecker $centrifugoChecker) |
|
| 40 | { |
|
| 41 | $this->centrifugoChecker = $centrifugoChecker; |
|
| 42 | ||
| 43 | parent::__construct($centrifugo); |
|
| 44 | } |
|
| 45 | ||
| 46 | /** |
|
| 47 | * {@inheritdoc} |
|
| 48 | */ |
|
| 49 | protected function configure(): void |
|
| 50 | { |
|
| 51 | $this |
|
| 52 | ->setDescription('Unsubscribe user from channel') |
|
| 53 | ->setDefinition( |
|
| 54 | new InputDefinition([ |
|
| 55 | new InputArgument('user', InputArgument::REQUIRED, 'User ID'), |
|
| 56 | new InputArgument('channel', InputArgument::REQUIRED, 'Channel name'), |
|
| 57 | ]) |
|
| 58 | ) |
|
| 59 | ->setHelp( |
|
| 60 | <<<'HELP' |
|
| 61 | The <info>%command.name%</info> command allows to unsubscribe user from channel: |
|
| 62 | ||
| 63 | <info>%command.full_name%</info> <comment>user123</comment> <comment>channelAbc</comment> |
|
| 64 | ||
| 65 | Read more at https://centrifugal.github.io/centrifugo/server/http_api/#unsubscribe |
|
| 66 | HELP |
|
| 67 | ) |
|
| 68 | ; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * {@inheritdoc} |
|
| 73 | */ |
|
| 74 | protected function initialize(InputInterface $input, OutputInterface $output): void |
|
| 75 | { |
|
| 76 | parent::initialize($input, $output); |
|
| 77 | ||
| 78 | $this->initializeUserArgument($input); |
|
| 79 | $this->initializeChannelArgument($input); |
|
| 80 | } |
|
| 81 | ||
| 82 | /** |
|
| 83 | * {@inheritdoc} |
|
| 84 | */ |
|
| 85 | protected function execute(InputInterface $input, OutputInterface $output): int |
|
| 86 | { |
|
| 87 | $io = new SymfonyStyle($input, $output); |
|
| 88 | ||
| 89 | try { |
|
| 90 | $this->centrifugo->unsubscribe($this->user, $this->channel); |
|
| 91 | $io->success('DONE'); |
|
| 92 | } catch (\Throwable $e) { |
|
| 93 | $io->error($e->getMessage()); |
|
| 94 | ||
| 95 | return $e->getCode(); |
|
| 96 | } |
|
| 97 | ||
| 98 | return 0; |
|
| 99 | } |
|
| 100 | } |
|
| 101 | ||