Skip to content

Commit

Permalink
Pass DimensionContentCollection to DataMapper services (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Jan 20, 2021
1 parent e3e85e9 commit 8f26491
Show file tree
Hide file tree
Showing 30 changed files with 814 additions and 395 deletions.
6 changes: 3 additions & 3 deletions Content/Application/ContentDataMapper/ContentDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper;

use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\DataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;

class ContentDataMapper implements ContentDataMapperInterface
{
Expand All @@ -32,11 +33,10 @@ public function __construct(iterable $dataMappers)

public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): void {
foreach ($this->dataMappers as $mapper) {
$mapper->map($data, $unlocalizedObject, $localizedObject);
$mapper->map($data, $dimensionContentCollection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

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

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

interface ContentDataMapperInterface
{
/**
* @param array<string, mixed> $data
*/
public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@

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

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

interface DataMapperInterface
{
/**
* @param array<string, mixed> $data
*/
public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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\ExcerptInterface;

class ExcerptDataMapper implements DataMapperInterface
Expand All @@ -37,13 +38,18 @@ public function __construct(TagFactoryInterface $tagFactory, CategoryFactoryInte

public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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\RoutableInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
Expand Down Expand Up @@ -76,9 +77,14 @@ public function __construct(

public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): void {
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();
$localizedObject = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

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

if (!$localizedObject || !$localizedObject instanceof RoutableInterface) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@

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

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

class SeoDataMapper implements DataMapperInterface
{
public function map(
array $data,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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\TemplateInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

Expand All @@ -39,9 +40,12 @@ public function __construct(StructureMetadataFactoryInterface $factory, array $s

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

if (!$unlocalizedObject instanceof TemplateInterface) {
return;
}
Expand All @@ -65,6 +69,8 @@ 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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@

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,
object $unlocalizedObject,
?object $localizedObject = null
DimensionContentCollectionInterface $dimensionContentCollection
): 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)));
Expand Down
15 changes: 8 additions & 7 deletions Content/Application/ContentMerger/ContentMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ public function __construct(

public function merge(DimensionContentCollectionInterface $dimensionContentCollection): DimensionContentInterface
{
$unlocalizedDimensionContent = $dimensionContentCollection->getUnlocalizedDimensionContent();

if (!$unlocalizedDimensionContent) {
if (!$dimensionContentCollection->count()) {
throw new \RuntimeException('Expected at least one dimensionContent given.');
}

$contentRichEntity = $unlocalizedDimensionContent->getResource();

$mergedDimensionContent = $contentRichEntity->createDimensionContent();
$mergedDimensionContent->markAsMerged();
$mergedDimensionContent = null;

foreach ($dimensionContentCollection as $dimensionContent) {
if (!$mergedDimensionContent) {
$contentRichEntity = $dimensionContent->getResource();
$mergedDimensionContent = $contentRichEntity->createDimensionContent();
$mergedDimensionContent->markAsMerged();
}

foreach ($this->mergers as $merger) {
$merger->merge($mergedDimensionContent, $dimensionContent);
}
Expand Down
3 changes: 1 addition & 2 deletions Content/Application/ContentWorkflow/ContentWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public function apply(

$dimensionContentCollection = $this->dimensionContentRepository->load($contentRichEntity, $dimensionAttributes);
$dimensionAttributes = $dimensionContentCollection->getDimensionAttributes();

$localizedDimensionContent = $dimensionContentCollection->getLocalizedDimensionContent();
$localizedDimensionContent = $dimensionContentCollection->getDimensionContent($dimensionAttributes);

if (!$localizedDimensionContent) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ public function onUnpublish(TransitionEvent $transitionEvent): void
$liveDimensionAttributes = array_merge($dimensionAttributes, ['stage' => DimensionContentInterface::STAGE_LIVE]);

$dimensionContentCollection = $this->dimensionContentRepository->load($contentRichEntity, $liveDimensionAttributes);
$localizedDimensionContent = $dimensionContentCollection->getLocalizedDimensionContent();
if (!$localizedDimensionContent) {
$localizedLiveDimensionContent = $dimensionContentCollection->getDimensionContent($liveDimensionAttributes);

if (!$localizedLiveDimensionContent) {
throw new ContentNotFoundException($contentRichEntity, $liveDimensionAttributes);
}

$this->entityManager->remove($localizedDimensionContent);
$this->entityManager->remove($localizedLiveDimensionContent);
}

public static function getSubscribedEvents(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ public function create(
}
}

$this->contentDataMapper->map($data, $unlocalizedDimensionContent, $localizedDimensionContent);

return new DimensionContentCollection(
$dimensionContentCollection = new DimensionContentCollection(
$orderedContentDimensions,
$dimensionAttributes,
$dimensionContentCollection->getDimensionContentClass()
);

$this->contentDataMapper->map($data, $dimensionContentCollection);

return $dimensionContentCollection;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions Content/Domain/Model/DimensionContentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,6 @@ public function getDimensionContentClass(): string
return $this->dimensionContentClass;
}

public function getUnlocalizedDimensionContent(): ?DimensionContentInterface
{
return $this->unlocalizedDimensionContent;
}

public function getLocalizedDimensionContent(): ?DimensionContentInterface
{
return $this->localizedDimensionContent;
}

public function getDimensionContent(array $dimensionAttributes): ?DimensionContentInterface
{
$dimensionAttributes = array_merge($this->defaultDimensionAttributes, $dimensionAttributes);
Expand Down
4 changes: 0 additions & 4 deletions Content/Domain/Model/DimensionContentCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
*/
interface DimensionContentCollectionInterface extends \Traversable, \Countable
{
public function getUnlocalizedDimensionContent(): ?DimensionContentInterface;

public function getLocalizedDimensionContent(): ?DimensionContentInterface;

/**
* @param mixed[] $dimensionAttributes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public function getId($object)
*/
public function setValues($object, $locale, array $data): void
{
$this->contentDataMapper->map($data, $object, $object);
$previewDimensionContentCollection = new PreviewDimensionContentCollection($object, $locale);
$this->contentDataMapper->map($data, $previewDimensionContentCollection);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview;

use Doctrine\Common\Collections\ArrayCollection;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

/**
* @internal
* @implements \IteratorAggregate<DimensionContentInterface>
*/
class PreviewDimensionContentCollection implements \IteratorAggregate, DimensionContentCollectionInterface
{
/**
* @var DimensionContentInterface
*/
private $previewDimensionContent;

/**
* @var string
*/
private $previewLocale;

public function __construct(
DimensionContentInterface $previewDimensionContent,
string $previewLocale
) {
$this->previewDimensionContent = $previewDimensionContent;
$this->previewLocale = $previewLocale;
}

public function getDimensionContentClass(): string
{
return \get_class($this->previewDimensionContent);
}

public function getDimensionContent(array $dimensionAttributes): ?DimensionContentInterface
{
return $this->previewDimensionContent;
}

public function getDimensionAttributes(): array
{
return array_merge(
$this->previewDimensionContent::getDefaultDimensionAttributes(),
['locale' => $this->previewLocale]
);
}

public function getIterator()
{
return new ArrayCollection([$this->previewDimensionContent]);
}

public function count(): int
{
return 1;
}
}

0 comments on commit 8f26491

Please sign in to comment.