You are here

function uc_ups_cron in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 shipping/uc_ups/uc_ups.module \uc_ups_cron()
  2. 7.3 shipping/uc_ups/uc_ups.module \uc_ups_cron()

Implements hook_cron().

Deletes UPS shipping labels from the file system automatically on a periodic basis. Cron must be enabled for automatic deletion. Default is never delete the labels, keep them forever.

File

shipping/uc_ups/uc_ups.module, line 42
UPS shipping quote module.

Code

function uc_ups_cron() {
  $ups_config = \Drupal::config('uc_ups.settings');
  $cutoff = \Drupal::time()
    ->getRequestTime() - $ups_config
    ->get('label_lifetime');
  if ($cutoff == \Drupal::time()
    ->getRequestTime()) {

    // Label lifetime is set to 0, meaning never delete.
    return;
  }

  // Loop over label files in public://ups_labels and test
  // creation date against 'uc_ups_label_lifetime'.
  $files = file_scan_directory('public://ups_labels', '/^label-/');
  foreach ($files as $file) {
    if ($cutoff > filectime($file->uri)) {
      \Drupal::service('file_system')
        ->unlink($file->uri);
      \Drupal::logger('uc_ups')
        ->notice('Removed uc_ups label file @file.', [
        '@file' => $file->uri,
      ]);
    }
  }
}