Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 5, 2025
1 parent 1714646 commit 3d08d3a
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/Illuminate/Bus/PendingBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Conditionable;
use Laravel\SerializableClosure\SerializableClosure;
use RuntimeException;
use Throwable;

use function Illuminate\Support\enum_value;
Expand Down Expand Up @@ -56,8 +57,9 @@ class PendingBatch
public function __construct(Container $container, Collection $jobs)
{
$this->container = $container;

$this->jobs = $jobs->each(function (object|array $job) {
$this->checkJobIsBatchable($job);
$this->ensureJobIsBatchable($job);
});
}

Expand All @@ -72,13 +74,35 @@ public function add($jobs)
$jobs = is_iterable($jobs) ? $jobs : Arr::wrap($jobs);

foreach ($jobs as $job) {
$this->checkJobIsBatchable($job);
$this->ensureJobIsBatchable($job);

$this->jobs->push($job);
}

return $this;
}

/**
* Ensure the given job is batchable.
*
* @param object|array $job
* @return void
*/
protected function ensureJobIsBatchable(object|array $job): void
{
foreach (Arr::wrap($job) as $job) {
if ($job instanceof PendingBatch) {
$this->ensureJobIsBatchable($job->jobs->all());

return;
}

if (! in_array(Batchable::class, class_uses_recursive($job))) {
throw new RuntimeException(sprintf('Attempted to batch job [%s], but it does not use the Batchable trait.', $job::class));
}
}
}

/**
* Add a callback to be executed when the batch is stored.
*
Expand Down Expand Up @@ -417,19 +441,4 @@ protected function store($repository)

return $batch;
}

private function checkJobIsBatchable(object|array $job): void
{
foreach (Arr::wrap($job) as $job) {
if ($job instanceof PendingBatch) {
$this->checkJobIsBatchable($job->jobs->all());

return;
}

if (! in_array(Batchable::class, class_uses_recursive($job))) {
throw new \RuntimeException(sprintf('Job %s must use Batchable trait', $job::class));
}
}
}
}

0 comments on commit 3d08d3a

Please sign in to comment.