BatchFake.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @return void
  36. */
  37. public function __construct(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. return $this;
  81. }
  82. /**
  83. * Record that a job within the batch finished successfully, executing any callbacks if necessary.
  84. *
  85. * @param string $jobId
  86. * @return void
  87. */
  88. public function recordSuccessfulJob(string $jobId)
  89. {
  90. //
  91. }
  92. /**
  93. * Decrement the pending jobs for the batch.
  94. *
  95. * @param string $jobId
  96. * @return \Illuminate\Bus\UpdatedBatchJobCounts
  97. */
  98. public function decrementPendingJobs(string $jobId)
  99. {
  100. //
  101. }
  102. /**
  103. * Record that a job within the batch failed to finish successfully, executing any callbacks if necessary.
  104. *
  105. * @param string $jobId
  106. * @param \Throwable $e
  107. * @return void
  108. */
  109. public function recordFailedJob(string $jobId, $e)
  110. {
  111. //
  112. }
  113. /**
  114. * Increment the failed jobs for the batch.
  115. *
  116. * @param string $jobId
  117. * @return \Illuminate\Bus\UpdatedBatchJobCounts
  118. */
  119. public function incrementFailedJobs(string $jobId)
  120. {
  121. return new UpdatedBatchJobCounts;
  122. }
  123. /**
  124. * Cancel the batch.
  125. *
  126. * @return void
  127. */
  128. public function cancel()
  129. {
  130. $this->cancelledAt = Carbon::now();
  131. }
  132. /**
  133. * Delete the batch from storage.
  134. *
  135. * @return void
  136. */
  137. public function delete()
  138. {
  139. $this->deleted = true;
  140. }
  141. /**
  142. * Determine if the batch has been deleted.
  143. *
  144. * @return bool
  145. */
  146. public function deleted()
  147. {
  148. return $this->deleted;
  149. }
  150. }