BatchFake.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Carbon\CarbonImmutable;
  4. use Illuminate\Bus\Batch;
  5. use Illuminate\Bus\UpdatedBatchJobCounts;
  6. use Illuminate\Support\Carbon;
  7. use Illuminate\Support\Collection;
  8. class BatchFake extends Batch
  9. {
  10. /**
  11. * The jobs that have been added to the batch.
  12. *
  13. * @var array
  14. */
  15. public $added = [];
  16. /**
  17. * Indicates if the batch has been deleted.
  18. *
  19. * @var bool
  20. */
  21. public $deleted = false;
  22. /**
  23. * Create a new batch instance.
  24. *
  25. * @param string $id
  26. * @param string $name
  27. * @param int $totalJobs
  28. * @param int $pendingJobs
  29. * @param int $failedJobs
  30. * @param array $failedJobIds
  31. * @param array $options
  32. * @param \Carbon\CarbonImmutable $createdAt
  33. * @param \Carbon\CarbonImmutable|null $cancelledAt
  34. * @param \Carbon\CarbonImmutable|null $finishedAt
  35. */
  36. public function __construct(
  37. string $id,
  38. string $name,
  39. int $totalJobs,
  40. int $pendingJobs,
  41. int $failedJobs,
  42. array $failedJobIds,
  43. array $options,
  44. CarbonImmutable $createdAt,
  45. ?CarbonImmutable $cancelledAt = null,
  46. ?CarbonImmutable $finishedAt = null,
  47. ) {
  48. $this->id = $id;
  49. $this->name = $name;
  50. $this->totalJobs = $totalJobs;
  51. $this->pendingJobs = $pendingJobs;
  52. $this->failedJobs = $failedJobs;
  53. $this->failedJobIds = $failedJobIds;
  54. $this->options = $options;
  55. $this->createdAt = $createdAt;
  56. $this->cancelledAt = $cancelledAt;
  57. $this->finishedAt = $finishedAt;
  58. }
  59. /**
  60. * Get a fresh instance of the batch represented by this ID.
  61. *
  62. * @return self
  63. */
  64. public function fresh()
  65. {
  66. return $this;
  67. }
  68. /**
  69. * Add additional jobs to the batch.
  70. *
  71. * @param \Illuminate\Support\Enumerable|object|array $jobs
  72. * @return self
  73. */
  74. public function add($jobs)
  75. {
  76. $jobs = Collection::wrap($jobs);
  77. foreach ($jobs as $job) {
  78. $this->added[] = $job;
  79. }
  80. $this->totalJobs += $jobs->count();
  81. return $this;
  82. }
  83. /**
  84. * Record that a job within the batch finished successfully, executing any callbacks if necessary.
  85. *
  86. * @param string $jobId
  87. * @return void
  88. */
  89. public function recordSuccessfulJob(string $jobId)
  90. {
  91. //
  92. }
  93. /**
  94. * Decrement the pending jobs for the batch.
  95. *
  96. * @param string $jobId
  97. * @return void
  98. */
  99. public function decrementPendingJobs(string $jobId)
  100. {
  101. //
  102. }
  103. /**
  104. * Record that a job within the batch failed to finish successfully, executing any callbacks if necessary.
  105. *
  106. * @param string $jobId
  107. * @param \Throwable $e
  108. * @return void
  109. */
  110. public function recordFailedJob(string $jobId, $e)
  111. {
  112. //
  113. }
  114. /**
  115. * Increment the failed jobs for the batch.
  116. *
  117. * @param string $jobId
  118. * @return \Illuminate\Bus\UpdatedBatchJobCounts
  119. */
  120. public function incrementFailedJobs(string $jobId)
  121. {
  122. return new UpdatedBatchJobCounts;
  123. }
  124. /**
  125. * Cancel the batch.
  126. *
  127. * @return void
  128. */
  129. public function cancel()
  130. {
  131. $this->cancelledAt = Carbon::now();
  132. }
  133. /**
  134. * Delete the batch from storage.
  135. *
  136. * @return void
  137. */
  138. public function delete()
  139. {
  140. $this->deleted = true;
  141. }
  142. /**
  143. * Determine if the batch has been deleted.
  144. *
  145. * @return bool
  146. */
  147. public function deleted()
  148. {
  149. return $this->deleted;
  150. }
  151. }