Skip to content

Commit 12ef535

Browse files
LOBsTerrjmolivas
authored andcommitted
[create:*] Create content exception. Fix #3779 (#3781)
* 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
1 parent 3741529 commit 12ef535

12 files changed

Lines changed: 212 additions & 173 deletions

src/Command/Create/CommentsCommand.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
142142
$titleWords = $input->getOption('title-words')?:5;
143143
$timeRange = $input->getOption('time-range')?:31536000;
144144

145-
$comments = $this->createCommentData->create(
145+
$result = $this->createCommentData->create(
146146
$nodeId,
147147
$limit,
148148
$titleWords,
149149
$timeRange
150150
);
151151

152-
$tableHeader = [
153-
$this->trans('commands.create.comments.messages.node-id'),
154-
$this->trans('commands.create.comments.messages.comment-id'),
155-
$this->trans('commands.create.comments.messages.title'),
156-
$this->trans('commands.create.comments.messages.created'),
157-
];
152+
if ($result['success']) {
158153

159-
$this->getIo()->table($tableHeader, $comments['success']);
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+
];
160160

161-
$this->getIo()->success(
162-
sprintf(
163-
$this->trans('commands.create.comments.messages.created-comments'),
164-
$limit
165-
)
166-
);
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+
}
167181

168182
return 0;
169183
}

src/Command/Create/NodesCommand.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,31 +203,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
203203
$contentTypes = $available_types;
204204
}
205205

206-
$nodes = $this->createNodeData->create(
206+
$result = $this->createNodeData->create(
207207
$contentTypes,
208208
$limit,
209209
$titleWords,
210210
$timeRange,
211211
$language
212212
);
213-
214-
$nodes = is_array($nodes) ? $nodes : [$nodes];
215-
216-
$tableHeader = [
217-
$this->trans('commands.create.nodes.messages.node-id'),
218-
$this->trans('commands.create.nodes.messages.content-type'),
219-
$this->trans('commands.create.nodes.messages.title'),
220-
$this->trans('commands.create.nodes.messages.created'),
221-
];
222-
223-
$this->getIo()->table($tableHeader, $nodes['success']);
224-
225-
$this->getIo()->success(
226-
sprintf(
227-
$this->trans('commands.create.nodes.messages.created-nodes'),
228-
$limit
229-
)
230-
);
213+
214+
if ($result['success']) {
215+
$tableHeader = [
216+
$this->trans('commands.create.nodes.messages.node-id'),
217+
$this->trans('commands.create.nodes.messages.content-type'),
218+
$this->trans('commands.create.nodes.messages.title'),
219+
$this->trans('commands.create.nodes.messages.created'),
220+
];
221+
222+
$this->getIo()->table($tableHeader, $result['success']);
223+
224+
$this->getIo()->success(
225+
sprintf(
226+
$this->trans('commands.create.nodes.messages.created-nodes'),
227+
count($result['success'])
228+
)
229+
);
230+
}
231+
232+
if (isset($result['error'])) {
233+
foreach ($result['error'] as $error) {
234+
$this->getIo()->error(
235+
sprintf(
236+
$this->trans('commands.create.nodes.messages.error'),
237+
$error
238+
)
239+
);
240+
}
241+
}
231242

232243
return 0;
233244
}

src/Command/Create/RolesCommand.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7676
{
7777
$limit = $input->getOption('limit')?:5;
7878

79-
$roles = $this->createRoleData->create(
79+
$result = $this->createRoleData->create(
8080
$limit
8181
);
8282

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

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

9191
$this->getIo()->success(
9292
sprintf(
9393
$this->trans('commands.create.roles.messages.created-roles'),
94-
$limit
94+
count($result['success'])
9595
)
9696
);
9797
}
9898

99+
if (isset($result['error'])) {
100+
foreach ($result['error'] as $error) {
101+
$this->getIo()->error(
102+
sprintf(
103+
$this->trans('commands.create.roles.messages.error'),
104+
$error
105+
)
106+
);
107+
}
108+
}
109+
99110
return 0;
100111
}
101112
}

src/Command/Create/TermsCommand.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
137137
$vocabularies = array_keys($this->drupalApi->getVocabularies());
138138
}
139139

140-
$terms = $this->createTermData->create(
140+
$result = $this->createTermData->create(
141141
$vocabularies,
142142
$limit,
143143
$nameWords
@@ -149,14 +149,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
149149
$this->trans('commands.create.terms.messages.name'),
150150
];
151151

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

154-
$this->getIo()->success(
155-
sprintf(
156-
$this->trans('commands.create.terms.messages.created-terms'),
157-
$limit
158-
)
159-
);
155+
$this->getIo()->success(
156+
sprintf(
157+
$this->trans('commands.create.terms.messages.created-terms'),
158+
count($result['success'])
159+
)
160+
);
161+
}
162+
163+
if (isset($result['error'])) {
164+
foreach ($result['error'] as $error) {
165+
$this->getIo()->error(
166+
sprintf(
167+
$this->trans('commands.create.terms.messages.error'),
168+
$error
169+
)
170+
);
171+
}
172+
}
160173

161174
return 0;
162175
}

src/Command/Create/UsersCommand.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
153153
$roles = $this->drupalApi->getRoles();
154154
}
155155

