Skip to content

Commit

Permalink
Add validation of node id. Fix code structure. Replace the wrong meth…
Browse files Browse the repository at this point in the history
…od getTitle with getSubject. Order use statements alphabetically (#3669)
  • Loading branch information
LOBsTerr authored and jmolivas committed Jan 10, 2018
1 parent 8d28be2 commit 40c3bc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/Command/Create/CommentsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

namespace Drupal\Console\Command\Create;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\Console\Command\Shared\CreateTrait;
use Drupal\Console\Utils\Create\CommentData;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Utils\Create\CommentData;
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
Expand Down Expand Up @@ -134,6 +135,14 @@ 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;
Expand Down
26 changes: 13 additions & 13 deletions src/Utils/Create/CommentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public function create(
) {
$comments = [];

for ($i=0; $i<$limit; $i++) {
for ($i = 0; $i < $limit; $i++) {
$comment = $this->entityTypeManager->getStorage('comment')->create(
[
'entity_id' => $nid,
'entity_type' => 'node',
'field_name' => 'comment',
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
'uid' => $this->getUserID(),
'status' => true,
'subject' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
'language' => 'und',
'comment_body' => ['und' => ['random body']],
'entity_id' => $nid,
'entity_type' => 'node',
'field_name' => 'comment',
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
'uid' => $this->getUserID(),
'status' => true,
'subject' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
'language' => 'und',
'comment_body' => ['und' => ['random body']],
]
);

Expand All @@ -61,12 +61,12 @@ public function create(
$comment->getCreatedTime(),
'custom',
'Y-m-d h:i:s'
)
),
];
} catch (\Exception $error) {
$comments['error'][] = [
'title' => $comment->getTitle(),
'error' => $error->getMessage()
'title' => $comment->getSubject(),
'error' => $error->getMessage(),
];
}
}
Expand Down

0 comments on commit 40c3bc7

Please sign in to comment.