5 * Contains \Drupal\Console\Command\Create\UsersCommand.
8 namespace Drupal\Console\Command\Create;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\CreateTrait;
17 use Drupal\Console\Utils\Create\UserData;
18 use Drupal\Console\Utils\DrupalApi;
19 use Drupal\Console\Core\Style\DrupalStyle;
24 * @package Drupal\Console\Command\Create
26 class UsersCommand extends Command
38 protected $createUserData;
41 * UsersCommand constructor.
43 * @param DrupalApi $drupalApi
44 * @param UserData $createUserData
46 public function __construct(
48 UserData $createUserData
50 $this->drupalApi = $drupalApi;
51 $this->createUserData = $createUserData;
52 parent::__construct();
58 protected function configure()
61 ->setName('create:users')
62 ->setDescription($this->trans('commands.create.users.description'))
65 InputArgument::IS_ARRAY,
66 $this->trans('commands.create.users.arguments.roles')
71 InputOption::VALUE_OPTIONAL,
72 $this->trans('commands.create.users.options.limit')
77 InputOption::VALUE_OPTIONAL,
78 $this->trans('commands.create.users.options.password')
83 InputOption::VALUE_OPTIONAL,
84 $this->trans('commands.create.users.options.time-range')
91 protected function interact(InputInterface $input, OutputInterface $output)
93 $io = new DrupalStyle($input, $output);
95 $rids = $input->getArgument('roles');
97 $roles = $this->drupalApi->getRoles();
99 $this->trans('commands.create.users.questions.roles'),
100 array_values($roles),
106 function ($role) use ($roles) {
107 return array_search($role, $roles);
112 $input->setArgument('roles', $rids);
115 $limit = $input->getOption('limit');
118 $this->trans('commands.create.users.questions.limit'),
121 $input->setOption('limit', $limit);
124 $password = $input->getOption('password');
126 $password = $io->ask(
127 $this->trans('commands.create.users.questions.password'),
131 $input->setOption('password', $password);
134 $timeRange = $input->getOption('time-range');
136 $timeRanges = $this->getTimeRange();
138 $timeRange = $io->choice(
139 $this->trans('commands.create.nodes.questions.time-range'),
140 array_values($timeRanges)
143 $input->setOption('time-range', array_search($timeRange, $timeRanges));
150 protected function execute(InputInterface $input, OutputInterface $output)
152 $io = new DrupalStyle($input, $output);
154 $roles = $input->getArgument('roles');
155 $limit = $input->getOption('limit')?:25;
156 $password = $input->getOption('password');
157 $timeRange = $input->getOption('time-range')?:31536000;
160 $roles = $this->drupalApi->getRoles();
163 $users = $this->createUserData->create(
171 $this->trans('commands.create.users.messages.user-id'),
172 $this->trans('commands.create.users.messages.username'),
173 $this->trans('commands.create.users.messages.roles'),
174 $this->trans('commands.create.users.messages.created'),
177 if ($users['success']) {
178 $io->table($tableHeader, $users['success']);
182 $this->trans('commands.create.users.messages.created-users'),