| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
|
| 4 |
namespace Yoast\WP\SEO\MyYoast_Client\Application\Exceptions; |
| 5 |
|
| 6 |
/** |
| 7 |
* Exception thrown when the authorization server rate-limits a DCR or |
| 8 |
* RFC 7592 request (HTTP 429). |
| 9 |
* |
| 10 |
* Extends Registration_Failed_Exception so existing catch sites keep working. |
| 11 |
* Carries the parsed `Retry-After` value (in seconds) so the UI can show |
| 12 |
* the user how long to wait. |
| 13 |
*/ |
| 14 |
class Rate_Limited_Exception extends Registration_Failed_Exception { |
| 15 |
|
| 16 |
/** |
| 17 |
* Number of seconds the caller should wait before retrying, or null |
| 18 |
* when the server didn't send a usable `Retry-After` header. |
| 19 |
* |
| 20 |
* @var int|null |
| 21 |
*/ |
| 22 |
private $retry_after_seconds; |
| 23 |
|
| 24 |
/** |
| 25 |
* Rate_Limited_Exception constructor. |
| 26 |
* |
| 27 |
* @param string $message The exception message. |
| 28 |
* @param int|null $retry_after_seconds Seconds until the caller may retry, or null. |
| 29 |
*/ |
| 30 |
public function __construct( string $message, ?int $retry_after_seconds = null ) { |
| 31 |
parent::__construct( $message ); |
| 32 |
$this->retry_after_seconds = $retry_after_seconds; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns the parsed `Retry-After` value in seconds, or null when absent. |
| 37 |
* |
| 38 |
* @return int|null |
| 39 |
*/ |
| 40 |
public function get_retry_after_seconds(): ?int { |
| 41 |
return $this->retry_after_seconds; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Parses an HTTP `Retry-After` value into seconds-until-retry. |
| 46 |
* |
| 47 |
* Accepts either the delta-seconds form (`"120"`) or the HTTP-date form |
| 48 |
* (`"Wed, 27 May 2026 14:30:00 GMT"`) per RFC 9110 §10.2.3. |
| 49 |
* |
| 50 |
* phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- Raw header value is heterogeneous (string, numeric, array, or null). |
| 51 |
* |
| 52 |
* @param mixed $retry_after The raw header value (string, numeric, array, or null). |
| 53 |
* |
| 54 |
* @return int|null Seconds until retry (clamped to >= 0), or null when unparseable. |
| 55 |
* |
| 56 |
* phpcs:enable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint |
| 57 |
*/ |
| 58 |
public static function parse_retry_after( $retry_after ): ?int { |
| 59 |
if ( \is_array( $retry_after ) ) { |
| 60 |
$retry_after = \reset( $retry_after ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( $retry_after === null || $retry_after === '' ) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
if ( \is_numeric( $retry_after ) ) { |
| 68 |
return \max( 0, (int) $retry_after ); |
| 69 |
} |
| 70 |
|
| 71 |
$timestamp = \strtotime( (string) $retry_after ); |
| 72 |
if ( $timestamp === false ) { |
| 73 |
return null; |
| 74 |
} |
| 75 |
|
| 76 |
return \max( 0, ( $timestamp - \time() ) ); |
| 77 |
} |
| 78 |
} |
| 79 |
|