quickbooks_api-8.x-1.0-beta4/src/Cron.php

src/Cron.php
<?php

namespace Drupal\quickbooks_api;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\State\StateInterface;

/**
 * Handles running cron to renew Quickbooks API Oauth keys.
 */
class Cron {

  /**
   * Once a day is enough. (3600 seconds in an hour * 24 hours)
   */
  public const CRON_INTERVAL = 86400;

  /**
   * Constructs a Cron object.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $log
   *   Quickbooks Logger Channel.
   * @param \Drupal\quickbooks_api\QuickbooksService $quickbooksService
   *   Quickbooks API service.
   */
  public function __construct(protected StateInterface $state, protected ConfigFactoryInterface $configFactory, protected TimeInterface $time, protected LoggerChannelInterface $log, protected QuickbooksService $quickbooksService) {}

  /**
   * Method description.
   */
  public function refresh() {
    $request_time = $this->time->getRequestTime();

    // Do not attempt to run on an unconfigured system.
    $configured = FALSE;

    try {
      $this->quickbooksService->buildSettings();
      $configured = TRUE;
    }
    catch (\Exception $exception) {
      // Do nothing.
    }

    if (!$configured) {
      $this->log->notice("Skipping Quickbooks cron. System not configured.");
      return;
    }

    // We usually don't want to act every time cron runs (which could be every
    // minute) so keep a time for the next run in the site state.
    $next_execution = $this->state->get('quickbooks_api.next_execution', 0);

    if ($request_time >= $next_execution) {

      // Calling the data service up should refresh access token if needed.
      $data_service = $this->quickbooksService->dataService();
      // Using the 24 buffer, check if the refresh token should be refreshed.
      $refresh_token_expiry = $this->state->get(QuickbooksService::STATE_REFRESH_EXPIRY);

      if (($this->time->getCurrentTime() - self::CRON_INTERVAL) > $refresh_token_expiry) {
        $helper = $data_service->getOAuth2LoginHelper();
        // Refresh all tokens.
        $tokens = $helper->refreshToken();
        // Set the new access token and expiry to state.
        $this->state->set(QuickbooksService::STATE_ACCESS_TOKEN, $tokens->getAccessToken());
        $this->state->set(QuickbooksService::STATE_ACCESS_TOKEN_EXPIRY, QuickbooksService::addTokenBuffer(strtotime($tokens->getAccessTokenExpiresAt())));
        $this->log->info("Quickbooks API access token updated");
        // Update the refresh too.
        $this->state->set(QuickbooksService::STATE_REFRESH_TOKEN, $tokens->getRefreshToken());
        $this->state->set(QuickbooksService::STATE_REFRESH_EXPIRY, QuickbooksService::addTokenBuffer(strtotime($tokens->getRefreshTokenExpiresAt())));
        $this->log->info("Quickbooks API refresh token updated");
      }
      // Set the next execution time.
      $this->state->set('quickbooks_api.next_execution', $request_time + self::CRON_INTERVAL);
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc