You are here

public function OAuth2ServerEntityController::save in OAuth2 Server 7

Overrides EntityAPIControllerExportable::save().

Overrides EntityAPIControllerExportable::save

File

includes/oauth2_server.server_controller.inc, line 73

Class

OAuth2ServerEntityController
The entity controller for oauth2_server entities.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {

  // There are scopes to be saved.
  if (!empty($entity->scopes)) {
    $existing_scopes = array();

    // Gather scope names, load all existing scopes, rekey them by name.
    $scope_names = array();
    foreach ($entity->scopes as $scope_values) {
      $scope_names[] = $scope_values['name'];
    }
    $scopes = oauth2_server_scope_load_multiple($entity->name, $scope_names);
    foreach ($scopes as $scope) {
      $existing_scopes[$scope->name] = $scope;
    }

    // Handle the insert / update.
    foreach ($entity->scopes as $scope_values) {
      $scope = entity_create('oauth2_server_scope', $scope_values);
      $scope->server = $entity->name;

      // A scope with the same name exists, steal its id, do an update.
      if (isset($existing_scopes[$scope->name])) {
        $existing_scope = $existing_scopes[$scope->name];
        $scope->scope_id = $existing_scope->scope_id;
        unset($scope->is_new);
      }
      $scope
        ->save();
    }
  }
  return parent::save($entity, $transaction);
}