Rename ::setError() to ::afterSend()

This commit is contained in:
Zach Borboa
2023-08-25 06:12:03 -07:00
parent d19e6b97c6
commit 11bd600918
6 changed files with 29 additions and 34 deletions
+2 -2
View File
@@ -199,6 +199,7 @@ More examples are available under [/examples](https://github.com/php-curl-class/
Curl::__construct($base_url = null, $options = [])
Curl::__destruct()
Curl::__get($name)
Curl::afterSend($callback)
Curl::attemptRetry()
Curl::beforeSend($callback)
Curl::buildPostData($data)
@@ -278,7 +279,6 @@ Curl::setDefaultTimeout()
Curl::setDefaultUserAgent()
Curl::setDefaultXmlDecoder()
Curl::setDigestAuthentication($username, $password = '')
Curl::setError($function)
Curl::setFile($file)
Curl::setFollowLocation($follow_location = true)
Curl::setForbidReuse($forbid_reuse = true)
@@ -323,6 +323,7 @@ MultiCurl::addPatch($url, $data = [])
MultiCurl::addPost($url, $data = '', $follow_303_with_post = false)
MultiCurl::addPut($url, $data = [])
MultiCurl::addSearch($url, $data = [])
MultiCurl::afterSend($callback)
MultiCurl::beforeSend($callback)
MultiCurl::close()
MultiCurl::complete($callback)
@@ -341,7 +342,6 @@ MultiCurl::setCookieJar($cookie_jar)
MultiCurl::setCookieString($string)
MultiCurl::setCookies($cookies)
MultiCurl::setDigestAuthentication($username, $password = '')
MultiCurl::setError($function)
MultiCurl::setFile($file)
MultiCurl::setFollowLocation($follow_location = true)
MultiCurl::setForbidReuse($forbid_reuse = true)
+12 -11
View File
@@ -7,7 +7,7 @@ namespace Curl;
abstract class BaseCurl
{
public $beforeSendCallback = null;
public $errorDecider = null;
public $afterSendCallback = null;
public $successCallback = null;
public $errorCallback = null;
public $completeCallback = null;
@@ -140,24 +140,25 @@ abstract class BaseCurl
}
/**
* Set Error
* After Send
*
* Use this function to override the default implemention for determining
* whether or not the request errored. The instance is passed as the first
* argument to the function and the instance has attributes like
* $instance->httpStatusCode and $instance->response to help decide if the
* request errored. Set $instance->error to true or false within the
* function.
* This function is called after the request has been sent.
*
* It can be used to override whether or not the request errored. The
* instance is passed as the first argument to the function and the instance
* has attributes like $instance->httpStatusCode and $instance->response to
* help decide if the request errored. Set $instance->error to true or false
* within the function.
*
* When $instance->error is true indicating a request error, the error
* callback set by Curl::error() is called. When $instance->error is false,
* the success callback set by Curl::success() is called.
*
* @param $function callable
* @param $callback callable|null
*/
public function setError($function)
public function afterSend($callback)
{
$this->errorDecider = $function;
$this->afterSendCallback = $callback;
}
/**
+5 -11
View File
@@ -536,18 +536,12 @@ class Curl extends BaseCurl
$this->httpStatusCode = $this->getInfo(CURLINFO_HTTP_CODE);
$this->httpError = in_array((int) floor($this->httpStatusCode / 100), [4, 5], true);
$this->error = $this->curlError || $this->httpError;
if ($this->errorDecider === null) {
$this->error = $this->curlError || $this->httpError;
} else {
$this->error = null;
$this->call($this->errorDecider);
if (!in_array($this->error, [true, false], true)) {
trigger_error(
'$instance->error MUST be set to true or false inside the setError() function',
E_USER_WARNING
);
}
$this->call($this->afterSendCallback);
if (!in_array($this->error, [true, false], true)) {
trigger_error('$instance->error MUST be set to true or false', E_USER_WARNING);
}
$this->errorCode = $this->error ? ($this->curlError ? $this->curlErrorCode : $this->httpStatusCode) : 0;
+2 -2
View File
@@ -844,8 +844,8 @@ class MultiCurl extends BaseCurl
if ($curl->beforeSendCallback === null) {
$curl->beforeSend($this->beforeSendCallback);
}
if ($curl->errorDecider === null) {
$curl->setError($this->errorDecider);
if ($curl->afterSend === null) {
$curl->afterSend($this->afterSendCallback);
}
if ($curl->successCallback === null) {
$curl->success($this->successCallback);
+4 -4
View File
@@ -4921,11 +4921,11 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('bar', $test->curl->response->{'123'});
}
public function testSetErrorAttemptCount()
public function testAfterSendAttemptCount()
{
$test = new Test();
$test->curl->setRetry(10);
$test->curl->setError(function ($instance) {
$test->curl->afterSend(function ($instance) {
if ($instance->attempts < 5) {
$instance->error = true;
} else {
@@ -4938,12 +4938,12 @@ class PHPCurlClassTest extends \PHPUnit\Framework\TestCase
$this->assertFalse($test->curl->error);
}
public function testSetErrorResponseMessage()
public function testAfterSendResponseMessage()
{
$test = new Test();
$test->curl->setOpt(CURLOPT_COOKIEJAR, '/dev/null');
$test->curl->setRetry(5);
$test->curl->setError(function ($instance) {
$test->curl->afterSend(function ($instance) {
$instance->error = $instance->response->message !== '202 Accepted';
});
$test->server('retry', 'GET', ['failures' => 3]);
+4 -4
View File
@@ -5453,11 +5453,11 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase
$this->assertFalse($instance->error);
}
public function testSetErrorAttemptCount()
public function testAfterSendAttemptCount()
{
$multi_curl = new MultiCurl();
$multi_curl->setRetry(10);
$multi_curl->setError(function ($instance) {
$multi_curl->afterSend(function ($instance) {
if ($instance->attempts < 5) {
$instance->error = true;
} else {
@@ -5472,12 +5472,12 @@ class PHPMultiCurlClassTest extends \PHPUnit\Framework\TestCase
$this->assertFalse($instance->error);
}
public function testSetErrorResponseMessage()
public function testAfterSendResponseMessage()
{
$multi_curl = new MultiCurl();
$multi_curl->setOpt(CURLOPT_COOKIEJAR, '/dev/null');
$multi_curl->setRetry(5);
$multi_curl->setError(function ($instance) {
$multi_curl->afterSend(function ($instance) {
$instance->error = $instance->response->message !== '202 Accepted';
});
$multi_curl->setHeader('X-DEBUG-TEST', 'retry');