Skip to content

Commit

Permalink
fix: Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nattfarinn committed Dec 20, 2023
1 parent e4556aa commit b981897
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions eZ/Publish/Core/Persistence/Legacy/Content/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
* Mapper for Content Handler.
*
* Performs mapping of Content objects.
*
* @phpstan-type TVersionedLanguageFieldDefinitionsMap array<int, array<int, array<string, array<int, \eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition>>>>
* @phpstan-type TVersionedFieldMap array<int, array<int, array<int, \eZ\Publish\SPI\Persistence\Content\Field>>>
* @phpstan-type TVersionedNameMap array<int, array<int, array<string, array<int, string>>>>
* @phpstan-type TContentInfoMap array<int, \eZ\Publish\SPI\Persistence\Content\ContentInfo>
* @phpstan-type TVersionInfoMap array<int, array<int, \eZ\Publish\SPI\Persistence\Content\VersionInfo>>
*/
class Mapper
{
Expand Down Expand Up @@ -193,13 +199,12 @@ public function extractContentFromRows(
string $prefix = 'ezcontentobject_'
): array {
$versionedNameData = [];
$languageCodes = [];

foreach ($nameRows as $row) {
$contentId = (int)$row["{$prefix}name_contentobject_id"];
$versionNo = (int)$row["{$prefix}name_content_version"];
$versionedNameData[$contentId][$versionNo][$row["{$prefix}name_content_translation"]] = $row["{$prefix}name_name"];
$languageCodes[] = $row["{$prefix}name_content_translation"];
$languageCode = $row["{$prefix}name_content_translation"];
$versionedNameData[$contentId][$versionNo][$languageCode] = $row["{$prefix}name_name"];
}

$contentInfos = [];
Expand Down Expand Up @@ -249,21 +254,27 @@ public function extractContentFromRows(
}

/**
* @phpstan-param TContentInfoMap $contentInfos
* @phpstan-param TVersionInfoMap $versionInfos
* @phpstan-param TVersionedFieldMap $fields
* @phpstan-param TVersionedLanguageFieldDefinitionsMap $missingFieldDefinitions
* @phpstan-param TVersionedNameMap $versionedNames
*
* @return \eZ\Publish\SPI\Persistence\Content[]
*/
private function buildContentObjects(
array $contentInfos,
array $versionInfos,
array $fields,
array $fieldDefinitions,
array $versionedNameData
array $missingFieldDefinitions,
array $versionedNames
): array {
$results = [];

foreach ($contentInfos as $contentId => $contentInfo) {
foreach ($versionInfos[$contentId] as $versionId => $versionInfo) {
// Fallback to just main language name if versioned name data is missing
$names = $versionedNameData[$contentId][$versionInfo->versionNo]
$names = $versionedNames[$contentId][$versionInfo->versionNo]
?? [$contentInfo->mainLanguageCode => $contentInfo->name];

$content = new Content();
Expand All @@ -272,7 +283,8 @@ private function buildContentObjects(
$content->versionInfo->contentInfo = $contentInfo;
$content->fields = array_values($fields[$contentId][$versionId]);

foreach ($fieldDefinitions[$contentId][$versionId] as $languageCode => $versionFieldDefinitions) {
$missingVersionFieldDefinitions = $missingFieldDefinitions[$contentId][$versionId];
foreach ($missingVersionFieldDefinitions as $languageCode => $versionFieldDefinitions) {
foreach ($versionFieldDefinitions as $fieldDefinition) {
$content->fields[] = $this->createEmptyField(
$fieldDefinition,
Expand All @@ -289,6 +301,8 @@ private function buildContentObjects(
}

/**
* @phpstan-return TVersionedLanguageFieldDefinitionsMap
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
*/
private function loadCachedVersionFieldDefinitionsPerLanguage(
Expand All @@ -306,14 +320,16 @@ private function loadCachedVersionFieldDefinitionsPerLanguage(
$languageMask = (int)$row["{$prefix}version_language_mask"];

if (!isset($fieldDefinitions[$contentId][$versionId])) {
$contentType = $contentTypes[$contentTypeId] = $contentTypes[$contentTypeId]
?? $this->contentTypeHandler->load($contentTypeId);
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
$languageCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
foreach ($languageCodes as $languageCode) {
$id = $fieldDefinition->id;
$fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition;
}
continue;
}

$languageCodes = $this->extractLanguageCodesFromMask($languageMask, $allLanguages);
$contentType = $contentTypes[$contentTypeId] = $contentTypes[$contentTypeId]
?? $this->contentTypeHandler->load($contentTypeId);
foreach ($contentType->fieldDefinitions as $fieldDefinition) {
foreach ($languageCodes as $languageCode) {
$id = $fieldDefinition->id;
$fieldDefinitions[$contentId][$versionId][$languageCode][$id] = $fieldDefinition;
}
}
}
Expand Down

0 comments on commit b981897

Please sign in to comment.