Skip to content

Commit 1ed01ec

Browse files
committed
TASK: Code cleanup
1 parent 301154e commit 1ed01ec

1 file changed

Lines changed: 45 additions & 43 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
1818
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling\ErrorHandlingService;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
1920
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
2021
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
2122
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
@@ -35,12 +36,9 @@
3536
use Neos\Flow\Configuration\ConfigurationManager;
3637
use Neos\Flow\Core\Booting\Exception\SubProcessException;
3738
use Neos\Flow\Core\Booting\Scripts;
38-
use Neos\Flow\Exception;
39-
use Neos\Utility\Exception\FilesException;
40-
use Neos\Utility\Files;
4139
use Neos\Flow\Log\Utility\LogEnvironment;
40+
use Neos\Utility\Files;
4241
use Psr\Log\LoggerInterface;
43-
use Symfony\Component\Yaml\Yaml;
4442

4543
/**
4644
* Provides CLI features for index handling
@@ -137,13 +135,11 @@ class NodeIndexCommandController extends CommandController
137135
* Index a single node by the given identifier and workspace name
138136
*
139137
* @param string $identifier
140-
* @param string $workspace
138+
* @param string|null $workspace
141139
* @param string|null $postfix
142140
* @return void
143141
* @throws ConfigurationException
144-
* @throws FilesException
145-
* @throws StopCommandException
146-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
142+
* @throws Exception
147143
* @throws \Flowpack\ElasticSearch\Exception
148144
*/
149145
public function indexNodeCommand(string $identifier, string $workspace = null, string $postfix = null): void
@@ -161,13 +157,13 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
161157
$node = $context->getNodeByIdentifier($identifier);
162158

163159
if ($node === null) {
164-
return [$workspace->getName(), '-', json_encode($dimensions), 'not found'];
160+
return [$workspace->getName(), '-', json_encode($dimensions, JSON_THROW_ON_ERROR), 'not found'];
165161
}
166162

167163
$this->nodeIndexer->setDimensions($dimensions);
168164
$this->nodeIndexer->indexNode($node);
169165

170-
return [$workspace->getName(), $node->getNodeType()->getName(), json_encode($dimensions), '<success>indexed</success>'];
166+
return [$workspace->getName(), $node->getNodeType()->getName(), json_encode($dimensions, JSON_THROW_ON_ERROR), '<success>indexed</success>'];
171167
};
172168

173169
$indexInWorkspace = function ($identifier, Workspace $workspace) use ($indexNode) {
@@ -209,13 +205,13 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
209205
*
210206
* This command (re-)indexes all nodes contained in the content repository and sets the schema beforehand.
211207
*
212-
* @param int $limit Amount of nodes to index at maximum
208+
* @param int|null $limit Amount of nodes to index at maximum
213209
* @param bool $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
214-
* @param string $workspace name of the workspace which should be indexed
215-
* @param string $postfix Index postfix, index with the same postfix will be deleted if exist
210+
* @param string|null $workspace name of the workspace which should be indexed
211+
* @param string|null $postfix Index postfix, index with the same postfix will be deleted if exist
216212
* @return void
217213
* @throws StopCommandException
218-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
214+
* @throws Exception
219215
* @throws ConfigurationException
220216
*/
221217
public function buildCommand(int $limit = null, bool $update = false, string $workspace = null, string $postfix = null): void
@@ -232,26 +228,26 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
232228

233229
$createIndicesAndApplyMapping = function (array $dimensionsValues) use ($update, $postfix) {
234230
$this->executeInternalCommand('createInternal', [
235-
'dimensionsValues' => json_encode($dimensionsValues),
231+
'dimensionsValues' => json_encode($dimensionsValues, JSON_THROW_ON_ERROR),
236232
'update' => $update,
237233
'postfix' => $postfix,
238234
]);
239235
};
240236

241-
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $update, $postfix) {
237+
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $postfix) {
242238
$this->build($dimensionsValues, $workspace, $postfix, $limit);
243239
};
244240

245241
$refresh = function (array $dimensionsValues) use ($postfix) {
246242
$this->executeInternalCommand('refreshInternal', [
247-
'dimensionsValues' => json_encode($dimensionsValues),
243+
'dimensionsValues' => json_encode($dimensionsValues, JSON_THROW_ON_ERROR),
248244
'postfix' => $postfix,
249245
]);
250246
};
251247

