dummyjoblist.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\BackgroundJob;
  9. /**
  10. * Class DummyJobList
  11. *
  12. * in memory job list for testing purposes
  13. */
  14. class DummyJobList extends \OC\BackgroundJob\JobList {
  15. /**
  16. * @var \OC\BackgroundJob\Job[]
  17. */
  18. private $jobs = array();
  19. private $last = 0;
  20. public function __construct() {
  21. }
  22. /**
  23. * @param \OC\BackgroundJob\Job|string $job
  24. * @param mixed $argument
  25. */
  26. public function add($job, $argument = null) {
  27. if (is_string($job)) {
  28. /** @var \OC\BackgroundJob\Job $job */
  29. $job = new $job;
  30. }
  31. $job->setArgument($argument);
  32. if (!$this->has($job, null)) {
  33. $this->jobs[] = $job;
  34. }
  35. }
  36. /**
  37. * @param \OC\BackgroundJob\Job|string $job
  38. * @param mixed $argument
  39. */
  40. public function remove($job, $argument = null) {
  41. $index = array_search($job, $this->jobs);
  42. if ($index !== false) {
  43. unset($this->jobs[$index]);
  44. }
  45. }
  46. /**
  47. * check if a job is in the list
  48. *
  49. * @param $job
  50. * @param mixed $argument
  51. * @return bool
  52. */
  53. public function has($job, $argument) {
  54. return array_search($job, $this->jobs) !== false;
  55. }
  56. /**
  57. * get all jobs in the list
  58. *
  59. * @return \OC\BackgroundJob\Job[]
  60. */
  61. public function getAll() {
  62. return $this->jobs;
  63. }
  64. /**
  65. * get the next job in the list
  66. *
  67. * @return \OC\BackgroundJob\Job
  68. */
  69. public function getNext() {
  70. if (count($this->jobs) > 0) {
  71. if ($this->last < (count($this->jobs) - 1)) {
  72. $i = $this->last + 1;
  73. } else {
  74. $i = 0;
  75. }
  76. return $this->jobs[$i];
  77. } else {
  78. return null;
  79. }
  80. }
  81. /**
  82. * set the job that was last ran
  83. *
  84. * @param \OC\BackgroundJob\Job $job
  85. */
  86. public function setLastJob($job) {
  87. $i = array_search($job, $this->jobs);
  88. if ($i !== false) {
  89. $this->last = $i;
  90. } else {
  91. $this->last = 0;
  92. }
  93. }
  94. /**
  95. * @param int $id
  96. * @return Job
  97. */
  98. public function getById($id) {
  99. foreach ($this->jobs as $job) {
  100. if ($job->getId() === $id) {
  101. return $job;
  102. }
  103. }
  104. return null;
  105. }
  106. /**
  107. * get the id of the last ran job
  108. *
  109. * @return int
  110. */
  111. public function getLastJob() {
  112. return $this->last;
  113. }
  114. /**
  115. * set the lastRun of $job to now
  116. *
  117. * @param \OC\BackgroundJob\Job $job
  118. */
  119. public function setLastRun($job) {
  120. $job->setLastRun(time());
  121. }
  122. }