Yaffs site version 1.1
[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 Drupal\Console\Core\Command\Shared\CommandTrait;
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\Utils\Create\VocabularyData;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class VocabulariesCommand
20  *
21  * @package Drupal\Console\Command\Generate
22  */
23 class VocabulariesCommand extends Command
24 {
25     use CommandTrait;
26
27     /**
28      * @var VocabularyData
29      */
30     protected $vocabularyData;
31
32     /**
33      * UsersCommand constructor.
34      *
35      * @param $vocabularyData
36      */
37     public function __construct(VocabularyData $vocabularyData)
38     {
39         $this->vocabularyData = $vocabularyData;
40         parent::__construct();
41     }
42
43     /**
44      * {@inheritdoc}
45      */
46     protected function configure()
47     {
48         $this
49             ->setName('create:vocabularies')
50             ->setDescription($this->trans('commands.create.vocabularies.description'))
51             ->addOption(
52                 'limit',
53                 null,
54                 InputOption::VALUE_OPTIONAL,
55                 $this->trans('commands.create.vocabularies.options.limit')
56             )
57             ->addOption(
58                 'name-words',
59                 null,
60                 InputOption::VALUE_OPTIONAL,
61                 $this->trans('commands.create.vocabularies.options.name-words')
62             );
63     }
64
65     /**
66      * {@inheritdoc}
67      */
68     protected function interact(InputInterface $input, OutputInterface $output)
69     {
70         $io = new DrupalStyle($input, $output);
71
72         $limit = $input->getOption('limit');
73         if (!$limit) {
74             $limit = $io->ask(
75                 $this->trans('commands.create.vocabularies.questions.limit'),
76                 25
77             );
78             $input->setOption('limit', $limit);
79         }
80
81         $nameWords = $input->getOption('name-words');
82         if (!$nameWords) {
83             $nameWords = $io->ask(
84                 $this->trans('commands.create.vocabularies.questions.name-words'),
85                 5
86             );
87
88             $input->setOption('name-words', $nameWords);
89         }
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     protected function execute(InputInterface $input, OutputInterface $output)
96     {
97         $io = new DrupalStyle($input, $output);
98
99         $limit = $input->getOption('limit')?:25;
100         $nameWords = $input->getOption('name-words')?:5;
101
102         $vocabularies = $this->vocabularyData->create(
103             $limit,
104             $nameWords
105         );
106
107         $tableHeader = [
108           $this->trans('commands.create.vocabularies.messages.vocabulary-id'),
109           $this->trans('commands.create.vocabularies.messages.name'),
110         ];
111
112         if (isset($vocabularies['success'])) {
113             $io->table($tableHeader, $vocabularies['success']);
114
115             $io->success(
116                 sprintf(
117                     $this->trans('commands.create.vocabularies.messages.created-terms'),
118                     $limit
119                 )
120             );
121         } else {
122             $io->error(
123                 sprintf(
124                     $this->trans('commands.create.vocabularies.messages.error'),
125                     $vocabularies['error'][0]['error']
126                 )
127             );
128         }
129
130         return 0;
131     }
132 }