You are here

function commerce_license_synchronization_queue_process in Commerce License 7

Worker callback for synchronizing licenses.

1 string reference to 'commerce_license_synchronization_queue_process'
commerce_license_advanced_queue_info in ./commerce_license.module
Implements hook_advanced_queue_info().

File

./commerce_license.module, line 659
Provides a framework for selling access to local or remote resources.

Code

function commerce_license_synchronization_queue_process($item) {
  $license = entity_load_single('commerce_license', $item->data['license_id']);
  if (!$license) {
    return array(
      'status' => ADVANCEDQUEUE_STATUS_FAILURE,
      'result' => 'License #' . $item->data['license_id'] . ' no longer exists',
    );
  }

  // Synchronize the license.
  $result = $license
    ->synchronize();
  $sync_status = $license->wrapper->sync_status
    ->value();

  // Before commerce_license 7.x-1.3 license types didn't need to set the
  // sync_status inside synchronize(), it was enough to return a boolean.
  // Handle compatibility with license types that still do that.
  if ($sync_status == COMMERCE_LICENSE_NEEDS_SYNC) {
    $sync_status = $result ? COMMERCE_LICENSE_SYNCED : COMMERCE_LICENSE_SYNC_FAILED;
    $license->wrapper->sync_status = $sync_status;
    $license
      ->save();
  }

  // Handle the sync result.
  $success_message = 'Processed license #' . $license->license_id;
  $fail_message = 'Synchronization failed for license #' . $license->license_id;
  switch ($sync_status) {
    case COMMERCE_LICENSE_SYNCED:

      // Fire a rules event and a hook, allowing developers to respond
      // to a successful synchronization (e.g. sending a notification mail).
      rules_invoke_all('commerce_license_synchronize', $license);
      return array(
        'status' => ADVANCEDQUEUE_STATUS_SUCCESS,
        'result' => $success_message,
      );
    case COMMERCE_LICENSE_SYNC_FAILED_RETRY:
      return array(
        'status' => ADVANCEDQUEUE_STATUS_FAILURE_RETRY,
        'result' => $fail_message . ': Please retry',
      );
    case COMMERCE_LICENSE_SYNC_FAILED:

      // Fire a rules event and a hook, allowing developers to respond
      // to a failed synchronization.
      rules_invoke_all('commerce_license_synchronize_failed', $license);
      return array(
        'status' => ADVANCEDQUEUE_STATUS_FAILURE,
        'result' => $fail_message,
      );
  }
}