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