Test.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Command\Broadcast;
  8. use OCP\EventDispatcher\ABroadcastedEvent;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. class Test extends Command {
  15. public function __construct(
  16. private IEventDispatcher $eventDispatcher,
  17. ) {
  18. parent::__construct();
  19. }
  20. protected function configure(): void {
  21. $this
  22. ->setName('broadcast:test')
  23. ->setDescription('test the SSE broadcaster')
  24. ->addArgument(
  25. 'uid',
  26. InputArgument::REQUIRED,
  27. 'the UID of the users to receive the event'
  28. )
  29. ->addArgument(
  30. 'name',
  31. InputArgument::OPTIONAL,
  32. 'the event name',
  33. 'test'
  34. );
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int {
  37. $name = $input->getArgument('name');
  38. $uid = $input->getArgument('uid');
  39. $event = new class($name, $uid) extends ABroadcastedEvent {
  40. /** @var string */
  41. private $name;
  42. /** @var string */
  43. private $uid;
  44. public function __construct(string $name,
  45. string $uid) {
  46. parent::__construct();
  47. $this->name = $name;
  48. $this->uid = $uid;
  49. }
  50. public function broadcastAs(): string {
  51. return $this->name;
  52. }
  53. public function getUids(): array {
  54. return [
  55. $this->uid,
  56. ];
  57. }
  58. public function jsonSerialize(): array {
  59. return [
  60. 'description' => 'this is a test event',
  61. ];
  62. }
  63. };
  64. $this->eventDispatcher->dispatch('broadcasttest', $event);
  65. return 0;
  66. }
  67. }