Skip to content

Commit

Permalink
Add versioning to dimension content
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 5, 2021
1 parent 9fa38cb commit 695b797
Show file tree
Hide file tree
Showing 20 changed files with 282 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getIgnoredAttributes(object $object): array
'merged',
'dimension',
'resource',
'version',
];
}

Expand Down
8 changes: 5 additions & 3 deletions Content/Application/ContentWorkflow/ContentWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ private function getWorkflow(): SymfonyWorkflowInterface
// | New |--------->| Unpublished | | Review |---------->| Published | | draft | | Review draft |
// | | | |<---------------------| | | |--------------->| |<----------------------------| |
// +-----+ +-------------+ reject +--------+ +------------+ create draft +-------+ reject draft +---------------+
// A | A |
// +---+ | publish |
// publish +----------------------------------------------------------------+
// A | A | A | A |
// +---+ +---+ | | restore | |
// restore publish | +----------------------+ |
// | publish |
// +--------------------------------------------------------------------+

// Configures places
$definition = $definitionBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;

Expand All @@ -29,9 +30,17 @@ class PublishTransitionSubscriber implements EventSubscriberInterface
*/
private $contentCopier;

public function __construct(ContentCopierInterface $contentCopier)
{
/**
* @var DimensionContentRepositoryInterface
*/
private $dimensionContentRepository;

public function __construct(
ContentCopierInterface $contentCopier,
DimensionContentRepositoryInterface $dimensionContentRepository
) {
$this->contentCopier = $contentCopier;
$this->dimensionContentRepository = $dimensionContentRepository;
}

public function onPublish(TransitionEvent $transitionEvent): void
Expand Down Expand Up @@ -68,6 +77,21 @@ public function onPublish(TransitionEvent $transitionEvent): void

$dimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;

// create new version
// TODO optimize latest version and publish locales on write process to avoid loading them here?
$version = 1 + $this->dimensionContentRepository->getLatestVersion($contentRichEntity);
$publishLocales = $this->dimensionContentRepository->getLocales($contentRichEntity, $dimensionAttributes);

foreach ($publishLocales as $publishLocale) {
$this->contentCopier->copy(
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale]),
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale, 'version' => $version])
);
}

// publish content into live workspace
$this->contentCopier->copyFromDimensionContentCollection(
$dimensionContentCollection,
$contentRichEntity,
Expand Down
6 changes: 6 additions & 0 deletions Content/Domain/Model/DimensionContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface DimensionContentInterface
public const STAGE_DRAFT = 'draft';
public const STAGE_LIVE = 'live';

public const DEFAULT_VERSION = 0;

public static function getResourceKey(): string;

public function getLocale(): ?string;
Expand All @@ -28,6 +30,10 @@ public function getStage(): string;

public function setStage(string $stage): void;

public function getVersion(): int;

public function setVersion(int $version): void;

public function getResource(): ContentRichEntityInterface;

public function isMerged(): bool;
Expand Down
16 changes: 16 additions & 0 deletions Content/Domain/Model/DimensionContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ trait DimensionContentTrait
*/
protected $stage = DimensionContentInterface::STAGE_DRAFT;

/**
* @var int
*/
protected $version = DimensionContentInterface::DEFAULT_VERSION;

/**
* @var bool
*/
Expand All @@ -50,6 +55,16 @@ public function getStage(): string
return $this->stage;
}

public function setVersion(int $version): void
{
$this->version = $version;
}

public function getVersion(): int
{
return $this->version;
}

public function isMerged(): bool
{
return $this->isMerged;
Expand All @@ -65,6 +80,7 @@ public static function getDefaultDimensionAttributes(): array
return [
'locale' => null,
'stage' => DimensionContentInterface::STAGE_DRAFT,
'version' => DimensionContentInterface::DEFAULT_VERSION,
];
}

