Skip to content

Commit

Permalink
Remove Dimension Entity (#174)
Browse files Browse the repository at this point in the history
* Remove Dimension entity and inline locale and stage getter and setters

* Fixes after review of nnatter

* Fix merge conflict in UPGRADE

* Refractor CreateExampleTrait to user mapper services

* Increase locale to 7

* Fix phpstan

* Fix composer scripts after rebase

* Fix composer scripts

* Apply suggestions from code review

Co-authored-by: nnatter <[email protected]>

* Apply change requests

* Refractor CreateExampleTrait

* Rename getDefaultAttributes to getDefaultDimensionAttributes

Co-authored-by: nnatter <[email protected]>
  • Loading branch information
alexander-schranz and niklasnatter authored Jan 14, 2021
1 parent b2a62a6 commit 29afa52
Show file tree
Hide file tree
Showing 103 changed files with 1,622 additions and 3,014 deletions.
5 changes: 2 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
- disable_php_memory_limit
- run: php -m
- run: php composer.phar bootstrap-test-environment
- run: php composer.phar test -- --coverage-php Tests/reports/coverage.php --coverage-html Tests/reports/html --log-junit Tests/reports/unit/junit.xml --coverage-clover Tests/reports/clover.xml
- run: php composer.phar test-with-coverage
- store_artifacts:
path: Tests/reports/html
- store_test_results:
Expand All @@ -122,8 +122,7 @@ jobs:
at: .
- enable_php_pcov
- disable_php_memory_limit
- run: php vendor/bin/code-coverage-checker "Tests/reports/coverage.php" "line" "99.00" "Content/Domain" "Content/Application"
- run: php vendor/bin/code-coverage-checker "Tests/reports/coverage.php" "line" "94.00" "Content/Infrastructure" "DependencyInjection"
- run: php composer.phar check-coverage

coverage-all-upload:
docker:
Expand Down
1 change: 1 addition & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ return PhpCsFixer\Config::create()
PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('cache')
->exclude('Tests/reports/')
->in(__DIR__)
);
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public function map(
}

if (null === $entityClass || null === $routeSchema) {
return;
// TODO FIXME add test case for this
return; // @codeCoverageIgnore
}

$routePath = $data[$name] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

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

