Skip to content

Commit

Permalink
[create:*] Create content exception. Fix #3779 (#3781)
Browse files Browse the repository at this point in the history
* create:vocabularies - Improve error handling

* create:users - Improve error handling

* create:terms - Improve error handling

* create:roles - Improve error handling

* create:nodes - Improve error handling

* create:comments - Improve error handling
  • Loading branch information
LOBsTerr authored and jmolivas committed Feb 17, 2018
1 parent 3741529 commit 12ef535
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 173 deletions.
42 changes: 28 additions & 14 deletions src/Command/Create/CommentsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,28 +142,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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']) {

$this->getIo()->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'),
];

$this->getIo()->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;
}
Expand Down
49 changes: 30 additions & 19 deletions src/Command/Create/NodesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
$contentTypes = $available_types;
}

$nodes = $this->createNodeData->create(
$result = $this->createNodeData->create(
$contentTypes,
$limit,
$titleWords,
$timeRange,
$language
);

$nodes = is_array($nodes) ? $nodes : [$nodes];

$tableHeader = [
$this->trans('commands.create.nodes.messages.node-id'),
$this->trans('commands.create.nodes.messages.content-type'),
$this->trans('commands.create.nodes.messages.title'),
$this->trans('commands.create.nodes.messages.created'),
];

$this->getIo()->table($tableHeader, $nodes['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.nodes.messages.created-nodes'),
$limit
)
);

if ($result['success']) {
$tableHeader = [
$this->trans('commands.create.nodes.messages.node-id'),
$this->trans('commands.create.nodes.messages.content-type'),
$this->trans('commands.create.nodes.messages.title'),
$this->trans('commands.create.nodes.messages.created'),
];

$this->getIo()->table($tableHeader, $result['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.nodes.messages.created-nodes'),
count($result['success'])
)
);
}

if (isset($result['error'])) {
foreach ($result['error'] as $error) {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.nodes.messages.error'),
$error
)
);
}
}

return 0;
}
Expand Down
19 changes: 15 additions & 4 deletions src/Command/Create/RolesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$limit = $input->getOption('limit')?:5;

$roles = $this->createRoleData->create(
$result = $this->createRoleData->create(
$limit
);

Expand All @@ -85,17 +85,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->trans('commands.create.roles.messages.role-name'),
];

if ($roles['success']) {
$this->getIo()->table($tableHeader, $roles['success']);
if ($result['success']) {
$this->getIo()->table($tableHeader, $result['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.roles.messages.created-roles'),
$limit
count($result['success'])
)
);
}

if (isset($result['error'])) {
foreach ($result['error'] as $error) {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.roles.messages.error'),
$error
)
);
}
}

return 0;
}
}
29 changes: 21 additions & 8 deletions src/Command/Create/TermsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$vocabularies = array_keys($this->drupalApi->getVocabularies());
}

$terms = $this->createTermData->create(
$result = $this->createTermData->create(
$vocabularies,
$limit,
$nameWords
Expand All @@ -149,14 +149,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->trans('commands.create.terms.messages.name'),
];

$this->getIo()->table($tableHeader, $terms['success']);
if ($result['success']) {
$this->getIo()->table($tableHeader, $result['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.terms.messages.created-terms'),
$limit
)
);
$this->getIo()->success(
sprintf(
$this->trans('commands.create.terms.messages.created-terms'),
count($result['success'])
)
);
}

if (isset($result['error'])) {
foreach ($result['error'] as $error) {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.terms.messages.error'),
$error
)
);
}
}

return 0;
}
Expand Down
19 changes: 15 additions & 4 deletions src/Command/Create/UsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$roles = $this->drupalApi->getRoles();
}

$users = $this->createUserData->create(
$result = $this->createUserData->create(
$roles,
$limit,
$password,
Expand All @@ -167,17 +167,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->trans('commands.create.users.messages.created'),
];

if ($users['success']) {
$this->getIo()->table($tableHeader, $users['success']);
if ($result['success']) {
$this->getIo()->table($tableHeader, $result['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.users.messages.created-users'),
$limit
count($result['success'])
)
);
}

if (isset($result['error'])) {
foreach ($result['error'] as $error) {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.users.messages.error'),
$error
)
);
}
}

return 0;
}
}
26 changes: 15 additions & 11 deletions src/Command/Create/VocabulariesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$limit = $input->getOption('limit')?:25;
$nameWords = $input->getOption('name-words')?:5;

$vocabularies = $this->vocabularyData->create(
$result = $this->vocabularyData->create(
$limit,
$nameWords
);
Expand All @@ -101,24 +101,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->trans('commands.create.vocabularies.messages.name'),
];

if (isset($vocabularies['success'])) {
$this->getIo()->table($tableHeader, $vocabularies['success']);
if (isset($result['success'])) {
$this->getIo()->table($tableHeader, $result['success']);

$this->getIo()->success(
sprintf(
$this->trans('commands.create.vocabularies.messages.created-terms'),
$limit
)
);
} else {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.vocabularies.messages.error'),
$vocabularies['error'][0]['error']
count($result['success'])
)
);
}

if (isset($result['error'])) {
foreach ($result['error'] as $error) {
$this->getIo()->error(
sprintf(
$this->trans('commands.create.vocabularies.messages.error'),
$error
)
);
}
}

return 0;
}
}
35 changes: 16 additions & 19 deletions src/Utils/Create/CommentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ public function create(
$comments = [];

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']],
]
);
try {
$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']],
]
);

$this->generateFieldSampleData($comment);
$this->generateFieldSampleData($comment);

try {
$comment->save();
$comments['success'][] = [
'nid' => $nid,
Expand All @@ -64,10 +64,7 @@ public function create(
),
];
} catch (\Exception $error) {
$comments['error'][] = [
'title' => $comment->getSubject(),
'error' => $error->getMessage(),
];
$comments['error'][] = $error->getMessage();
}
}

Expand Down
41 changes: 18 additions & 23 deletions src/Utils/Create/NodeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,24 @@ public function create(
) {
$nodes = [];
$bundles = $this->drupalApi->getBundles();
for ($i=0; $i<$limit; $i++) {
$contentType = $contentTypes[array_rand($contentTypes)];
$node = $this->entityTypeManager->getStorage('node')->create(
[
'nid' => null,
'type' => $contentType,
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
'uid' => $this->getUserID(),
'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
'revision' => mt_rand(0, 1),
'status' => true,
'promote' => mt_rand(0, 1),
'langcode' => $language
]
);

$this->generateFieldSampleData($node);

for ($i = 0; $i < $limit; $i++) {
try {
$contentType = $contentTypes[array_rand($contentTypes)];
$node = $this->entityTypeManager->getStorage('node')->create(
[
'nid' => null,
'type' => $contentType,
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
'uid' => $this->getUserID(),
'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
'revision' => mt_rand(0, 1),
'status' => true,
'promote' => mt_rand(0, 1),
'langcode' => $language
]
);

$this->generateFieldSampleData($node);
$node->save();
$nodes['success'][] = [
'nid' => $node->id(),
Expand All @@ -67,11 +66,7 @@ public function create(
)
];
} catch (\Exception $error) {
$nodes['error'][] = [
'node_type' => $bundles[$contentType],
'title' => $node->getTitle(),
'error' => $error->getMessage()
];
$nodes['error'][] = $error->getMessage();
}
}

Expand Down
Loading

0 comments on commit 12ef535

Please sign in to comment.