From 1ca59c0ca68480153c13c527052fd0b41ba342ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20S=C5=82omka?= Date: Wed, 13 Sep 2023 14:10:07 +0200 Subject: [PATCH] Implemented code review suggestions --- .../EventSubscriber/NameSchemaSubscriber.php | 67 +++++++++++++------ .../NameSchema/NameSchemaService.php | 2 +- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php b/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php index 61f2c81a51..12c7fc8ef9 100644 --- a/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php +++ b/src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php @@ -113,8 +113,6 @@ public function isValid(AbstractSchemaEvent $event): bool /** * @param array $languages * @param array $identifiers - * @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType - * @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|null $content * @param array> $tokenValues * @param array> $fieldMap * @@ -129,29 +127,58 @@ private function processEvent( array $fieldMap = [] ): array { foreach ($languages as $languageCode) { - $tokenValues[$languageCode] ?? $tokenValues[$languageCode] = []; - foreach ($identifiers as $identifier) { - $fieldDefinition = $contentType->getFieldDefinition($identifier); - if (null === $fieldDefinition) { - continue; - } - - $persistenceFieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier); + $tokenValues[$languageCode] = $content !== null || !empty($fieldMap) + ? $this->getValues( + $identifiers, + $contentType, + $content, + $fieldMap, + $languageCode + ) + : []; + } - if ($content === null && empty($fieldMap)) { - continue; - } + return $tokenValues; + } - $field = $fieldMap ? $fieldMap[$identifier][$languageCode] ?? '' : $content->getFieldValue($identifier, $languageCode); + /** + * @param array $identifiers + * @param array> $fieldMap + * + * @return array + */ + private function getValues( + array $identifiers, + ContentType $contentType, + ?Content $content, + array $fieldMap, + string $languageCode + ): array { + $tokenValues = []; + foreach ($identifiers as $identifier) { + $fieldDefinition = $contentType->getFieldDefinition($identifier); + if (null === $fieldDefinition) { + continue; + } - $field = $field ? $persistenceFieldType->getName( - $field, - $fieldDefinition, - $languageCode - ) : ''; + $persistenceFieldType = $this->fieldTypeRegistry->getFieldType($fieldDefinition->fieldTypeIdentifier); - $tokenValues[$languageCode][$identifier] = $field; + if ($content === null && empty($fieldMap)) { + continue; } + + $field = $fieldMap ? $fieldMap[$identifier][$languageCode] ?? '' : $content->getFieldValue( + $identifier, + $languageCode + ); + + $field = $field ? $persistenceFieldType->getName( + $field, + $fieldDefinition, + $languageCode + ) : ''; + + $tokenValues[$identifier] = $field; } return $tokenValues; diff --git a/src/lib/Repository/NameSchema/NameSchemaService.php b/src/lib/Repository/NameSchema/NameSchemaService.php index 51cd29c0b0..782a154191 100644 --- a/src/lib/Repository/NameSchema/NameSchemaService.php +++ b/src/lib/Repository/NameSchema/NameSchemaService.php @@ -278,7 +278,7 @@ protected function filterNameSchema(string $nameSchema): array public function buildNames(array $tokenValues, string $nameSchema): array { if (empty($tokenValues)) { - throw new UnresolvedTokenNamesException(); + throw new UnresolvedTokenNamesException('$tokenValues', 'is Empty'); } [$filteredNameSchema, $groupLookupTable] = $this->filterNameSchema($nameSchema);