You are here

public function DefaultExchangerImporter::run in Commerce Exchanger 8

Triggers importing exchange rates.

Parameters

bool $force: If import is forced regardless of cron setup.

Overrides ExchangerImporterInterface::run

File

src/DefaultExchangerImporter.php, line 54

Class

DefaultExchangerImporter
Class DefaultExchangerImporter.

Namespace

Drupal\commerce_exchanger

Code

public function run($force = FALSE) {
  foreach ($this->providers as $provider) {
    assert($provider instanceof ExchangeRatesInterface);
    $plugin = $provider
      ->getPlugin();
    if ($plugin instanceof ExchangerProviderRemoteInterface) {
      $last_update = $this->state
        ->get('commerce_exchanger.' . $provider
        ->id() . ' .last_update_time');
      $cron_setup = $provider
        ->getPluginConfiguration()['cron'] ?? 1;
      $cron_schedule = time() - 24 / $cron_setup * 60 * 60;

      // Exclude manual plugins. Check either time or force import.
      if ($force || $last_update < $cron_schedule) {
        $plugin
          ->import();

        // Update last imported time.
        $this->state
          ->set('commerce_exchanger.' . $provider
          ->id() . ' .last_update_time', time());
      }
    }
  }
}