Skip to content

Commit

Permalink
Simplify data maper with unlocalized and localized dimension content
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 6, 2021
1 parent 9fa38cb commit 32a6d9c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class AuthorDataMapper implements DataMapperInterface
{
Expand All @@ -31,29 +32,14 @@ public function __construct(ContactFactoryInterface $contactFactory)

public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$unlocalizedObject instanceof AuthorInterface) {
return;
}

$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if ($localizedObject) {
if (!$localizedObject instanceof AuthorInterface) {
throw new \RuntimeException(\sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', AuthorInterface::class, \get_class($localizedObject)));
}

$this->setAuthorData($localizedObject, $data);

if (!$localizedDimensionContent instanceof AuthorInterface) {
return;
}

$this->setAuthorData($unlocalizedObject, $data);
$this->setAuthorData($localizedDimensionContent, $data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

interface DataMapperInterface
{
Expand All @@ -22,6 +22,7 @@ interface DataMapperInterface
*/
public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;

class ExcerptDataMapper implements DataMapperInterface
Expand All @@ -38,29 +39,14 @@ public function __construct(TagFactoryInterface $tagFactory, CategoryFactoryInte

public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$unlocalizedObject instanceof ExcerptInterface) {
return;
}

$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if ($localizedObject) {
if (!$localizedObject instanceof ExcerptInterface) {
throw new \RuntimeException(\sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', ExcerptInterface::class, \get_class($localizedObject)));
}

$this->setExcerptData($localizedObject, $data);

if (!$localizedDimensionContent instanceof ExcerptInterface) {
return;
}

$this->setExcerptData($unlocalizedObject, $data);
$this->setExcerptData($localizedDimensionContent, $data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
Expand Down Expand Up @@ -77,23 +77,18 @@ public function __construct(

public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$localizedObject || !$localizedObject instanceof RoutableInterface) {
if (!$localizedDimensionContent instanceof RoutableInterface) {
return;
}

if (!$localizedObject instanceof TemplateInterface) {
throw new \RuntimeException('LocalizedObject needs to extend the TemplateInterface');
if (!$localizedDimensionContent instanceof TemplateInterface) {
throw new \RuntimeException('LocalizedDimensionContent needs to extend the TemplateInterface');
}

$type = $localizedObject::getTemplateType();
$type = $localizedDimensionContent::getTemplateType();

/** @var string|null $template */
$template = $data['template'] ?? null;
Expand All @@ -116,28 +111,28 @@ public function map(
return;
}

if (!$localizedObject->getResourceId()) {
if (!$localizedDimensionContent->getResourceId()) {
// FIXME the code only works if the entity is flushed once and has a valid id

return;
}

$locale = $localizedObject->getLocale();
$locale = $localizedDimensionContent->getLocale();
if (!$locale) {
return;
}

/** @var string $name */
$name = $property->getName();

$currentRoutePath = $localizedObject->getTemplateData()[$name] ?? null;
$currentRoutePath = $localizedDimensionContent->getTemplateData()[$name] ?? null;
if (!\array_key_exists($name, $data) && null !== $currentRoutePath) {
return;
}

$entityClass = null;
$routeSchema = null;
$resourceKey = $localizedObject::getResourceKey();
$resourceKey = $localizedDimensionContent::getResourceKey();
foreach ($this->routeMappings as $key => $mapping) {
if ($resourceKey === $mapping['resource_key']) {
$entityClass = $mapping['entityClass'] ?? $key;
Expand All @@ -157,8 +152,8 @@ public function map(
$routeGenerationData = \array_merge(
$data,
[
'_unlocalizedObject' => $unlocalizedObject,
'_localizedObject' => $localizedObject,
'_unlocalizedObject' => $unlocalizedDimensionContent,
'_localizedObject' => $localizedDimensionContent,
]
);

Expand All @@ -174,17 +169,17 @@ public function map(

$route = $this->routeManager->createOrUpdateByAttributes(
$entityClass,
(string) $localizedObject->getResourceId(),
(string) $localizedDimensionContent->getResourceId(),
$locale,
$routePath
);

$this->conflictResolver->resolve($route);

if (($data[$name] ?? null) !== $route->getPath()) {
$localizedObject->setTemplateData(
$localizedDimensionContent->setTemplateData(
\array_merge(
$localizedObject->getTemplateData(),
$localizedDimensionContent->getTemplateData(),
[$name => $route->getPath()]
)
);
Expand Down
26 changes: 6 additions & 20 deletions Content/Application/ContentDataMapper/DataMapper/SeoDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,21 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;

class SeoDataMapper implements DataMapperInterface
{
public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$unlocalizedObject instanceof SeoInterface) {
return;
}

$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if ($localizedObject) {
if (!$localizedObject instanceof SeoInterface) {
throw new \RuntimeException(\sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', SeoInterface::class, \get_class($localizedObject)));
}

$this->setSeoData($localizedObject, $data);

if (!$localizedDimensionContent instanceof SeoInterface) {
return;
}
}Content/Application/ContentDataMapper/DataMapper/TemplateDataMapper.php

$this->setSeoData($unlocalizedObject, $data);
$this->setSeoData($localizedDimensionContent, $data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

Expand All @@ -40,17 +41,16 @@ public function __construct(StructureMetadataFactoryInterface $factory, array $s

public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$unlocalizedObject instanceof TemplateInterface) {
if (!$localizedDimensionContent instanceof TemplateInterface
|| $unlocalizedDimensionContent instanceof TemplateInterface
) {
return;
}

$type = $unlocalizedObject::getTemplateType();
$type = $localizedDimensionContent::getTemplateType();

/** @var string|null $template */
$template = $data['template'] ?? null;
Expand All @@ -69,25 +69,11 @@ public function map(
$template
);

$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if ($localizedObject) {
if (!$localizedObject instanceof TemplateInterface) {
throw new \RuntimeException(\sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', TemplateInterface::class, \get_class($localizedObject)));
}

$localizedObject->setTemplateKey($template);
$localizedObject->setTemplateData($localizedData);
}

if (!$localizedObject) {
// Only set templateKey to unlocalizedDimension when no localizedDimension exist
$unlocalizedObject->setTemplateKey($template);
}
$localizedDimensionContent->setTemplateKey($template);
$localizedDimensionContent->setTemplateData($localizedData);

// Unlocalized dimensions can contain data of different templates so we need to merge them together
$unlocalizedObject->setTemplateData(\array_merge(
$unlocalizedObject->getTemplateData(),
$unlocalizedDimensionContent->setTemplateData(\array_merge(
$unlocalizedDimensionContent->getTemplateData(),
$unlocalizedData
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,21 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;

class WorkflowDataMapper implements DataMapperInterface
{
public function map(
array $data,
DimensionContentCollectionInterface $dimensionContentCollection
DimensionContentInterface $unlocalizedDimensionContent,
DimensionContentInterface $localizedDimensionContent
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$unlocalizedDimensionAttributes = \array_merge($dimensionAttributes, ['locale' => null]);
$unlocalizedObject = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);

if (!$unlocalizedObject instanceof WorkflowInterface) {
return;
}

$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if ($localizedObject) {
if (!$localizedObject instanceof WorkflowInterface) {
throw new \RuntimeException(\sprintf('Expected "$localizedObject" from type "%s" but "%s" given.', WorkflowInterface::class, \get_class($localizedObject)));
}

$this->setWorkflowData($localizedObject, $data);

if (!$localizedDimensionContent instanceof WorkflowInterface) {
return;
}

$this->setWorkflowData($unlocalizedObject, $data);
$this->setWorkflowData($localizedDimensionContent, $data);
}

/**
Expand Down

0 comments on commit 32a6d9c

Please sign in to comment.