Test.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Broadcast;
  26. use OCP\EventDispatcher\ABroadcastedEvent;
  27. use OCP\EventDispatcher\IEventDispatcher;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Output\OutputInterface;
  32. class Test extends Command {
  33. public function __construct(
  34. private IEventDispatcher $eventDispatcher,
  35. ) {
  36. parent::__construct();
  37. }
  38. protected function configure(): void {
  39. $this
  40. ->setName('broadcast:test')
  41. ->setDescription('test the SSE broadcaster')
  42. ->addArgument(
  43. 'uid',
  44. InputArgument::REQUIRED,
  45. 'the UID of the users to receive the event'
  46. )
  47. ->addArgument(
  48. 'name',
  49. InputArgument::OPTIONAL,
  50. 'the event name',
  51. 'test'
  52. );
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int {
  55. $name = $input->getArgument('name');
  56. $uid = $input->getArgument('uid');
  57. $event = new class($name, $uid) extends ABroadcastedEvent {
  58. /** @var string */
  59. private $name;
  60. /** @var string */
  61. private $uid;
  62. public function __construct(string $name,
  63. string $uid) {
  64. parent::__construct();
  65. $this->name = $name;
  66. $this->uid = $uid;
  67. }
  68. public function broadcastAs(): string {
  69. return $this->name;
  70. }
  71. public function getUids(): array {
  72. return [
  73. $this->uid,
  74. ];
  75. }
  76. public function jsonSerialize(): array {
  77. return [
  78. 'description' => 'this is a test event',
  79. ];
  80. }
  81. };
  82. $this->eventDispatcher->dispatch('broadcasttest', $event);
  83. return 0;
  84. }
  85. }