You are here

function fontscom_api_get_all_remote_fonts_count in @font-your-face 8.3

Gets total font count.

Parameters

bool $reset: If the cache should be flushed and force an API request.

Return value

int Total number of fonts on fonts.com.

2 calls to fontscom_api_get_all_remote_fonts_count()
fontscom_api_fontyourface_import in modules/fontscom_api/fontscom_api.module
Implements hook_fontyourface_import().
fontscom_api_form_font_settings_submit in modules/fontscom_api/fontscom_api.module
Submits Font settings form data.

File

modules/fontscom_api/fontscom_api.module, line 390
Fonts.com API module file.

Code

function fontscom_api_get_all_remote_fonts_count($reset = FALSE) {
  $data = NULL;
  if (!$reset && ($cache = \Drupal::cache()
    ->get('fontscom_api_remote_fonts_count'))) {
    return $cache->data;
  }
  try {
    $filters = fontscom_api_get_allowed_api_filters();
    $path = '/rest/json/AllFonts/?wfspstart=0&wfsplimit=1';
    if ($filters->FreeOrPaid == 0) {
      $path .= '&wfsfree=true';
    }
    $uri = FONTSCOM_API_BASE_URL . $path;
    $response = \Drupal::httpClient()
      ->get($uri, [
      'headers' => fontscom_api_headers($path),
      'verify' => FALSE,
    ]);
    $data = json_decode((string) $response
      ->getBody());
  } catch (Exception $e) {
    Drupal::messenger()
      ->addMessage(t('There was an error retrieving total Font count from Fonts.com. Error: %error', [
      '%error' => $e
        ->getMessage(),
    ]), 'error');
    return FALSE;
  }
  \Drupal::cache()
    ->set('fontscom_api_remote_fonts_count', $data->AllFonts->TotalRecords);
  return $data->AllFonts->TotalRecords;
}