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 Drupal\Console\Core\Command\Command;
15 use Drupal\Console\Command\Shared\CreateTrait;
16 use Drupal\Console\Utils\Create\UserData;
17 use Drupal\Console\Utils\DrupalApi;
22 * @package Drupal\Console\Command\Create
24 class UsersCommand extends Command
35 protected $createUserData;
38 * UsersCommand constructor.
40 * @param DrupalApi $drupalApi
41 * @param UserData $createUserData
43 public function __construct(
45 UserData $createUserData
47 $this->drupalApi = $drupalApi;
48 $this->createUserData = $createUserData;
49 parent::__construct();
55 protected function configure()
58 ->setName('create:users')
59 ->setDescription($this->trans('commands.create.users.description'))
62 InputArgument::IS_ARRAY,
63 $this->trans('commands.create.users.arguments.roles')
68 InputOption::VALUE_OPTIONAL,
69 $this->trans('commands.create.users.options.limit')
74 InputOption::VALUE_OPTIONAL,
75 $this->trans('commands.create.users.options.password')
80 InputOption::VALUE_OPTIONAL,
81 $this->trans('commands.create.users.options.time-range')
82 )->setAliases(['cru']);
88 protected function interact(InputInterface $input, OutputInterface $output)
90 $rids = $input->getArgument('roles');
92 $roles = $this->drupalApi->getRoles();
93 $rids = $this->getIo()->choice(
94 $this->trans('commands.create.users.questions.roles'),
101 function ($role) use ($roles) {
102 return array_search($role, $roles);
107 $input->setArgument('roles', $rids);
110 $limit = $input->getOption('limit');
112 $limit = $this->getIo()->ask(
113 $this->trans('commands.create.users.questions.limit'),
116 $input->setOption('limit', $limit);
119 $password = $input->getOption('password');
121 $password = $this->getIo()->ask(
122 $this->trans('commands.create.users.questions.password'),
126 $input->setOption('password', $password);
129 $timeRange = $input->getOption('time-range');
131 $timeRanges = $this->getTimeRange();
133 $timeRange = $this->getIo()->choice(
134 $this->trans('commands.create.nodes.questions.time-range'),
135 array_values($timeRanges)
138 $input->setOption('time-range', array_search($timeRange, $timeRanges));
145 protected function execute(InputInterface $input, OutputInterface $output)
147 $roles = $input->getArgument('roles');
148 $limit = $input->getOption('limit')?:25;
149 $password = $input->getOption('password');
150 $timeRange = $input->getOption('time-range')?:31536000;
153 $roles = $this->drupalApi->getRoles();
156 $result = $this->createUserData->create(
164 $this->trans('commands.create.users.messages.user-id'),
165 $this->trans('commands.create.users.messages.username'),
166 $this->trans('commands.create.users.messages.roles'),
167 $this->trans('commands.create.users.messages.created'),
170 if ($result['success']) {
171 $this->getIo()->table($tableHeader, $result['success']);
173 $this->getIo()->success(
175 $this->trans('commands.create.users.messages.created-users'),
176 count($result['success'])
181 if (isset($result['error'])) {
182 foreach ($result['error'] as $error) {
183 $this->getIo()->error(
185 $this->trans('commands.create.users.messages.error'),