Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[role:delete] New command #3594

Merged
merged 1 commit into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/services/role.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@ services:
console.role_new:
class: Drupal\Console\Command\Role\NewCommand
arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api', '@console.validator', '@console.string_converter']
tags:
- { name: drupal.command }
console.role_delete:
class: Drupal\Console\Command\Role\DeleteCommand
arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api']
tags:
- { name: drupal.command }
182 changes: 182 additions & 0 deletions src/Command/Role/DeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* @file
* Contains \Drupal\Console\Command\Role\DeleteCommand.
*/

namespace Drupal\Console\Command\Role;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Console\Utils\DrupalApi;
use Drupal\Console\Command\Shared\ConfirmationTrait;
use Drupal\Console\Core\Style\DrupalStyle;

class DeleteCommand extends Command
{
use ConfirmationTrait;

/**
* @var Connection
*/
protected $database;

/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* @var DateFormatterInterface
*/
protected $dateFormatter;

/**
* @var DrupalApi
*/
protected $drupalApi;


/**
* DeleteCommand constructor.
*
* @param Connection $database
* @param EntityTypeManagerInterface $entityTypeManager
* @param DateFormatterInterface $dateFormatter
* @param DrupalApi $drupalApi
*/
public function __construct(
Connection $database,
EntityTypeManagerInterface $entityTypeManager,
DateFormatterInterface $dateFormatter,
DrupalApi $drupalApi
) {
$this->database = $database;
$this->entityTypeManager = $entityTypeManager;
$this->dateFormatter = $dateFormatter;
$this->drupalApi = $drupalApi;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('role:delete')
->setDescription($this->trans('commands.role.delete.description'))
->setHelp($this->trans('commands.role.delete.help'))
->addArgument(
'roles',
InputArgument::IS_ARRAY,
$this->trans('commands.role.delete.argument.roles')
)->setAliases(['rd']);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$roles = $input->getArgument('roles');

$role = $this->deleteRole(
$roles
);

$tableHeader = [
$this->trans('commands.role.delete.messages.role-id'),
$this->trans('commands.role.delete.messages.role-name'),
];

if ($role['success']) {
$io->success(
sprintf(
$this->trans('commands.role.delete.messages.role-created')
)
);

$io->table($tableHeader, $role['success']);

return 0;
}

if ($role['error']) {
$io->error($role['error']['error']);

return 1;
}
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$rolename = $input->getArgument('roles');
if (!$rolename) {
$roles_collection = [];
$roles = array_keys($this->drupalApi->getRoles());
$io->writeln($this->trans('commands.common.questions.roles.message'));
while (true) {
$role = $io->choiceNoList(
$this->trans('commands.common.questions.roles.name'),
$roles,
null,
true
);
$role = trim($role);
if (empty($role)) {
break;
}

array_push($roles_collection, $role);
$role_key = array_search($role, $roles, true);
if ($role_key >= 0) {
unset($roles[$role_key]);
}
}

$input->setArgument('roles', $roles_collection);
}
}

/**
* Remove and returns an array of deleted roles
*
* @param $roles
*
* @return $array
*/
private function deleteRole($roles)
{
$result = [];
try {
foreach ($roles as $value) {
$role = $this->entityTypeManager->getStorage('user_role')->load($value);
$this->entityTypeManager->getStorage('user_role')->delete([$role]);

$result['success'][] = [
'role-id' => $value,
'role-name' => $value
];
}
} catch (\Exception $e) {
$result['error'] = [
'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()
];
}

return $result;
}
}
8 changes: 8 additions & 0 deletions src/Command/Role/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ function ($machine_name) {
}
}

/**
* Create and returns an array of new role
*
* @param $rolename
* @param $machine_name
*
* @return $array
*/
private function createRole($rolename, $machine_name)
{
$role = Role::create(
Expand Down
3 changes: 0 additions & 3 deletions src/Utils/Create/RoleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public function __construct(
/**
* Create and returns an array of new Roles.
*
* @param $roles
* @param $limit
* @param $password
* @param $timeRange
*
* @return array
*/
Expand Down