252248
$updateAliases = function (array $dimensionsValues) use ($update, $postfix) {
253249
$this->executeInternalCommand('aliasInternal', [
254-
'dimensionsValues' => json_encode($dimensionsValues),
250+
'dimensionsValues' => json_encode($dimensionsValues, JSON_THROW_ON_ERROR),
255251
'postfix' => $postfix,
256252
'update' => $update,
257253
]);
@@ -268,11 +264,6 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
268264

269265
$runAndLog($createIndicesAndApplyMapping, 'Creating indices and apply mapping');
270266

271-
// $timeStart = microtime(true);
272-
// $this->output(str_pad('Indexing nodes ... ', 20));
273-
// $buildIndex([]);
274-
// $this->outputLine('<success>Done</success> (took %s seconds)', [number_format(microtime(true) - $timeStart, 2)]);
275-
276267
$runAndLog($buildIndex, 'Indexing nodes');
277268

278269
$runAndLog($refresh, 'Refresh indicies');
@@ -289,10 +280,14 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
289280
* Build up the node index
290281
*
291282
* @param array $dimensionsValues
292-
* @param string $postfix
293-
* @param string $workspace
294-
* @param int $limit
283+
* @param string|null $workspace
284+
* @param string|null $postfix
285+
* @param int|null $limit
286+
* @throws ConfigurationException
295287
* @throws Exception
288+
* @throws RuntimeException
289+
* @throws SubProcessException
290+
* @throws \JsonException
296291
*/
297292
private function build(array $dimensionsValues, ?string $workspace = null, ?string $postfix = null, ?int $limit = null): void
298293
{
@@ -307,7 +302,7 @@ private function build(array $dimensionsValues, ?string $workspace = null, ?stri
307302
$buildWorkspaceCommandOptions = static function ($workspace, array $dimensionsValues, ?int $limit, ?string $postfix) {
308303
return [
309304
'workspace' => $workspace instanceof Workspace ? $workspace->getName() : $workspace,
310-
'dimensionsValues' => json_encode($dimensionsValues),
305+
'dimensionsValues' => json_encode($dimensionsValues, JSON_THROW_ON_ERROR),
311306
'postfix' => $postfix,
312307
'limit' => $limit,
313308
];
@@ -342,24 +337,24 @@ private function build(array $dimensionsValues, ?string $workspace = null, ?stri
342337
* @param string $dimensionsValues
343338
* @param bool $update
344339
* @param string|null $postfix
345-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
340+
* @throws Exception
346341
* @throws \Flowpack\ElasticSearch\Exception
347342
* @throws \Neos\Flow\Http\Exception
348343
* @throws \Exception
349344
* @Flow\Internal
350345
*/
351-
public function createInternalCommand(string $dimensionsValues, bool $update = false, ?string $postfix = null): void
346+
public function createInternalCommand(string $dimensionsValues, bool $update = false, string $postfix = null): void
352347
{
353348
if ($update === true) {
354349
$this->logger->warning('!!! Update Mode (Development) active!', LogEnvironment::fromMethodName(__METHOD__));
355350
} else {
356-
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
351+
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true, 512, JSON_THROW_ON_ERROR), $postfix);
357352
if ($this->nodeIndexer->getIndex()->exists() === true) {
358353
$this->logger->warning(sprintf('Deleted index with the same postfix (%s)!', $postfix), LogEnvironment::fromMethodName(__METHOD__));
359354
$this->nodeIndexer->getIndex()->delete();
360355
}
361356
$this->nodeIndexer->getIndex()->create();
362-
$this->logger->info('Created index ' . $this->nodeIndexer->getIndexName() . ' with dimensions ' . json_encode($dimensionsValuesArray), LogEnvironment::fromMethodName(__METHOD__));
357+
$this->logger->info('Created index ' . $this->nodeIndexer->getIndexName() . ' with dimensions ' . json_encode($dimensionsValuesArray, JSON_THROW_ON_ERROR), LogEnvironment::fromMethodName(__METHOD__));
363358
}
364359

365360
$this->applyMapping();
@@ -370,19 +365,20 @@ public function createInternalCommand(string $dimensionsValues, bool $update = f
370365
* @param string $workspace
371366
* @param string $dimensionsValues
372367
* @param string $postfix
373-
* @param int $limit
368+
* @param int|null $limit
374369
* @return void
375370
* @Flow\Internal
371+
* @throws \JsonException
376372
*/
377373
public function buildWorkspaceInternalCommand(string $workspace, string $dimensionsValues, string $postfix, int $limit = null): void
378374
{
379-
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
375+
$dimensionsValuesArray = $this->configureNodeIndexer(json_decode($dimensionsValues, true, 512, JSON_THROW_ON_ERROR), $postfix);
380376

381377
$workspaceLogger = function ($workspaceName, $indexedNodes, $dimensions) {
382378
if ($dimensions === []) {
383379
$message = 'Workspace "' . $workspaceName . '" without dimensions done. (Indexed ' . $indexedNodes . ' nodes)';
384380
} else {
385-
$message = 'Workspace "' . $workspaceName . '" and dimensions "' . json_encode($dimensions) . '" done. (Indexed ' . $indexedNodes . ' nodes)';
381+
$message = 'Workspace "' . $workspaceName . '" and dimensions "' . json_encode($dimensions, JSON_THROW_ON_ERROR) . '" done. (Indexed ' . $indexedNodes . ' nodes)';
386382
}
387383
$this->outputLine($message);
388384
};
@@ -397,15 +393,16 @@ public function buildWorkspaceInternalCommand(string $workspace, string $dimensi
397393
*
398394
* @param string $dimensionsValues
399395
* @param string $postfix
400-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
396+
* @throws Exception
401397
* @throws \Flowpack\ElasticSearch\Exception
402398
* @throws \Neos\Flow\Http\Exception
403399
* @throws ConfigurationException
400+
* @throws \JsonException
404401
* @Flow\Internal
405402
*/
406403
public function refreshInternalCommand(string $dimensionsValues, string $postfix): void
407404
{
408-
$this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
405+
$this->configureNodeIndexer(json_decode($dimensionsValues, true, 512, JSON_THROW_ON_ERROR), $postfix);
409406

410407
$this->logger->info(vsprintf('Refreshing index %s', [$this->nodeIndexer->getIndexName()]), LogEnvironment::fromMethodName(__METHOD__));
411408
$this->nodeIndexer->getIndex()->refresh();
@@ -417,18 +414,19 @@ public function refreshInternalCommand(string $dimensionsValues, string $postfix
417414
* @param string $dimensionsValues
418415
* @param string $postfix
419416
* @param bool $update
420-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
417+
* @throws Exception
421418
* @throws \Flowpack\ElasticSearch\Exception
422419
* @throws ApiException
423420
* @throws ConfigurationException
421+
* @throws \JsonException
424422
* @Flow\Internal
425423
*/
426424
public function aliasInternalCommand(string $dimensionsValues, string $postfix, bool $update = false): void
427425
{
428426
if ($update === true) {
429427
return;
430428
}
431-
$this->configureNodeIndexer(json_decode($dimensionsValues, true), $postfix);
429+
$this->configureNodeIndexer(json_decode($dimensionsValues, true, 512, JSON_THROW_ON_ERROR), $postfix);
432430

433431
$this->logger->info(vsprintf('Update alias for index %s', [$this->nodeIndexer->getIndexName()]), LogEnvironment::fromMethodName(__METHOD__));
434432
$this->nodeIndexer->updateIndexAlias();
@@ -451,7 +449,9 @@ private function configureNodeIndexer(array $dimensionsValues, string $postfix):
451449
* Clean up old indexes (i.e. all but the current one)
452450
*
453451
* @return void
454-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
452+
* @throws ConfigurationException
453+
* @throws Exception
454+
* @throws \JsonException
455455
*/
456456
public function cleanupCommand(): void
457457
{
@@ -468,7 +468,7 @@ public function cleanupCommand(): void
468468
}
469469
} catch (ApiException $exception) {
470470
$exception->getResponse()->getBody()->rewind();
471-
$response = json_decode($exception->getResponse()->getBody()->getContents(), false);
471+
$response = json_decode($exception->getResponse()->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
472472
$message = sprintf('Nothing removed. ElasticSearch responded with status %s', $response->status);
473473

474474
if (isset($response->error->type)) {
@@ -499,6 +499,7 @@ private function outputErrorHandling(): void
499499
* @return string
500500
* @throws RuntimeException
501501
* @throws SubProcessException
502+
* @throws \JsonException
502503
*/
503504
private function executeInternalCommand(string $command, array $arguments): string
504505
{
@@ -509,7 +510,7 @@ private function executeInternalCommand(string $command, array $arguments): stri
509510
$status = Scripts::executeCommand($commandIdentifier, $this->flowSettings, true, array_filter($arguments));
510511

511512
if ($status !== true) {
512-
throw new RuntimeException(vsprintf('Command: %s with parameters: %s', [$commandIdentifier, json_encode($arguments)]), 1426767159);
513+
throw new RuntimeException(vsprintf('Command: %s with parameters: %s', [$commandIdentifier, json_encode($arguments, JSON_THROW_ON_ERROR)]), 1426767159);
513514
}
514515
} else {
515516
$commandIdentifier = $command . 'Command';
@@ -536,7 +537,7 @@ private function createContentContext(string $workspaceName, array $dimensions =
536537

537538
if ($dimensions !== []) {
538539
$contextProperties['dimensions'] = $dimensions;
539-
$contextProperties['targetDimensions'] = array_map(function ($dimensionValues) {
540+
$contextProperties['targetDimensions'] = array_map(static function ($dimensionValues) {
540541
return array_shift($dimensionValues);
541542
}, $dimensions);
542543
}
@@ -548,9 +549,10 @@ private function createContentContext(string $workspaceName, array $dimensions =
548549
* Apply the mapping to the current index.
549550
*
550551
* @return void
551-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
552+
* @throws Exception
552553
* @throws \Flowpack\ElasticSearch\Exception
553554
* @throws ConfigurationException
555+
* @throws \Neos\Flow\Http\Exception
554556
*/
555557
private function applyMapping(): void
556558
{

0 commit comments

Comments
 (0)