656d3969f461d0fa07298a3db186aa4283e5df1d
[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 Drupal\Console\Annotations\DrupalCommand;
10 use Drupal\Console\Command\Shared\CreateTrait;
11 use Drupal\Console\Core\Command\Command;
12 use Drupal\Console\Utils\Create\CommentData;
13 use Drupal\node\Entity\Node;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17
18 /**
19  * Class CommentsCommand
20  *
21  * @package Drupal\Console\Command\Generate
22  *
23  * @DrupalCommand(
24  *     extension = "comment",
25  *     extensionType = "module"
26  * )
27  */
28 class CommentsCommand extends Command
29 {
30     use CreateTrait;
31
32     /**
33      * @var CommentData
34      */
35     protected $createCommentData;
36
37     /**
38      * CommentsCommand constructor.
39      *
40      * @param CommentData $createCommentData
41      */
42     public function __construct(CommentData $createCommentData)
43     {
44         $this->createCommentData = $createCommentData;
45         parent::__construct();
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     protected function configure()
52     {
53         $this
54             ->setName('create:comments')
55             ->setDescription($this->trans('commands.create.comments.description'))
56             ->addArgument(
57                 'node-id',
58                 InputOption::VALUE_REQUIRED,
59                 $this->trans('commands.create.comments.arguments.node-id'),
60                 null
61             )
62             ->addOption(
63                 'limit',
64                 null,
65                 InputOption::VALUE_OPTIONAL,
66                 $this->trans('commands.create.comments.options.limit')
67             )
68             ->addOption(
69                 'title-words',
70                 null,
71                 InputOption::VALUE_OPTIONAL,
72                 $this->trans('commands.create.comments.options.title-words')
73             )
74             ->addOption(
75                 'time-range',
76                 null,
77                 InputOption::VALUE_OPTIONAL,
78                 $this->trans('commands.create.comments.options.time-range')
79             )->setAliases(['crc']);
80     }
81
82     /**
83      * {@inheritdoc}
84      */
85     protected function interact(InputInterface $input, OutputInterface $output)
86     {
87         $nodeId  = $input->getArgument('node-id');
88         if (!$nodeId) {
89             $nodeId = $this->getIo()->ask(
90                 $this->trans('commands.create.comments.questions.node-id')
91             );
92             $input->setArgument('node-id', $nodeId);
93         }
94
95         $limit = $input->getOption('limit');
96         if (!$limit) {
97             $limit = $this->getIo()->ask(
98                 $this->trans('commands.create.comments.questions.limit'),
99                 25
100             );
101             $input->setOption('limit', $limit);
102         }
103
104         $titleWords = $input->getOption('title-words');
105         if (!$titleWords) {
106             $titleWords = $this->getIo()->ask(
107                 $this->trans('commands.create.comments.questions.title-words'),
108                 5
109             );
110
111             $input->setOption('title-words', $titleWords);
112         }
113
114         $timeRange = $input->getOption('time-range');
115         if (!$timeRange) {
116             $timeRanges = $this->getTimeRange();
117
118             $timeRange = $this->getIo()->choice(
119                 $this->trans('commands.create.comments.questions.time-range'),
120                 array_values($timeRanges)
121             );
122
123             $input->setOption('time-range', array_search($timeRange, $timeRanges));
124         }
125     }
126
127     /**
128      * {@inheritdoc}
129      */
130     protected function execute(InputInterface $input, OutputInterface $output)
131     {
132         $nodeId = $input->getArgument('node-id')?:1;
133         $node = \Drupal\node\Entity\Node::load($nodeId);
134         if (empty($node)) {
135             throw new \InvalidArgumentException(
136                 $this->trans(
137                     'commands.generate.controller.messages.node-id-invalid'
138                 )
139             );
140         }
141         $limit = $input->getOption('limit')?:25;
142         $titleWords = $input->getOption('title-words')?:5;
143         $timeRange = $input->getOption('time-range')?:31536000;
144
145         $result = $this->createCommentData->create(
146             $nodeId,
147             $limit,
148             $titleWords,
149             $timeRange
150         );
151
152         if ($result['success']) {
153
154             $tableHeader = [
155                 $this->trans('commands.create.comments.messages.node-id'),
156                 $this->trans('commands.create.comments.messages.comment-id'),
157                 $this->trans('commands.create.comments.messages.title'),
158                 $this->trans('commands.create.comments.messages.created'),
159             ];
160
161             $this->getIo()->table($tableHeader, $result['success']);
162
163             $this->getIo()->success(
164                 sprintf(
165                     $this->trans('commands.create.comments.messages.created-comments'),
166                     count($result['success'])
167                 )
168             );
169         }
170
171         if (isset($result['error'])) {
172             foreach ($result['error'] as $error) {
173                 $this->getIo()->error(
174                     sprintf(
175                         $this->trans('commands.create.comments.messages.error'),
176                         $error
177                     )
178                 );
179             }
180         }
181
182         return 0;
183     }
184 }