Skip to content

Commit

Permalink
Add ContentMetadataInspector service to provide correct target classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwestwerk authored Oct 9, 2020
1 parent 0fcecd7 commit 8913239
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Application\ContentMetadataInspector;

use Doctrine\ORM\EntityManagerInterface;

class ContentMetadataInspector implements ContentMetadataInspectorInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}

public function getDimensionContentClass(string $contentRichEntityClass): string
{
$classMetadata = $this->entityManager->getClassMetadata($contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');

return $associationMapping['targetEntity'];
}

public function getDimensionContentMappingProperty(string $contentRichEntityClass): string
{
$classMetadata = $this->entityManager->getClassMetadata($contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');

return $associationMapping['mappedBy'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\Application\ContentMetadataInspector;

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

interface ContentMetadataInspectorInterface
{
/**
* @param class-string<ContentRichEntityInterface> $contentRichEntityClass
*
* @return class-string<DimensionContentInterface>
*/
public function getDimensionContentClass(string $contentRichEntityClass): string;

/**
* @param class-string<ContentRichEntityInterface> $contentRichEntityClass
*/
public function getDimensionContentMappingProperty(string $contentRichEntityClass): string;
}
20 changes: 14 additions & 6 deletions Content/Infrastructure/Doctrine/DimensionContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollection;
Expand All @@ -28,25 +29,32 @@ class DimensionContentRepository implements DimensionContentRepositoryInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;

public function __construct(
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector
) {
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
}

public function load(
ContentRichEntityInterface $contentRichEntity,
DimensionCollectionInterface $dimensionCollection
): DimensionContentCollectionInterface {
$classMetadata = $this->entityManager->getClassMetadata(\get_class($contentRichEntity));
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');
$dimensionContentClass = $associationMapping['targetEntity'];
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentMappingProperty(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent')
->addSelect('dimension')
->innerJoin('dimensionContent.dimension', 'dimension')
->innerJoin('dimensionContent.' . $associationMapping['mappedBy'], 'content')
->innerJoin('dimensionContent.' . $mappingProperty, 'content')
->where('content.id = :id')
->setParameter('id', $contentRichEntity->getId());

Expand Down
29 changes: 7 additions & 22 deletions Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@

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

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\AdminBundle\Admin\View\DropdownToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\FormViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\PreviewFormViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
Expand All @@ -43,9 +41,9 @@ class ContentViewBuilderFactory implements ContentViewBuilderFactoryInterface
private $objectProviderRegistry;

/**
* @var EntityManagerInterface
* @var ContentMetadataInspectorInterface
*/
private $entityManager;
private $contentMetadataInspector;

/**
* @var SecurityCheckerInterface
Expand All @@ -55,19 +53,19 @@ class ContentViewBuilderFactory implements ContentViewBuilderFactoryInterface
public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory,
PreviewObjectProviderRegistryInterface $objectProviderRegistry,
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector,
SecurityCheckerInterface $securityChecker
) {
$this->viewBuilderFactory = $viewBuilderFactory;
$this->objectProviderRegistry = $objectProviderRegistry;
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->securityChecker = $securityChecker;
}

public function getDefaultToolbarActions(
string $contentRichEntityClass
): array {
$dimensionContentClass = $this->getDimensionContentClass($contentRichEntityClass);
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass($contentRichEntityClass);

$toolbarActions = [];

Expand Down Expand Up @@ -132,7 +130,7 @@ public function createViews(
?string $securityContext = null,
?array $toolbarActions = null
): array {
$dimensionContentClass = $this->getDimensionContentClass($contentRichEntityClass);
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass($contentRichEntityClass);

$resourceKey = $dimensionContentClass::getResourceKey();
$previewEnabled = $this->objectProviderRegistry->hasPreviewObjectProvider($resourceKey);
Expand Down Expand Up @@ -291,17 +289,4 @@ private function hasPermission(?string $securityContext, string $permissionType)

return $this->securityChecker->hasPermission($securityContext, $permissionType);
}

/**
* @param class-string<ContentRichEntityInterface> $contentRichEntityClass
*
* @return class-string<DimensionContentInterface>
*/
private function getDimensionContentClass(string $contentRichEntityClass): string
{
$classMetadata = $this->entityManager->getClassMetadata($contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');

return $associationMapping['targetEntity'];
}
}
12 changes: 9 additions & 3 deletions Content/Infrastructure/Sulu/Search/ContentReindexProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Massive\Bundle\SearchBundle\Search\Reindex\LocalizedReindexProviderInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
Expand All @@ -30,6 +31,11 @@ class ContentReindexProvider implements LocalizedReindexProviderInterface
*/
private $entityManager;

/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;

/**
* @var ContentResolverInterface
*/
Expand All @@ -55,11 +61,13 @@ class ContentReindexProvider implements LocalizedReindexProviderInterface
*/
public function __construct(
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector,
ContentResolverInterface $contentResolver,
string $context,
string $contentRichEntityClass
) {
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->contentResolver = $contentResolver;
$this->context = $context;
$this->contentRichEntityClass = $contentRichEntityClass;
Expand Down Expand Up @@ -173,9 +181,7 @@ private function getDimensionContentClass(): string
return $this->dimensionContentClass;
}

$classMetadata = $this->entityManager->getClassMetadata($this->contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');
$this->dimensionContentClass = $associationMapping['targetEntity'];
$this->dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass($this->contentRichEntityClass);

return $this->dimensionContentClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

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

use Doctrine\ORM\EntityManagerInterface;
use Massive\Bundle\SearchBundle\Search\Document;
use Massive\Bundle\SearchBundle\Search\Factory;
use Massive\Bundle\SearchBundle\Search\Metadata\ClassMetadata;
use Massive\Bundle\SearchBundle\Search\Metadata\ComplexMetadata;
use Massive\Bundle\SearchBundle\Search\Metadata\Field\Expression;
use Massive\Bundle\SearchBundle\Search\Metadata\IndexMetadata;
use Massive\Bundle\SearchBundle\Search\Metadata\ProviderInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
Expand Down Expand Up @@ -49,9 +49,9 @@ class ContentSearchMetadataProvider implements ProviderInterface
];

/**
* @var EntityManagerInterface
* @var ContentMetadataInspectorInterface
*/
private $entityManager;
private $contentMetadataInspector;

/**
* @var Factory
Expand All @@ -77,12 +77,12 @@ class ContentSearchMetadataProvider implements ProviderInterface
* @param class-string<ContentRichEntityInterface> $contentRichEntityClass
*/
public function __construct(
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector,
Factory $searchMetadataFactory,
StructureMetadataFactoryInterface $structureFactory,
string $contentRichEntityClass
) {
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->searchMetadataFactory = $searchMetadataFactory;
$this->structureFactory = $structureFactory;
$this->contentRichEntityClass = $contentRichEntityClass;
Expand Down Expand Up @@ -366,9 +366,7 @@ private function getDimensionContentClass(): string
return $this->dimensionContentClass;
}

$classMetadata = $this->entityManager->getClassMetadata($this->contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');
$this->dimensionContentClass = $associationMapping['targetEntity'];
$this->dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass($this->contentRichEntityClass);

return $this->dimensionContentClass;
}
Expand Down
11 changes: 9 additions & 2 deletions Content/Infrastructure/Sulu/Teaser/ContentTeaserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\ContentWorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
Expand All @@ -40,6 +41,11 @@ abstract class ContentTeaserProvider implements TeaserProviderInterface
*/
protected $entityManager;

/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;

/**
* @var StructureMetadataFactoryInterface
*/
Expand All @@ -56,11 +62,13 @@ abstract class ContentTeaserProvider implements TeaserProviderInterface
public function __construct(
ContentManagerInterface $contentManager,
EntityManagerInterface $entityManager,
ContentMetadataInspectorInterface $contentMetadataInspector,
StructureMetadataFactoryInterface $metadataFactory,
string $contentRichEntityClass
) {
$this->contentManager = $contentManager;
$this->entityManager = $entityManager;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->metadataFactory = $metadataFactory;
$this->contentRichEntityClass = $contentRichEntityClass;
}
Expand Down Expand Up @@ -297,8 +305,7 @@ protected function getEntityIdField(): string

protected function getResourceKey(): string
{
$classMetadata = $this->entityManager->getClassMetadata($this->contentRichEntityClass);
$dimensionContentClass = $classMetadata->getAssociationMapping('dimensionContents')['targetEntity'];
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass($this->contentRichEntityClass);

return $dimensionContentClass::getResourceKey();
}
Expand Down
10 changes: 9 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<service id="sulu_content.content_view_builder_factory" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Admin\ContentViewBuilderFactory">
<argument type="service" id="sulu_admin.view_builder_factory"/>
<argument type="service" id="sulu_preview.preview_object_provider_registry"/>
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sulu_content.content_metadata_inspector"/>
<argument type="service" id="sulu_security.security_checker"/>

<tag name="sulu.context" context="admin"/>
Expand Down Expand Up @@ -57,9 +57,17 @@

<service id="Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\ContentDataMapperInterface" alias="sulu_content.content_data_mapper"/>

<!-- Content Metadata Inspector -->
<service id="sulu_content.content_metadata_inspector" class="Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspector">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>

<service id="Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface" alias="sulu_content.content_metadata_inspector"/>

<!-- Content Dimension Loader -->
<service id="sulu_content.dimension_content_repository" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentRepository">
<argument type="service" id="doctrine.orm.entity_manager"/>
<argument type="service" id="sulu_content.content_metadata_inspector"/>
</service>

<service id="Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface" alias="sulu_content.dimension_content_repository"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ services:
arguments:
- '@sulu_content.content_manager'
- '@doctrine.orm.entity_manager'
- '@sulu_content.content_metadata_inspector'
- '@sulu_page.structure.factory'
- '@translator'
tags:
Expand Down Expand Up @@ -72,6 +73,7 @@ services:
class: Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Search\ContentReindexProvider
arguments:
- '@doctrine.orm.entity_manager'
- '@sulu_content.content_metadata_inspector'
- '@sulu_content.content_resolver'
- '%sulu.context%'
- Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example
Expand All @@ -81,7 +83,7 @@ services:
example_test.example_search_metadata_provider:
class: Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Search\ContentSearchMetadataProvider
arguments:
- '@doctrine.orm.entity_manager'
- '@sulu_content.content_metadata_inspector'
- '@massive_search.factory_default'
- '@sulu_page.structure.factory'
- Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example
Expand Down
Loading

0 comments on commit 8913239

Please sign in to comment.