Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Create / UsersCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Create\UsersCommand.
6  */
7
8 namespace Drupal\Console\Command\Create;
9
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;
20
21 /**
22  * Class UsersCommand
23  *
24  * @package Drupal\Console\Command\Create
25  */
26 class UsersCommand extends Command
27 {
28     use CreateTrait;
29     use CommandTrait;
30
31     /**
32      * @var DrupalApi
33      */
34     protected $drupalApi;
35     /**
36      * @var UserData
37      */
38     protected $createUserData;
39
40     /**
41      * UsersCommand constructor.
42      *
43      * @param DrupalApi $drupalApi
44      * @param UserData  $createUserData
45      */
46     public function __construct(
47         DrupalApi $drupalApi,
48         UserData $createUserData
49     ) {
50         $this->drupalApi = $drupalApi;
51         $this->createUserData = $createUserData;
52         parent::__construct();
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     protected function configure()
59     {
60         $this
61             ->setName('create:users')
62             ->setDescription($this->trans('commands.create.users.description'))
63             ->addArgument(
64                 'roles',
65                 InputArgument::IS_ARRAY,
66                 $this->trans('commands.create.users.arguments.roles')
67             )
68             ->addOption(
69                 'limit',
70                 null,
71                 InputOption::VALUE_OPTIONAL,
72                 $this->trans('commands.create.users.options.limit')
73             )
74             ->addOption(
75                 'password',
76                 null,
77                 InputOption::VALUE_OPTIONAL,
78                 $this->trans('commands.create.users.options.password')
79             )
80             ->addOption(
81                 'time-range',
82                 null,
83                 InputOption::VALUE_OPTIONAL,
84                 $this->trans('commands.create.users.options.time-range')
85             );
86     }
87
88     /**
89      * {@inheritdoc}
90      */
91     protected function interact(InputInterface $input, OutputInterface $output)
92     {
93         $io = new DrupalStyle($input, $output);
94
95         $rids = $input->getArgument('roles');
96         if (!$rids) {
97             $roles = $this->drupalApi->getRoles();
98             $rids = $io->choice(
99                 $this->trans('commands.create.users.questions.roles'),
100                 array_values($roles),
101                 null,
102                 true
103             );
104
105             $rids = array_map(
106                 function ($role) use ($roles) {
107                     return array_search($role, $roles);
108                 },
109                 $rids
110             );
111
112             $input->setArgument('roles', $rids);
113         }
114
115         $limit = $input->getOption('limit');
116         if (!$limit) {
117             $limit = $io->ask(
118                 $this->trans('commands.create.users.questions.limit'),
119                 10
120             );
121             $input->setOption('limit', $limit);
122         }
123
124         $password = $input->getOption('password');
125         if (!$password) {
126             $password = $io->ask(
127                 $this->trans('commands.create.users.questions.password'),
128                 5
129             );
130
131             $input->setOption('password', $password);
132         }
133
134         $timeRange = $input->getOption('time-range');
135         if (!$timeRange) {
136             $timeRanges = $this->getTimeRange();
137
138             $timeRange = $io->choice(
139                 $this->trans('commands.create.nodes.questions.time-range'),
140                 array_values($timeRanges)
141             );
142
143             $input->setOption('time-range', array_search($timeRange, $timeRanges));
144         }
145     }
146
147     /**
148      * {@inheritdoc}
149      */
150     protected function execute(InputInterface $input, OutputInterface $output)
151     {
152         $io = new DrupalStyle($input, $output);
153
154         $roles = $input->getArgument('roles');
155         $limit = $input->getOption('limit')?:25;
156         $password = $input->getOption('password');
157         $timeRange = $input->getOption('time-range')?:31536000;
158
159         if (!$roles) {
160             $roles = $this->drupalApi->getRoles();
161         }
162
163         $users = $this->createUserData->create(
164             $roles,
165             $limit,
166             $password,
167             $timeRange
168         );
169
170         $tableHeader = [
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'),
175         ];
176
177         if ($users['success']) {
178             $io->table($tableHeader, $users['success']);
179
180             $io->success(
181                 sprintf(
182                     $this->trans('commands.create.users.messages.created-users'),
183                     $limit
184                 )
185             );
186         }
187
188         return 0;
189     }
190 }