You are here

public function KeyCommands::delete in Key 8

Delete a key.

@command key:delete

@aliases key-delete @format table

Parameters

string $id: The ID (machine name) of the key to delete.

array $options: Options array.

File

src/Commands/KeyCommands.php, line 201

Class

KeyCommands
Class KeyCommands.

Namespace

Drupal\key\Commands

Code

public function delete($id, array $options = []) {

  // Look for a key with the specified ID. If one does not exist, set an
  // error and abort.

  /* @var $key \Drupal\key\Entity\Key */
  $key = $this->repository
    ->getKey($id);
  if (!$key) {
    throw new \Exception(dt('Key !id does not exist.', [
      '!id' => $id,
    ]));
  }

  // Confirm that the key should be deleted.
  $this->logger
    ->warning('Be extremely careful when deleting a key! It may result in losing access to a service or making encrypted data unreadable.');
  $this->output
    ->writeln(dt('The following key will be deleted: !id', [
    '!id' => $id,
  ]));
  if (!$this
    ->io()
    ->confirm(dt('Do you want to continue?'))) {

    // Removing drush_user_abort(), no current implementation of that.
    return;
  }

  // Delete the key.
  $key
    ->delete();

  // Try to load the key to confirm that it was deleted.
  $key_check = $this->repository
    ->getKey($id);

  // If the key still exists, set an error and abort.
  if ($key_check) {
    throw new \Exception(dt('Key !id was not deleted.', [
      '!id' => $id,
    ]));
  }
  $this->logger
    ->info('Key !id was deleted successfully.', [
    '!id' => $id,
  ]);
}