mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
89 lines
1.7 KiB
PHP
89 lines
1.7 KiB
PHP
|
<?php
|
||
|
/**
|
||
|
* Failure registry.
|
||
|
*
|
||
|
* This class is used to remember API failures.
|
||
|
* Mostly to prevent multiple failed API requests.
|
||
|
*
|
||
|
* @package WooCommerce\PayPalCommerce\ApiClient\Helper
|
||
|
*/
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
|
||
|
|
||
|
/**
|
||
|
* Class FailureRegistry
|
||
|
*/
|
||
|
class FailureRegistry {
|
||
|
const CACHE_KEY = 'failure_registry';
|
||
|
const CACHE_TIMEOUT = DAY_IN_SECONDS; // If necessary we can increase this.
|
||
|
|
||
|
const SELLER_STATUS_KEY = 'seller_status';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* The Cache.
|
||
|
*
|
||
|
* @var Cache
|
||
|
*/
|
||
|
private $cache;
|
||
|
|
||
|
/**
|
||
|
* FailureRegistry constructor.
|
||
|
*
|
||
|
* @param Cache $cache The Cache.
|
||
|
*/
|
||
|
public function __construct( Cache $cache ) {
|
||
|
$this->cache = $cache;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $key
|
||
|
* @param int $seconds
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function has_failure_in_timeframe( string $key, int $seconds ): bool {
|
||
|
$cache_key = $this->cache_key( $key );
|
||
|
$failure_time = $this->cache->get( $cache_key );
|
||
|
|
||
|
if ( ! $failure_time ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$expiration = $failure_time + $seconds;
|
||
|
return $expiration > time();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $key
|
||
|
* @return void
|
||
|
*/
|
||
|
public function add_failure( string $key ) {
|
||
|
$cache_key = $this->cache_key( $key );
|
||
|
$this->cache->set( $cache_key, time(), self::CACHE_TIMEOUT );
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $key
|
||
|
* @return void
|
||
|
*/
|
||
|
public function clear_failures( string $key ) {
|
||
|
$cache_key = $this->cache_key( $key );
|
||
|
if ( $this->cache->has( $cache_key ) ) {
|
||
|
$this->cache->delete( $cache_key );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Build cache key.
|
||
|
*
|
||
|
* @param string $key The cache key.
|
||
|
* @return string|null
|
||
|
*/
|
||
|
private function cache_key( string $key ): ?string {
|
||
|
return implode( '_', array( self::CACHE_KEY, $key ) );
|
||
|
}
|
||
|
|
||
|
}
|