You are here

class State in CloudFlare 8

Tracks rate limits associated with CloudFlare Api.

Hierarchy

Expanded class hierarchy of State

4 files declare their use of State
ApiRateLimitCheckTest.php in modules/cloudflarepurger/tests/src/Unit/ApiRateLimitCheckTest.php
DailyTagPurgeLimitCheckTest.php in modules/cloudflarepurger/tests/src/Unit/DailyTagPurgeLimitCheckTest.php
DiagnosticCheckTestBase.php in modules/cloudflarepurger/tests/src/Unit/DiagnosticCheckTestBase.php
StateTest.php in tests/src/Unit/StateTest.php
1 string reference to 'State'
cloudflare.services.yml in ./cloudflare.services.yml
cloudflare.services.yml
1 service uses State
cloudflare.state in ./cloudflare.services.yml
Drupal\cloudflare\State

File

src/State.php, line 11

Namespace

Drupal\cloudflare
View source
class State implements CloudFlareStateInterface {
  const TAG_PURGE_DAILY_COUNT = "cloudflare_tag_purge_daily_count";
  const TAG_PURGE_DAILY_COUNT_START = "cloudflare_tag_purge_daily_start";
  const API_RATE_COUNT = "cloudflare_api_rate_count";
  const API_RATE_COUNT_START = "cloudflare_api_rate_count_start";

  /**
   * Tracks rate limits associated with CloudFlare Api.
   *
   * @var \Drupal\cloudflare\CloudFlareStateInterface
   */
  protected $state;

  /**
   * Timestamp service.
   *
   * @var \Drupal\CloudFlare\CloudFlareTimestampInterface
   */
  protected $timestamper;

  /**
   * State constructor.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The drupal state service.
   * @param \Drupal\CloudFlare\CloudFlareTimestampInterface $timestamper
   *   Cloudflare timestamp service.
   */
  public function __construct(StateInterface $state, CloudFlareTimestampInterface $timestamper) {
    $this->state = $state;
    $this->timestamper = $timestamper;
  }

  /**
   * {@inheritdoc}
   */
  public function incrementTagPurgeDailyCount() {
    $count = $this->state
      ->get(self::TAG_PURGE_DAILY_COUNT);
    $last_recorded_timestamp = $this->state
      ->get(self::TAG_PURGE_DAILY_COUNT_START);
    $last_recorded_timestamp = is_null($last_recorded_timestamp) ? new DateTime('2001-01-01') : $last_recorded_timestamp;
    $now = $this->timestamper
      ->now();
    $todays_date = $now
      ->format('Y-m-d');
    $last_recorded_date = $last_recorded_timestamp
      ->format('Y-m-d');
    if (empty($last_recorded_timestamp) || $last_recorded_date != $todays_date) {
      $this->state
        ->set(self::TAG_PURGE_DAILY_COUNT, 1);
      $this->state
        ->set(self::TAG_PURGE_DAILY_COUNT_START, $now);
    }
    else {
      $count++;
      $this->state
        ->set(self::TAG_PURGE_DAILY_COUNT, $count);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getTagDailyCount() {
    return $this->state
      ->get(self::TAG_PURGE_DAILY_COUNT);
  }

  /**
   * {@inheritdoc}
   */
  public function incrementApiRateCount() {
    $count = $this->state
      ->get(self::API_RATE_COUNT);
    $last_recorded_timestamp = $this->state
      ->get(self::API_RATE_COUNT_START);
    $last_recorded_timestamp = is_null($last_recorded_timestamp) ? new DateTime('2001-01-01') : $last_recorded_timestamp;
    $now = $this->timestamper
      ->now();
    $diff = $now
      ->getTimestamp() - $last_recorded_timestamp
      ->getTimestamp();
    $minutes_passed = $diff / 60;
    if ($minutes_passed >= 5) {
      $this->state
        ->set(self::API_RATE_COUNT, 1);
      $this->state
        ->set(self::API_RATE_COUNT_START, $now);
    }
    else {
      $this->state
        ->set(self::API_RATE_COUNT, ++$count);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getApiRateCount() {
    $count = $this->state
      ->get(self::API_RATE_COUNT);
    return $count;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
State::$state protected property Tracks rate limits associated with CloudFlare Api.
State::$timestamper protected property Timestamp service.
State::API_RATE_COUNT constant
State::API_RATE_COUNT_START constant
State::getApiRateCount public function Get the count of purges done in the past 5 minutes. Overrides CloudFlareStateInterface::getApiRateCount
State::getTagDailyCount public function Get the count of tag purges done today. Overrides CloudFlareStateInterface::getTagDailyCount
State::incrementApiRateCount public function Increment the count of api calls done in the past 5 minutes. Overrides CloudFlareStateInterface::incrementApiRateCount
State::incrementTagPurgeDailyCount public function Increment the count of tag purges done today. Overrides CloudFlareStateInterface::incrementTagPurgeDailyCount
State::TAG_PURGE_DAILY_COUNT constant
State::TAG_PURGE_DAILY_COUNT_START constant
State::__construct public function State constructor.