d1b45d633f5c7b6cf9d871fd42479336b4f51453
[yaffs-website] / vendor / drupal / console / src / Command / Role / DeleteCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Role\DeleteCommand.
5  */
6
7 namespace Drupal\Console\Command\Role;
8
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Drupal\Console\Core\Command\Command;
13 use Drupal\Core\Database\Connection;
14 use Drupal\Core\Entity\EntityTypeManagerInterface;
15 use Drupal\Core\Datetime\DateFormatterInterface;
16 use Drupal\Console\Utils\DrupalApi;
17 use Drupal\Console\Utils\Validator;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
19
20 class DeleteCommand extends Command
21 {
22     use ConfirmationTrait;
23
24     /**
25      * @var Connection
26      */
27     protected $database;
28
29     /**
30      * @var EntityTypeManagerInterface
31      */
32     protected $entityTypeManager;
33
34     /**
35      * @var DateFormatterInterface
36      */
37     protected $dateFormatter;
38
39     /**
40      * @var DrupalApi
41      */
42     protected $drupalApi;
43
44     /**
45      * @var Validator
46      */
47     protected $validator;
48
49     /**
50      * DeleteCommand constructor.
51      *
52      * @param Connection                 $database
53      * @param EntityTypeManagerInterface $entityTypeManager
54      * @param DateFormatterInterface     $dateFormatter
55      * @param DrupalApi                  $drupalApi
56      * @param Validator                  $validator
57      */
58     public function __construct(
59         Connection $database,
60         EntityTypeManagerInterface $entityTypeManager,
61         DateFormatterInterface $dateFormatter,
62         DrupalApi $drupalApi,
63         Validator $validator
64     ) {
65         $this->database = $database;
66         $this->entityTypeManager = $entityTypeManager;
67         $this->dateFormatter = $dateFormatter;
68         $this->drupalApi = $drupalApi;
69         $this->validator = $validator;
70         parent::__construct();
71     }
72
73     /**
74      * {@inheritdoc}
75      */
76     protected function configure()
77     {
78         $this
79             ->setName('role:delete')
80             ->setDescription($this->trans('commands.role.delete.description'))
81             ->setHelp($this->trans('commands.role.delete.help'))
82             ->addArgument(
83                 'roles',
84                 InputArgument::IS_ARRAY,
85                 $this->trans('commands.role.delete.argument.roles')
86             )->setAliases(['rd']);
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     protected function execute(InputInterface $input, OutputInterface $output)
93     {
94         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmOperation
95         if (!$this->confirmOperation()) {
96             return 1;
97         }
98
99         $roles = $input->getArgument('roles');
100         foreach ($roles as $roleItem) {
101             $this->validator->validateRoleExistence($roleItem, $this->drupalApi->getRoles());
102         }
103
104         $role = $this->deleteRole($roles);
105
106         $tableHeader = [
107             $this->trans('commands.role.delete.messages.role-id'),
108             $this->trans('commands.role.delete.messages.role-name'),
109         ];
110
111         if ($role['success']) {
112             $this->getIo()->success(
113                 sprintf(
114                     $this->trans('commands.role.delete.messages.role-created')
115                 )
116             );
117
118             $this->getIo()->table($tableHeader, $role['success']);
119
120             return 0;
121         }
122
123         if ($role['error']) {
124             $this->getIo()->error($role['error']['error']);
125
126             return 1;
127         }
128     }
129
130     /**
131      * {@inheritdoc}
132      */
133     protected function interact(InputInterface $input, OutputInterface $output)
134     {
135         $rolename = $input->getArgument('roles');
136         if (!$rolename) {
137             $roles_collection = [];
138             $siteRoles = $this->drupalApi->getRoles();
139             $roles = array_keys($siteRoles);
140             $this->getIo()->writeln($this->trans('commands.common.questions.roles.message'));
141             while (true) {
142                 $role = $this->getIo()->choiceNoList(
143                     $this->trans('commands.common.questions.roles.name'),
144                     $roles,
145                     '',
146                     true
147                 );
148                 $role = trim($role);
149                 if (empty($role) || is_numeric($role)) {
150                     break;
151                 }
152
153                 if (!array_key_exists($role, $siteRoles)) {
154                     $this->getIo()->error(sprintf(
155                         $this->trans('commands.role.delete.messages.invalid-machine-name'),
156                         $role
157                     ));
158                     continue;
159                 }
160
161                 array_push($roles_collection, $role);
162                 $role_key = array_search($role, $roles, true);
163                 if ($role_key >= 0) {
164                     unset($roles[$role_key]);
165                 }
166             }
167
168             $input->setArgument('roles', $roles_collection);
169         }
170     }
171
172     /**
173      * Remove and returns an array of deleted roles
174      *
175      * @param $roles
176      *
177      * @return $array
178      */
179     private function deleteRole($roles)
180     {
181         $result = [];
182         try {
183             foreach ($roles as $value) {
184                 $role = $this->entityTypeManager->getStorage('user_role')->load($value);
185                 $this->entityTypeManager->getStorage('user_role')->delete([$role]);
186
187                 $result['success'][] = [
188                     'role-id' => $value,
189                     'role-name' => $value
190                 ];
191             }
192         } catch (\Exception $e) {
193             $result['error'] = [
194                 'error' => 'Error: ' . get_class($e) . ', code: ' . $e->getCode() . ', message: ' . $e->getMessage()
195             ];
196         }
197
198         return $result;
199     }
200 }