ff04f05843783cd230356f77edbddd9c47cf47a6
[yaffs-website] / vendor / drupal / console / src / Command / Create / CommentsCommand.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Command\Create\CommentsCommand.
5  */
6
7 namespace Drupal\Console\Command\Create;
8
9 use Symfony\Component\Console\Input\InputOption;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Command\Command;
13 use Drupal\Console\Core\Command\Shared\CommandTrait;
14 use Drupal\Console\Command\Shared\CreateTrait;
15 use Drupal\Console\Utils\Create\CommentData;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 /**
19  * Class CommentsCommand
20  *
21  * @package Drupal\Console\Command\Generate
22  */
23 class CommentsCommand extends Command
24 {
25     use CreateTrait;
26     use CommandTrait;
27
28     /**
29      * @var CommentData
30      */
31     protected $createCommentData;
32
33     /**
34      * CommentsCommand constructor.
35      *
36      * @param CommentData $createCommentData
37      */
38     public function __construct(CommentData $createCommentData)
39     {
40         $this->createCommentData = $createCommentData;
41         parent::__construct();
42     }
43
44     /**
45      * {@inheritdoc}
46      */
47     protected function configure()
48     {
49         $this
50             ->setName('create:comments')
51             ->setDescription($this->trans('commands.create.comments.description'))
52             ->addArgument(
53                 'node-id',
54                 InputOption::VALUE_REQUIRED,
55                 $this->trans('commands.create.comments.arguments.node-id'),
56                 null
57             )
58             ->addOption(
59                 'limit',
60                 null,
61                 InputOption::VALUE_OPTIONAL,
62                 $this->trans('commands.create.comments.options.limit')
63             )
64             ->addOption(
65                 'title-words',
66                 null,
67                 InputOption::VALUE_OPTIONAL,
68                 $this->trans('commands.create.comments.options.title-words')
69             )
70             ->addOption(
71                 'time-range',
72                 null,
73                 InputOption::VALUE_OPTIONAL,
74                 $this->trans('commands.create.comments.options.time-range')
75             );
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     protected function interact(InputInterface $input, OutputInterface $output)
82     {
83         $io = new DrupalStyle($input, $output);
84
85         $nodeId  = $input->getArgument('node-id');
86         if (!$nodeId) {
87             $nodeId = $io->ask(
88                 $this->trans('commands.create.comments.questions.node-id')
89             );
90             $input->setArgument('node-id', $nodeId);
91         }
92
93         $limit = $input->getOption('limit');
94         if (!$limit) {
95             $limit = $io->ask(
96                 $this->trans('commands.create.comments.questions.limit'),
97                 25
98             );
99             $input->setOption('limit', $limit);
100         }
101
102         $titleWords = $input->getOption('title-words');
103         if (!$titleWords) {
104             $titleWords = $io->ask(
105                 $this->trans('commands.create.comments.questions.title-words'),
106                 5
107             );
108
109             $input->setOption('title-words', $titleWords);
110         }
111
112         $timeRange = $input->getOption('time-range');
113         if (!$timeRange) {
114             $timeRanges = $this->getTimeRange();
115
116             $timeRange = $io->choice(
117                 $this->trans('commands.create.comments.questions.time-range'),
118                 array_values($timeRanges)
119             );
120
121             $input->setOption('time-range', array_search($timeRange, $timeRanges));
122         }
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     protected function execute(InputInterface $input, OutputInterface $output)
129     {
130         $io = new DrupalStyle($input, $output);
131
132         $nodeId = $input->getArgument('node-id')?:1;
133         $limit = $input->getOption('limit')?:25;
134         $titleWords = $input->getOption('title-words')?:5;
135         $timeRange = $input->getOption('time-range')?:31536000;
136
137         $comments = $this->createCommentData->create(
138             $nodeId,
139             $limit,
140             $titleWords,
141             $timeRange
142         );
143
144         $tableHeader = [
145             $this->trans('commands.create.comments.messages.node-id'),
146             $this->trans('commands.create.comments.messages.comment-id'),
147             $this->trans('commands.create.comments.messages.title'),
148             $this->trans('commands.create.comments.messages.created'),
149         ];
150
151         $io->table($tableHeader, $comments['success']);
152
153         $io->success(
154             sprintf(
155                 $this->trans('commands.create.comments.messages.created-comments'),
156                 $limit
157             )
158         );
159
160         return 0;
161     }
162 }