156-
$users = $this->createUserData->create(
156+
$result = $this->createUserData->create(
157157
$roles,
158158
$limit,
159159
$password,
@@ -167,17 +167,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
167167
$this->trans('commands.create.users.messages.created'),
168168
];
169169

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

173173
$this->getIo()->success(
174174
sprintf(
175175
$this->trans('commands.create.users.messages.created-users'),
176-
$limit
176+
count($result['success'])
177177
)
178178
);
179179
}
180180

181+
if (isset($result['error'])) {
182+
foreach ($result['error'] as $error) {
183+
$this->getIo()->error(
184+
sprintf(
185+
$this->trans('commands.create.users.messages.error'),
186+
$error
187+
)
188+
);
189+
}
190+
}
191+
181192
return 0;
182193
}
183194
}

src/Command/Create/VocabulariesCommand.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9191
$limit = $input->getOption('limit')?:25;
9292
$nameWords = $input->getOption('name-words')?:5;
9393

94-
$vocabularies = $this->vocabularyData->create(
94+
$result = $this->vocabularyData->create(
9595
$limit,
9696
$nameWords
9797
);
@@ -101,24 +101,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
101101
$this->trans('commands.create.vocabularies.messages.name'),
102102
];
103103

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

107107
$this->getIo()->success(
108108
sprintf(
109109
$this->trans('commands.create.vocabularies.messages.created-terms'),
110-
$limit
111-
)
112-
);
113-
} else {
114-
$this->getIo()->error(
115-
sprintf(
116-
$this->trans('commands.create.vocabularies.messages.error'),
117-
$vocabularies['error'][0]['error']
110+
count($result['success'])
118111
)
119112
);
120113
}
121114

115+
if (isset($result['error'])) {
116+
foreach ($result['error'] as $error) {
117+
$this->getIo()->error(
118+
sprintf(
119+
$this->trans('commands.create.vocabularies.messages.error'),
120+
$error
121+
)
122+
);
123+
}
124+
}
125+
122126
return 0;
123127
}
124128
}

src/Utils/Create/CommentData.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ public function create(
3535
$comments = [];
3636

3737
for ($i = 0; $i < $limit; $i++) {
38-
$comment = $this->entityTypeManager->getStorage('comment')->create(
39-
[
40-
'entity_id' => $nid,
41-
'entity_type' => 'node',
42-
'field_name' => 'comment',
43-
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
44-
'uid' => $this->getUserID(),
45-
'status' => true,
46-
'subject' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
47-
'language' => 'und',
48-
'comment_body' => ['und' => ['random body']],
49-
]
50-
);
38+
try {
39+
$comment = $this->entityTypeManager->getStorage('comment')->create(
40+
[
41+
'entity_id' => $nid,
42+
'entity_type' => 'node',
43+
'field_name' => 'comment',
44+
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
45+
'uid' => $this->getUserID(),
46+
'status' => true,
47+
'subject' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
48+
'language' => 'und',
49+
'comment_body' => ['und' => ['random body']],
50+
]
51+
);
5152

52-
$this->generateFieldSampleData($comment);
53+
$this->generateFieldSampleData($comment);
5354

54-
try {
5555
$comment->save();
5656
$comments['success'][] = [
5757
'nid' => $nid,
@@ -64,10 +64,7 @@ public function create(
6464
),
6565
];
6666
} catch (\Exception $error) {
67-
$comments['error'][] = [
68-
'title' => $comment->getSubject(),
69-
'error' => $error->getMessage(),
70-
];
67+
$comments['error'][] = $error->getMessage();
7168
}
7269
}
7370

src/Utils/Create/NodeData.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,24 @@ public function create(
3636
) {
3737
$nodes = [];
3838
$bundles = $this->drupalApi->getBundles();
39-
for ($i=0; $i<$limit; $i++) {
40-
$contentType = $contentTypes[array_rand($contentTypes)];
41-
$node = $this->entityTypeManager->getStorage('node')->create(
42-
[
43-
'nid' => null,
44-
'type' => $contentType,
45-
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
46-
'uid' => $this->getUserID(),
47-
'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
48-
'revision' => mt_rand(0, 1),
49-
'status' => true,
50-
'promote' => mt_rand(0, 1),
51-
'langcode' => $language
52-
]
53-
);
54-
55-
$this->generateFieldSampleData($node);
56-
39+
for ($i = 0; $i < $limit; $i++) {
5740
try {
41+
$contentType = $contentTypes[array_rand($contentTypes)];
42+
$node = $this->entityTypeManager->getStorage('node')->create(
43+
[
44+
'nid' => null,
45+
'type' => $contentType,
46+
'created' => REQUEST_TIME - mt_rand(0, $timeRange),
47+
'uid' => $this->getUserID(),
48+
'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
49+
'revision' => mt_rand(0, 1),
50+
'status' => true,
51+
'promote' => mt_rand(0, 1),
52+
'langcode' => $language
53+
]
54+
);
55+
56+
$this->generateFieldSampleData($node);
5857
$node->save();
5958
$nodes['success'][] = [
6059
'nid' => $node->id(),
@@ -67,11 +66,7 @@ public function create(
6766
)
6867
];
6968
} catch (\Exception $error) {
70-
$nodes['error'][] = [
71-
'node_type' => $bundles[$contentType],
72-
'title' => $node->getTitle(),
73-
'error' => $error->getMessage()
74-
];
69+
$nodes['error'][] = $error->getMessage();
7570
}
7671
}
7772

0 commit comments

Comments
 (0)