Expand Down
12 changes: 12 additions & 0 deletions Content/Domain/Repository/DimensionContentRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public function load(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentCollectionInterface;

public function getLatestVersion(ContentRichEntityInterface $contentRichEntity): int;

/**
* @param mixed[] $dimensionAttributes
*
* @return string[]
*/
public function getLocales(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array;
}
44 changes: 42 additions & 2 deletions Content/Infrastructure/Doctrine/DimensionContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public function load(

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->innerJoin('dimensionContent.' . $mappingProperty, 'content')
->where('content.id = :id')
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->setParameter('id', $contentRichEntity->getId());

$this->dimensionContentQueryEnhancer->addSelects(
Expand All @@ -77,4 +76,45 @@ public function load(
$dimensionContentClass
);
}

public function getLatestVersion(ContentRichEntityInterface $contentRichEntity): int
{
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent.version')
->orderBy('dimensionContent.version', 'DESC')
->setMaxResults(1)
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->setParameter('id', $contentRichEntity->getId());

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
}

public function getLocales(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array {
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent.locale')
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->andWhere('dimensionContent.locale IS NOT NULL')
->setParameter('id', $contentRichEntity->getId());

unset($dimensionAttributes['locale']);
foreach ($dimensionAttributes as $key => $value) {
$queryBuilder->andWhere('dimensionContent.' . $key . ' = :' . $key)
->setParameter(':' . $key, $value);
}

return \array_map(function($row) {
return $row['locale'];
}, $queryBuilder->getQuery()->getArrayResult());
}
}
1 change: 1 addition & 0 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
if ($reflection->implementsInterface(DimensionContentInterface::class)) {
$this->addField($metadata, 'stage', 'string', ['length' => 16, 'nullable' => false]);
$this->addField($metadata, 'locale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'version', 'integer', ['nullable' => false, 'default' => 0]);
}

if ($reflection->implementsInterface(SeoInterface::class)) {
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@

<service id="sulu_content.publish_transition_subscriber" class="Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\Subscriber\PublishTransitionSubscriber">
<argument type="service" id="sulu_content.content_copier"/>
<argument type="service" id="sulu_content.dimension_content_repository"/>

<tag name="kernel.event_subscriber"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@ public function testLoadExistOrderedDifferent(): void
}, \iterator_to_array($dimensionContentCollection)));
}

public function testGetLatestVersion(): void
{
// prepare database
$contentRichEntity = $this->createContentRichEntity();
$this->createContentDimension($contentRichEntity, []);
$this->createContentDimension($contentRichEntity, ['locale' => 'de']);
$this->createContentDimension($contentRichEntity, ['locale' => 'de', 'version' => 1]);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

// test functionality
$dimensionContentRepository = $this->createContentDimensionRepository();
$version = $dimensionContentRepository->getLatestVersion($contentRichEntity);

// assert result
$this->assertSame(1, $version);
}

public function testGetLocales(): void
{
// prepare database
$contentRichEntity = $this->createContentRichEntity();
$this->createContentDimension($contentRichEntity, []);
$this->createContentDimension($contentRichEntity, ['locale' => 'de']);
$this->createContentDimension($contentRichEntity, ['locale' => 'en']);
$this->createContentDimension($contentRichEntity, ['locale' => 'en', 'stage' => 'live']);
$this->createContentDimension($contentRichEntity, ['locale' => 'fr', 'stage' => 'live']);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

// test functionality
$dimensionContentRepository = $this->createContentDimensionRepository();
$locales = $dimensionContentRepository->getLocales($contentRichEntity, ['stage' => 'live']);

// assert result
$this->assertSame(['en', 'fr'], $locales);
}

private function createContentRichEntity(): Example
{
$example = new Example();
Expand All @@ -123,6 +163,7 @@ private function createContentDimension(Example $example, array $dimensionAttrib
$exampleDimension = new ExampleDimensionContent($example);
$exampleDimension->setStage($dimensionAttributes['stage'] ?? DimensionContentInterface::STAGE_DRAFT);
$exampleDimension->setLocale($dimensionAttributes['locale'] ?? null);
$exampleDimension->setVersion($dimensionAttributes['version'] ?? 0);

$this->getEntityManager()->persist($exampleDimension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function testMerge(): void
->shouldBeCalled();
$mergedDimensionContent->setStage('draft')
->shouldBeCalled();
$mergedDimensionContent->setVersion(0)
->shouldBeCalled();
$mergedDimensionContent->markAsMerged()
->shouldBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testIgnoredAttributes(): void
$object = $this->prophesize(DimensionContentInterface::class);

$this->assertSame(
['id', 'merged', 'dimension', 'resource'],
['id', 'merged', 'dimension', 'resource', 'version'],
$normalizer->getIgnoredAttributes($object->reveal())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ public function testTransitionNoWorkflowInterface(): void
$dimensionContent1 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getVersion()->willReturn(0);
$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getVersion()->willReturn(0);

$this->expectExceptionMessage(\sprintf(
'Expected "%s" but "%s" given.',
Expand Down Expand Up @@ -166,11 +168,13 @@ public function testNotExistTransition(): void
$dimensionContent1->willImplement(WorkflowInterface::class);
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getVersion()->willReturn(0);

$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent2->willImplement(WorkflowInterface::class);
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getVersion()->willReturn(0);

$dimensionContent2->getWorkflowPlace()
->willReturn('unpublished')
Expand Down Expand Up @@ -219,10 +223,12 @@ public function testTransitions(
$dimensionContent1->willImplement(WorkflowInterface::class);
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getVersion()->willReturn(0);
$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent2->willImplement(WorkflowInterface::class);
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getVersion()->willReturn(0);

$dimensionContent2->getWorkflowPlace()
->willReturn($currentPlace)
Expand Down
Loading

0 comments on commit 695b797

Please sign in to comment.