CronBus.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Command;
  25. use OCP\Command\ICommand;
  26. use SuperClosure\Serializer;
  27. class CronBus extends AsyncBus {
  28. /**
  29. * @var \OCP\BackgroundJob\IJobList
  30. */
  31. private $jobList;
  32. /**
  33. * @param \OCP\BackgroundJob\IJobList $jobList
  34. */
  35. public function __construct($jobList) {
  36. $this->jobList = $jobList;
  37. }
  38. protected function queueCommand($command) {
  39. $this->jobList->add($this->getJobClass($command), $this->serializeCommand($command));
  40. }
  41. /**
  42. * @param \OCP\Command\ICommand | callable $command
  43. * @return string
  44. */
  45. private function getJobClass($command) {
  46. if ($command instanceof \Closure) {
  47. return ClosureJob::class;
  48. } else if (is_callable($command)) {
  49. return CallableJob::class;
  50. } else if ($command instanceof ICommand) {
  51. return CommandJob::class;
  52. } else {
  53. throw new \InvalidArgumentException('Invalid command');
  54. }
  55. }
  56. /**
  57. * @param \OCP\Command\ICommand | callable $command
  58. * @return string
  59. */
  60. private function serializeCommand($command) {
  61. if ($command instanceof \Closure) {
  62. $serializer = new Serializer();
  63. return $serializer->serialize($command);
  64. } else if (is_callable($command) or $command instanceof ICommand) {
  65. return serialize($command);
  66. } else {
  67. throw new \InvalidArgumentException('Invalid command');
  68. }
  69. }
  70. }