quickbooks_api-8.x-1.0-beta4/quickbooks_api.module
quickbooks_api.module
<?php
/**
* @file
* Adds callback methods for oauth and cron.
*/
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\quickbooks_api\QuickbooksService;
/**
* Config form submit function to disconnect from Quickbooks.
*/
function quickbooks_api_config_form_oauth_disconnect() {
$logger_channel = 'quickbooks_api';
$refresh_token = \Drupal::state()->get(QuickbooksService::STATE_REFRESH_TOKEN);
$access_token = \Drupal::state()->get(QuickbooksService::STATE_ACCESS_TOKEN);
// We need to initialize the Quickbooks API service.
/** @var \Drupal\quickbooks_api\QuickbooksService $qbo */
$qbo = \Drupal::service("quickbooks_api.QuickbooksService");
// Get the login helper service.
$helper = $qbo->dataService()->getOAuth2LoginHelper();
// Revoke both the refresh and access tokens.
$tokens = [
'access' => $access_token,
'refresh' => $refresh_token,
];
foreach ($tokens as $label => $token) {
if ($token) {
try {
$revocation = $helper->revokeToken($token);
if ($revocation) {
$message = new TranslatableMarkup("Quickbooks API @label token revoked.", [
'@label' => $label,
]);
\Drupal::messenger()->addMessage($message);
\Drupal::logger($logger_channel)->notice($message);
}
}
catch (\Exception $exception) {
$message = new TranslatableMarkup("@label token revocation failure: @message", [
'@label' => $label,
'@message' => $exception->getMessage(),
]);
\Drupal::messenger()->addMessage($message);
\Drupal::logger($logger_channel)->error($message);
}
}
}
// Delete all authentication data from state.
$states = [
QuickbooksService::STATE_ACCESS_TOKEN,
QuickbooksService::STATE_ACCESS_TOKEN_EXPIRY,
QuickbooksService::STATE_REFRESH_TOKEN,
QuickbooksService::STATE_REFRESH_EXPIRY,
];
\Drupal::state()->deleteMultiple($states);
}
/**
* Run cron service.
*
* @todo This should all just go in the QuickbookService class.
*/
function quickbooks_api_cron() {
/** @var \Drupal\quickbooks_api\Cron $cron */
$cron = \Drupal::service('quickbooks_api.cron');
// Attempt to refresh oauth.
$cron->refresh();
}
