Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass DimensionContentCollection to DataMapper services #182

Merged
merged 15 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
niklasnatter marked this conversation as resolved.
Show resolved Hide resolved
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(
niklasnatter marked this conversation as resolved.
Show resolved Hide resolved
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;
niklasnatter marked this conversation as resolved.
Show resolved Hide resolved

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,67 @@
<?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;

/**
* @implements \IteratorAggregate<DimensionContentInterface>
*/
class PreviewDimensionContentCollection implements \IteratorAggregate, DimensionContentCollectionInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this class. Can we mark it as @internal at current state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
/**
* @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 ['locale' => $this->previewLocale];
Copy link
Member

@alexander-schranz alexander-schranz Jan 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A DimensionCollection should always return all its DimensionAttributes so we should array_merge() them here with the getDefaultDimensionAttributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

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

public function count(): int
{
return 1;
}
Comment on lines +67 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really happy with this :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should return here 2 to simulate that we have a unlocalized and localizedObject.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think i would not return 2 because that is misleading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could adjust the DimensionContentCollectionInterface to not extend Countable as described in #178 to get rid of the method 😇

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function (DimensionContentInterface $dimensionContent) {
* @param DimensionContentInterface[] $data
* @param string $locale
*
* @return ArrayAccessItem[]
* @return ResourceItemInterface[]
*/
protected function decorateResourceItems(array $data, $locale): array
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Application/config/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@

$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? null) ?: 'dev';
$_SERVER['APP_DEBUG'] = $_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? 'prod' !== $_SERVER['APP_ENV'];
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], \FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
Loading