Skip to content

Commit

Permalink
Implemented code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
kisztof committed Sep 13, 2023
1 parent 2589de0 commit d0a0698
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
69 changes: 48 additions & 21 deletions src/lib/Repository/EventSubscriber/NameSchemaSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function onResolveUrlAliasSchema(ResolveUrlAliasSchemaEvent $event): void
}

$languageList = array_map(
static fn (Language $language): string => $language->getLanguageCode(),
static fn(Language $language): string => $language->getLanguageCode(),
(array)$event->getContent()->getVersionInfo()->getLanguages()
);

Expand All @@ -113,8 +113,6 @@ public function isValid(AbstractSchemaEvent $event): bool
/**
* @param array<string> $languages
* @param array<int, string> $identifiers
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
* @param \Ibexa\Contracts\Core\Repository\Values\Content\Content|null $content
* @param array<string, array<string, string>> $tokenValues
* @param array<int|string, array<string, \Ibexa\Contracts\Core\FieldType\Value>> $fieldMap
*
Expand All @@ -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<int, string> $identifiers
* @param array<int|string, array<string, \Ibexa\Contracts\Core\FieldType\Value>> $fieldMap
*
* @return array<string, string>
*/
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;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d0a0698

Please sign in to comment.