You are here

public function State::incrementApiRateCount in CloudFlare 8

Increment the count of api calls done in the past 5 minutes.

Overrides CloudFlareStateInterface::incrementApiRateCount

See also

https://api.cloudflare.com/#requests

File

src/State.php, line 78

Class

State
Tracks rate limits associated with CloudFlare Api.

Namespace

Drupal\cloudflare

Code

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);
  }
}