Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
formatting
  • Loading branch information
taylorotwell committed Sep 29, 2025
commit 109b2bd2f61c351fad84cb148dcb7b120087ec16
178 changes: 63 additions & 115 deletions src/Illuminate/Http/Client/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
protected $factory;

/**
* The list of requests.
* The array of requests.
*
* @var array<array-key, \Illuminate\Http\Client\PendingRequest>
*/
Expand Down Expand Up @@ -105,7 +105,7 @@
public $createdAt = null;

/**
* The date when the batch was finished.
* The date when the batch finished.
*
* @var \Carbon\CarbonImmutable|null
*/
Expand Down Expand Up @@ -142,46 +142,6 @@
return $this->requests[$key] = $this->asyncRequest();
}

/**
* Get the total number of requests that have been processed by the batch thus far.
*
* @return non-negative-int
*/
public function processedRequests(): int
{
return $this->totalRequests - $this->pendingRequests;
}

/**
* Get the percentage of requests that have been processed (between 0-100).
*
* @return non-negative-int
*/
public function completion(): int
{
return $this->totalRequests > 0 ? round(($this->processedRequests() / $this->totalRequests) * 100) : 0;
}

/**
* Determine if the batch has job failures.
*
* @return bool
*/
public function hasFailures(): bool
{
return $this->failedRequests > 0;
}

/**
* Determine if the batch has finished executing.
*
* @return bool
*/
public function finished(): bool
{
return ! is_null($this->finishedAt);
}

/**
* Register a callback to run before the first request from the batch runs.
*
Expand All @@ -195,16 +155,6 @@
return $this;
}

/**
* Retrieve the before callback in the batch.
*
* @return (\Closure($this): void)|null
*/
public function beforeCallback(): ?Closure
{
return $this->beforeCallback;
}

/**
* Register a callback to run after a request from the batch succeeds.
*
Expand All @@ -218,16 +168,6 @@
return $this;
}

/**
* Retrieve the progress callback in the batch.
*
* @return (\Closure($this, int|string, \Illuminate\Http\Response): void)|null
*/
public function progressCallback(): ?Closure
{
return $this->progressCallback;
}

/**
* Register a callback to run after a request from the batch fails.
*
Expand All @@ -241,16 +181,6 @@
return $this;
}

/**
* Retrieve the catch callback in the batch.
*
* @return (\Closure($this, int|string, \Illuminate\Http\Response|\Illuminate\Http\Client\RequestException): void)|null
*/
public function catchCallback(): ?Closure
{
return $this->catchCallback;
}

/**
* Register a callback to run after all the requests from the batch succeed.
*
Expand All @@ -264,16 +194,6 @@
return $this;
}

/**
* Retrieve the then callback in the batch.
*
* @return (\Closure($this, array<int|string, \Illuminate\Http\Response>): void)|null
*/
public function thenCallback(): ?Closure
{
return $this->thenCallback;
}

/**
* Register a callback to run after all the requests from the batch finish.
*
Expand All @@ -288,36 +208,22 @@
}

/**
* Retrieve the finally callback in the batch.
* Send all of the requests in the batch.
*
* @return (\Closure($this, array<int|string, \Illuminate\Http\Response>): void)|null
*/
public function finallyCallback(): ?Closure
{
return $this->finallyCallback;
}

/**
* @return array<int|string, \Illuminate\Http\Response|\Illuminate\Http\Client\RequestException>
*/
public function send(): array
{
$this->inProgress = true;
$results = [];

$requests = $this->getRequests();
$beforeCallback = $this->beforeCallback();
$progressCallback = $this->progressCallback();
$catchCallback = $this->catchCallback();
$thenCallback = $this->thenCallback();
$finallyCallback = $this->finallyCallback();

if ($beforeCallback !== null) {
$beforeCallback($this);
if ($this->beforeCallback !== null) {
call_user_func($this->beforeCallback, $this);
}

$results = [];
$promises = [];
foreach ($requests as $key => $item) {

foreach ($this->requests as $key => $item) {
$promise = match (true) {
$item instanceof PendingRequest => $item->getPromise(),
default => $item,
Expand All @@ -328,36 +234,38 @@

if (! empty($promises)) {
(new EachPromise($promises, [
'fulfilled' => function ($result, $key) use (&$results, $progressCallback, $catchCallback) {
'fulfilled' => function ($result, $key) use (&$results) {
$results[$key] = $result;

$this->decrementPendingRequests();

if ($result instanceof Response && $result->successful()) {
if ($progressCallback !== null) {
$progressCallback($this, $key, $result);
if ($this->progressCallback !== null) {
call_user_func($this->progressCallback, $this, $key, $result);
}

return $result;
}

if (($result instanceof Response && $result->failed()) || $result instanceof RequestException) {
if (($result instanceof Response && $result->failed()) ||
$result instanceof RequestException) {
$this->incrementFailedRequests();

if ($catchCallback !== null) {
$catchCallback($this, $key, $result);
if ($this->catchCallback !== null) {
call_user_func($this->catchCallback, $this, $key, $result);
}
}

return $result;
},
'rejected' => function ($reason, $key) use ($catchCallback) {

Check failure on line 261 in src/Illuminate/Http/Client/Batch.php

View workflow job for this annotation

GitHub Actions / Source Code

Undefined variable: $catchCallback

Check failure on line 261 in src/Illuminate/Http/Client/Batch.php

View workflow job for this annotation

GitHub Actions / Source Code

Anonymous function has an unused use $catchCallback.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WendellAdriel There is an alert on this line with the use ($catchCallback). Is this used? Looks like it's only using $this->catchCallback and the use statement could be deleted

$this->decrementPendingRequests();

if ($reason instanceof RequestException) {
$this->incrementFailedRequests();

if ($catchCallback !== null) {
$catchCallback($this, $key, $reason);
if ($this->catchCallback !== null) {
call_user_func($this->catchCallback, $this, $key, $reason);
}
}

Expand All @@ -366,15 +274,15 @@
]))->promise()->wait();
}

if (! $this->hasFailures() && $thenCallback !== null) {
$thenCallback($this, $results);
if (! $this->hasFailures() && $this->thenCallback !== null) {
call_user_func($this->thenCallback, $this, $results);
}

if ($finallyCallback !== null) {
$finallyCallback($this, $results);
if ($this->finallyCallback !== null) {
call_user_func($this->finallyCallback, $this, $results);
}

$this->finishedAt = new CarbonImmutable();
$this->finishedAt = new CarbonImmutable;
$this->inProgress = false;

return $results;
Expand All @@ -390,6 +298,36 @@
return $this->factory->setHandler($this->handler)->async();
}

/**
* Get the total number of requests that have been processed by the batch thus far.
*
* @return non-negative-int
*/
public function processedRequests(): int
{
return $this->totalRequests - $this->pendingRequests;
}

/**
* Get the percentage of requests that have been processed (between 0-100).
*
* @return non-negative-int
*/
public function completion(): int
{
return $this->totalRequests > 0 ? round(($this->processedRequests() / $this->totalRequests) * 100) : 0;
}

/**
* Determine if the batch has finished executing.
*
* @return bool
*/
public function finished(): bool
{
return ! is_null($this->finishedAt);
}

/**
* Increment the count of total and pending requests in the batch.
*
Expand All @@ -411,6 +349,16 @@
$this->pendingRequests--;
}

/**
* Determine if the batch has job failures.
*
* @return bool
*/
public function hasFailures(): bool
{
return $this->failedRequests > 0;
}

/**
* Increment the count of failed requests in the batch.
*
Expand Down
Loading