class WorkflowDataMapper implements DataMapperInterface
Expand Down Expand Up @@ -60,7 +59,7 @@ private function setInitialPlaceToDraftDimension(WorkflowInterface $object, arra
// see: https://github.com/sulu/SuluContentBundle/issues/92

if (!$object instanceof DimensionContentInterface
|| DimensionInterface::STAGE_DRAFT !== $object->getDimension()->getStage()) {
|| DimensionContentInterface::STAGE_DRAFT !== $object->getStage()) {
return;
}

Expand All @@ -79,7 +78,7 @@ private function setPublishedToLiveDimension(WorkflowInterface $object, array $d
// therefore we only want to copy the published property from the draft to the live dimension

if (!$object instanceof DimensionContentInterface
|| DimensionInterface::STAGE_LIVE !== $object->getDimension()->getStage()) {
|| DimensionContentInterface::STAGE_LIVE !== $object->getStage()) {
return;
}

Expand Down
11 changes: 5 additions & 6 deletions Content/Application/ContentIndexer/ContentIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;

class ContentIndexer implements ContentIndexerInterface
{
Expand Down Expand Up @@ -88,7 +87,7 @@ public function deindexDimensionContent(DimensionContentInterface $dimensionCont
return;
}

$this->searchManager->deindex($dimensionContent, $dimensionContent->getDimension()->getLocale());
$this->searchManager->deindex($dimensionContent, $dimensionContent->getLocale());
}

/**
Expand All @@ -107,8 +106,8 @@ private function loadDimensionContent(

$dimensionContent = $this->contentResolver->resolve($contentRichEntity, $dimensionAttributes);

if ($locale !== $dimensionContent->getDimension()->getLocale()
|| $stage !== $dimensionContent->getDimension()->getStage()) {
if ($locale !== $dimensionContent->getLocale()
|| $stage !== $dimensionContent->getStage()) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
}

Expand All @@ -127,11 +126,11 @@ function ($indexName) use ($resourceKey, $stage) {
return $resourceKey === $indexName || $resourceKey . '_published' === $indexName;
}

if (DimensionInterface::STAGE_DRAFT === $stage) {
if (DimensionContentInterface::STAGE_DRAFT === $stage) {
return $resourceKey === $indexName;
}

if (DimensionInterface::STAGE_LIVE === $stage) {
if (DimensionContentInterface::STAGE_LIVE === $stage) {
return $resourceKey . '_published' === $indexName;
}

Expand Down
35 changes: 24 additions & 11 deletions Content/Application/ContentMerger/ContentMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentMerger\Merger\MergerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class ContentMerger implements ContentMergerInterface
{
Expand All @@ -24,35 +25,47 @@ class ContentMerger implements ContentMergerInterface
*/
private $mergers;

/**
* @var PropertyAccessor
*/
private $propertyAccessor;

/**
* @param iterable<MergerInterface> $mergers
*/
public function __construct(iterable $mergers)
{
public function __construct(
iterable $mergers,
PropertyAccessor $propertyAccessor
) {
$this->mergers = $mergers;
$this->propertyAccessor = $propertyAccessor;
}

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

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

/** @var DimensionContentInterface[] $dimensionContentCollectionArray */
$dimensionContentCollectionArray = iterator_to_array($dimensionContentCollection);
$lastKey = \count($dimensionContentCollectionArray) - 1;

$mostSpecificDimensionContent = $dimensionContentCollectionArray[$lastKey];
$mostSpecificDimension = $mostSpecificDimensionContent->getDimension();
$contentRichEntity = $mostSpecificDimensionContent->getResource();
$contentRichEntity = $unlocalizedDimensionContent->getResource();

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

foreach ($dimensionContentCollection as $dimensionContent) {
foreach ($this->mergers as $merger) {
$merger->merge($mergedDimensionContent, $dimensionContent);
}

foreach ($dimensionContentCollection->getDimensionAttributes() as $key => $value) {
$this->propertyAccessor->setValue(
$mergedDimensionContent,
$key,
$this->propertyAccessor->getValue($dimensionContent, $key)
);
}
}

return $mergedDimensionContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function enhance(object $object, array $normalizedData): array
}

$normalizedData['id'] = $object->getResource()->getId();
$normalizedData['locale'] = $object->getDimension()->getLocale();
$normalizedData['stage'] = $object->getDimension()->getStage();
$normalizedData['locale'] = $object->getLocale();
$normalizedData['stage'] = $object->getStage();

return $normalizedData;
}
Expand Down
11 changes: 1 addition & 10 deletions Content/Application/ContentPersister/ContentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,12 @@
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentPersister;

use Sulu\Bundle\ContentBundle\Content\Application\ContentMerger\ContentMergerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\DimensionCollectionFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\DimensionContentCollectionFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class ContentPersister implements ContentPersisterInterface
{
/**
* @var DimensionCollectionFactoryInterface
*/
private $dimensionCollectionFactory;

/**
* @var DimensionContentCollectionFactoryInterface
*/
Expand All @@ -37,11 +31,9 @@ class ContentPersister implements ContentPersisterInterface
private $contentMerger;

public function __construct(
DimensionCollectionFactoryInterface $dimensionCollectionFactory,
DimensionContentCollectionFactoryInterface $dimensionContentCollectionFactory,
ContentMergerInterface $contentMerger
) {
$this->dimensionCollectionFactory = $dimensionCollectionFactory;
$this->dimensionContentCollectionFactory = $dimensionContentCollectionFactory;
$this->contentMerger = $contentMerger;
}
Expand All @@ -56,10 +48,9 @@ public function persist(ContentRichEntityInterface $contentRichEntity, array $da
* TODO: maybe throw an exception here if the $dimensionAttributes contain another stage than 'STAGE_DRAFT'
*/

$dimensionCollection = $this->dimensionCollectionFactory->create($dimensionAttributes);
$dimensionContentCollection = $this->dimensionContentCollectionFactory->create(
$contentRichEntity,
$dimensionCollection,
$dimensionAttributes,
$data
);

Expand Down
16 changes: 1 addition & 15 deletions Content/Application/ContentResolver/ContentResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionRepositoryInterface;

class ContentResolver implements ContentResolverInterface
{
/**
* @var DimensionRepositoryInterface
*/
private $dimensionRepository;

/**
* @var DimensionContentRepositoryInterface
*/
Expand All @@ -38,24 +32,16 @@ class ContentResolver implements ContentResolverInterface
private $contentMerger;

public function __construct(
DimensionRepositoryInterface $dimensionRepository,
DimensionContentRepositoryInterface $dimensionContentRepository,
ContentMergerInterface $contentMerger
) {
$this->dimensionRepository = $dimensionRepository;
$this->dimensionContentRepository = $dimensionContentRepository;
$this->contentMerger = $contentMerger;
}

public function resolve(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface
{
$dimensionCollection = $this->dimensionRepository->findByAttributes($dimensionAttributes);

if (0 === \count($dimensionCollection)) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
}

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

if (0 === \count($dimensionContentCollection)) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
Expand Down
17 changes: 2 additions & 15 deletions Content/Application/ContentWorkflow/ContentWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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 Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Workflow\DefinitionBuilder;
Expand All @@ -36,11 +35,6 @@

class ContentWorkflow implements ContentWorkflowInterface
{
/**
* @var DimensionRepositoryInterface
*/
private $dimensionRepository;

/**
* @var DimensionContentRepositoryInterface
*/
Expand All @@ -62,13 +56,11 @@ class ContentWorkflow implements ContentWorkflowInterface
private $workflowRegistry;

public function __construct(
DimensionRepositoryInterface $dimensionRepository,
DimensionContentRepositoryInterface $dimensionContentRepository,
ContentMergerInterface $contentMerger,
?Registry $workflowRegistry = null,
?EventDispatcherInterface $eventDispatcher = null
) {
$this->dimensionRepository = $dimensionRepository;
$this->dimensionContentRepository = $dimensionContentRepository;
$this->contentMerger = $contentMerger;
$this->eventDispatcher = $eventDispatcher ?: new EventDispatcher();
Expand All @@ -93,13 +85,8 @@ public function apply(
* TODO: maybe throw an exception here if the $dimensionAttributes contain another stage than 'STAGE_DRAFT'
*/

$dimensionCollection = $this->dimensionRepository->findByAttributes($dimensionAttributes);

if (0 === \count($dimensionCollection)) {
throw new ContentNotFoundException($contentRichEntity, $dimensionAttributes);
}

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

$localizedDimensionContent = $dimensionContentCollection->getLocalizedDimensionContent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;
Expand Down Expand Up @@ -67,7 +66,7 @@ public function onPublish(TransitionEvent $transitionEvent): void
throw new \RuntimeException('No "contentRichEntity" given.');
}

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

$this->contentCopier->copyFromDimensionContentCollection(
$dimensionContentCollection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\ContentWorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;
Expand Down Expand Up @@ -52,8 +51,8 @@ public function onRemoveDraft(TransitionEvent $transitionEvent): void
throw new \RuntimeException('Transition context must contain "contentRichEntity".');
}

$draftDimensionAttributes = array_merge($dimensionAttributes, ['stage' => DimensionInterface::STAGE_DRAFT]);
$liveDimensionAttributes = array_merge($dimensionAttributes, ['stage' => DimensionInterface::STAGE_LIVE]);
$draftDimensionAttributes = array_merge($dimensionAttributes, ['stage' => DimensionContentInterface::STAGE_DRAFT]);
$liveDimensionAttributes = array_merge($dimensionAttributes, ['stage' => DimensionContentInterface::STAGE_LIVE]);

$this->contentCopier->copy(
$contentRichEntity,
Expand Down
Loading

0 comments on commit 29afa52

Please sign in to comment.