BatchRepositoryFake.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Carbon\CarbonImmutable;
  4. use Closure;
  5. use Illuminate\Bus\BatchRepository;
  6. use Illuminate\Bus\PendingBatch;
  7. use Illuminate\Bus\UpdatedBatchJobCounts;
  8. use Illuminate\Support\Str;
  9. class BatchRepositoryFake implements BatchRepository
  10. {
  11. /**
  12. * The batches stored in the repository.
  13. *
  14. * @var \Illuminate\Bus\Batch[]
  15. */
  16. protected $batches = [];
  17. /**
  18. * Retrieve a list of batches.
  19. *
  20. * @param int $limit
  21. * @param mixed $before
  22. * @return \Illuminate\Bus\Batch[]
  23. */
  24. public function get($limit, $before)
  25. {
  26. return $this->batches;
  27. }
  28. /**
  29. * Retrieve information about an existing batch.
  30. *
  31. * @param string $batchId
  32. * @return \Illuminate\Bus\Batch|null
  33. */
  34. public function find(string $batchId)
  35. {
  36. return $this->batches[$batchId] ?? null;
  37. }
  38. /**
  39. * Store a new pending batch.
  40. *
  41. * @param \Illuminate\Bus\PendingBatch $batch
  42. * @return \Illuminate\Bus\Batch
  43. */
  44. public function store(PendingBatch $batch)
  45. {
  46. $id = (string) Str::orderedUuid();
  47. $this->batches[$id] = new BatchFake(
  48. $id,
  49. $batch->name,
  50. count($batch->jobs),
  51. count($batch->jobs),
  52. 0,
  53. [],
  54. $batch->options,
  55. CarbonImmutable::now(),
  56. null,
  57. null
  58. );
  59. return $this->batches[$id];
  60. }
  61. /**
  62. * Increment the total number of jobs within the batch.
  63. *
  64. * @param string $batchId
  65. * @param int $amount
  66. * @return void
  67. */
  68. public function incrementTotalJobs(string $batchId, int $amount)
  69. {
  70. //
  71. }
  72. /**
  73. * Decrement the total number of pending jobs for the batch.
  74. *
  75. * @param string $batchId
  76. * @param string $jobId
  77. * @return \Illuminate\Bus\UpdatedBatchJobCounts
  78. */
  79. public function decrementPendingJobs(string $batchId, string $jobId)
  80. {
  81. return new UpdatedBatchJobCounts;
  82. }
  83. /**
  84. * Increment the total number of failed jobs for the batch.
  85. *
  86. * @param string $batchId
  87. * @param string $jobId
  88. * @return \Illuminate\Bus\UpdatedBatchJobCounts
  89. */
  90. public function incrementFailedJobs(string $batchId, string $jobId)
  91. {
  92. return new UpdatedBatchJobCounts;
  93. }
  94. /**
  95. * Mark the batch that has the given ID as finished.
  96. *
  97. * @param string $batchId
  98. * @return void
  99. */
  100. public function markAsFinished(string $batchId)
  101. {
  102. if (isset($this->batches[$batchId])) {
  103. $this->batches[$batchId]->finishedAt = now();
  104. }
  105. }
  106. /**
  107. * Cancel the batch that has the given ID.
  108. *
  109. * @param string $batchId
  110. * @return void
  111. */
  112. public function cancel(string $batchId)
  113. {
  114. if (isset($this->batches[$batchId])) {
  115. $this->batches[$batchId]->cancel();
  116. }
  117. }
  118. /**
  119. * Delete the batch that has the given ID.
  120. *
  121. * @param string $batchId
  122. * @return void
  123. */
  124. public function delete(string $batchId)
  125. {
  126. unset($this->batches[$batchId]);
  127. }
  128. /**
  129. * Execute the given Closure within a storage specific transaction.
  130. *
  131. * @param \Closure $callback
  132. * @return mixed
  133. */
  134. public function transaction(Closure $callback)
  135. {
  136. return $callback();
  137. }
  138. }