You are here

public function BrowscapImporter::import in Browscap 8.3

Helper function to update the browscap data.

Parameters

BrowscapEndpoint $browscap: The endpoint service for Browscap.

bool $cron: Optional import environment. If false, display status messages to the user in addition to logging information with the watchdog.

Return value

int A code indicating the result:

  • BROWSCAP_IMPORT_OK: New data was imported.
  • BROWSCAP_IMPORT_NO_NEW_VERSION: No new data version was available.
  • BROWSCAP_IMPORT_VERSION_ERROR: Checking the current data version failed.
  • BROWSCAP_IMPORT_DATA_ERROR: The data could not be downloaded or parsed.
1 call to BrowscapImporter::import()
BrowscapImportTest::testImport in src/Tests/BrowscapImportTest.php
Tests importing then querying Browscap data.

File

src/BrowscapImporter.php, line 72

Class

BrowscapImporter
Class BrowscapImporter.

Namespace

Drupal\browscap

Code

public function import(BrowscapEndpoint $browscap, $cron = TRUE) {
  $config = $this->config
    ->getEditable('browscap.settings');

  /*
   * Check if there is a new version
   */
  $local_version = $config
    ->get('version');
  \Drupal::logger('browscap')
    ->notice('Checking for new browscap version...');
  $current_version = $browscap
    ->getVersion();

  // Was it an error?
  if ($current_version == BrowscapImporter::BROWSCAP_IMPORT_VERSION_ERROR) {

    // Display a message to the user if the update process was triggered
    // manually.
    if ($cron == FALSE) {
      drupal_set_message($this
        ->t("Couldn't check version."), 'error');
    }
    return BrowscapImporter::BROWSCAP_IMPORT_VERSION_ERROR;
  }

  // Compare the current and local version numbers to determine if the
  // Browscap data requires updating.
  if ($current_version == $local_version) {

    // Log a message with the watchdog.
    \Drupal::logger('browscap')
      ->info('No new version of browscap to import');

    // Display a message to user if the update process was triggered manually.
    if ($cron == FALSE) {
      drupal_set_message($this
        ->t('No new version of browscap to import'));
    }
    return BrowscapImporter::BROWSCAP_IMPORT_NO_NEW_VERSION;
  }

  /*
   * If there is a new version retrieve the new data
   */
  $browscap_data = $browscap
    ->getBrowscapData($cron);

  // Process the browscap data.
  $result = $this
    ->processData($browscap_data);

  // If it's not an array, it's an error.
  if ($result != static::BROWSCAP_IMPORT_OK) {
    return $result;
  }

  // Clear the browscap data cache.
  $this->cache
    ->invalidateAll();

  // Update the browscap version and imported time.
  $config
    ->set('version', $current_version)
    ->set('imported', REQUEST_TIME)
    ->save();

  // Log a message with the watchdog.
  \Drupal::logger('browscap')
    ->notice('New version of browscap imported: %version', [
    '%version' => $current_version,
  ]);

  // Display a message to user if the update process was triggered manually.
  if ($cron == FALSE) {
    drupal_set_message($this
      ->t('New version of browscap imported: %version', [
      '%version' => $current_version,
    ]));
  }
  return static::BROWSCAP_IMPORT_OK;
}