Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Create / CommentsCommand.php
index ff04f05843783cd230356f77edbddd9c47cf47a6..656d3969f461d0fa07298a3db186aa4283e5df1d 100644 (file)
@@ -6,24 +6,28 @@
 
 namespace Drupal\Console\Command\Create;
 
-use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Command\Command;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
+use Drupal\Console\Annotations\DrupalCommand;
 use Drupal\Console\Command\Shared\CreateTrait;
+use Drupal\Console\Core\Command\Command;
 use Drupal\Console\Utils\Create\CommentData;
-use Drupal\Console\Core\Style\DrupalStyle;
+use Drupal\node\Entity\Node;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
 
 /**
  * Class CommentsCommand
  *
  * @package Drupal\Console\Command\Generate
+ *
+ * @DrupalCommand(
+ *     extension = "comment",
+ *     extensionType = "module"
+ * )
  */
 class CommentsCommand extends Command
 {
     use CreateTrait;
-    use CommandTrait;
 
     /**
      * @var CommentData
@@ -72,7 +76,7 @@ class CommentsCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL,
                 $this->trans('commands.create.comments.options.time-range')
-            );
+            )->setAliases(['crc']);
     }
 
     /**
@@ -80,11 +84,9 @@ class CommentsCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $nodeId  = $input->getArgument('node-id');
         if (!$nodeId) {
-            $nodeId = $io->ask(
+            $nodeId = $this->getIo()->ask(
                 $this->trans('commands.create.comments.questions.node-id')
             );
             $input->setArgument('node-id', $nodeId);
@@ -92,7 +94,7 @@ class CommentsCommand extends Command
 
         $limit = $input->getOption('limit');
         if (!$limit) {
-            $limit = $io->ask(
+            $limit = $this->getIo()->ask(
                 $this->trans('commands.create.comments.questions.limit'),
                 25
             );
@@ -101,7 +103,7 @@ class CommentsCommand extends Command
 
         $titleWords = $input->getOption('title-words');
         if (!$titleWords) {
-            $titleWords = $io->ask(
+            $titleWords = $this->getIo()->ask(
                 $this->trans('commands.create.comments.questions.title-words'),
                 5
             );
@@ -113,7 +115,7 @@ class CommentsCommand extends Command
         if (!$timeRange) {
             $timeRanges = $this->getTimeRange();
 
-            $timeRange = $io->choice(
+            $timeRange = $this->getIo()->choice(
                 $this->trans('commands.create.comments.questions.time-range'),
                 array_values($timeRanges)
             );
@@ -127,35 +129,55 @@ class CommentsCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $nodeId = $input->getArgument('node-id')?:1;
+        $node = \Drupal\node\Entity\Node::load($nodeId);
+        if (empty($node)) {
+            throw new \InvalidArgumentException(
+                $this->trans(
+                    'commands.generate.controller.messages.node-id-invalid'
+                )
+            );
+        }
         $limit = $input->getOption('limit')?:25;
         $titleWords = $input->getOption('title-words')?:5;
         $timeRange = $input->getOption('time-range')?:31536000;
 
-        $comments = $this->createCommentData->create(
+        $result = $this->createCommentData->create(
             $nodeId,
             $limit,
             $titleWords,
             $timeRange
         );
 
-        $tableHeader = [
-            $this->trans('commands.create.comments.messages.node-id'),
-            $this->trans('commands.create.comments.messages.comment-id'),
-            $this->trans('commands.create.comments.messages.title'),
-            $this->trans('commands.create.comments.messages.created'),
-        ];
+        if ($result['success']) {
 
-        $io->table($tableHeader, $comments['success']);
+            $tableHeader = [
+                $this->trans('commands.create.comments.messages.node-id'),
+                $this->trans('commands.create.comments.messages.comment-id'),
+                $this->trans('commands.create.comments.messages.title'),
+                $this->trans('commands.create.comments.messages.created'),
+            ];
 
-        $io->success(
-            sprintf(
-                $this->trans('commands.create.comments.messages.created-comments'),
-                $limit
-            )
-        );
+            $this->getIo()->table($tableHeader, $result['success']);
+
+            $this->getIo()->success(
+                sprintf(
+                    $this->trans('commands.create.comments.messages.created-comments'),
+                    count($result['success'])
+                )
+            );
+        }
+
+        if (isset($result['error'])) {
+            foreach ($result['error'] as $error) {
+                $this->getIo()->error(
+                    sprintf(
+                        $this->trans('commands.create.comments.messages.error'),
+                        $error
+                    )
+                );
+            }
+        }
 
         return 0;
     }