f749201c3dae3decb3d83541832b9d61e153ee0c
[yaffs-website] / vendor / drupal / console / src / Command / Create / RolesCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Create\RolesCommand.
6  */
7
8 namespace Drupal\Console\Command\Create;
9
10 use Drupal\Console\Utils\Create\RoleData;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Core\Command\Command;
15
16 /**
17  * Class RolesCommand
18  *
19  * @package Drupal\Console\Command\Create
20  */
21 class RolesCommand extends Command
22 {
23     /**
24      * @var RoleData
25      */
26     protected $createRoleData;
27
28     /**
29      * RolesCommand constructor.
30      *
31      * @param RoleData $createRoleData
32      */
33     public function __construct(
34         RoleData $createRoleData
35     ) {
36         $this->createRoleData = $createRoleData;
37         parent::__construct();
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function configure()
44     {
45         $this
46             ->setName('create:roles')
47             ->setDescription($this->trans('commands.create.roles.description'))
48             ->addOption(
49                 'limit',
50                 null,
51                 InputOption::VALUE_OPTIONAL,
52                 $this->trans('commands.create.roles.options.limit')
53             )
54             ->setAliases(['crr']);
55     }
56
57     /**
58      * {@inheritdoc}
59      */
60     protected function interact(InputInterface $input, OutputInterface $output)
61     {
62         $limit = $input->getOption('limit');
63         if (!$limit) {
64             $limit = $this->getIo()->ask(
65                 $this->trans('commands.create.roles.questions.limit'),
66                 5
67             );
68             $input->setOption('limit', $limit);
69         }
70     }
71
72     /**
73      * {@inheritdoc}
74      */
75     protected function execute(InputInterface $input, OutputInterface $output)
76     {
77         $limit = $input->getOption('limit')?:5;
78
79         $result = $this->createRoleData->create(
80             $limit
81         );
82
83         $tableHeader = [
84             $this->trans('commands.create.roles.messages.role-id'),
85             $this->trans('commands.create.roles.messages.role-name'),
86         ];
87
88         if ($result['success']) {
89             $this->getIo()->table($tableHeader, $result['success']);
90
91             $this->getIo()->success(
92                 sprintf(
93                     $this->trans('commands.create.roles.messages.created-roles'),
94                     count($result['success'])
95                 )
96             );
97         }
98
99         if (isset($result['error'])) {
100             foreach ($result['error'] as $error) {
101                 $this->getIo()->error(
102                     sprintf(
103                         $this->trans('commands.create.roles.messages.error'),
104                         $error
105                     )
106                 );
107             }
108         }
109
110         return 0;
111     }
112 }