CronBus.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Command;
  7. use Laravel\SerializableClosure\SerializableClosure;
  8. use OCP\BackgroundJob\IJob;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\Command\ICommand;
  11. class CronBus extends AsyncBus {
  12. public function __construct(
  13. private IJobList $jobList,
  14. ) {
  15. }
  16. protected function queueCommand($command): void {
  17. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  18. }
  19. /**
  20. * @param ICommand|callable $command
  21. * @return class-string<IJob>
  22. */
  23. private function getJobClass($command): string {
  24. if ($command instanceof \Closure) {
  25. return ClosureJob::class;
  26. } elseif (is_callable($command)) {
  27. return CallableJob::class;
  28. } elseif ($command instanceof ICommand) {
  29. return CommandJob::class;
  30. } else {
  31. throw new \InvalidArgumentException('Invalid command');
  32. }
  33. }
  34. /**
  35. * @param ICommand|callable $command
  36. * @return string
  37. */
  38. private function serializeCommand($command): string {
  39. if ($command instanceof \Closure) {
  40. return serialize(new SerializableClosure($command));
  41. } elseif (is_callable($command) or $command instanceof ICommand) {
  42. return serialize($command);
  43. } else {
  44. throw new \InvalidArgumentException('Invalid command');
  45. }
  46. }
  47. }