You are here

public function CronHandler::refreshTokens in Instagram Feeds 8

Refreshes Instagram token as per scheduled frequency.

Return value

$this

File

src/CronHandler.php, line 347

Class

CronHandler
Instagram Feeds Cron Handler Service.

Namespace

Drupal\instagram_feeds

Code

public function refreshTokens() {
  $current_time = \Drupal::time()
    ->getRequestTime();
  $frequency = $this->config
    ->get('refresh_frequency');

  /** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $account */
  foreach ($this
    ->getInstagramAccounts() as $account) {

    // Long-lived tokens are valid 60 days, so token_expiration - 60 days
    // (5184000 sec) will be the date, when token was generated/refreshed
    // last time. Continue only if frequency period has gone.
    // Expired token can no longer be regenerated.
    if ($current_time > $account
      ->getTokenExpirationTime() - 5184000 + $frequency) {
      $account
        ->refreshToken($this->httpClient, TRUE);
    }
  }
  return $this;
}