Skip to content

Commit

Permalink
Enhance ContentObjectProvider (#102)
Browse files Browse the repository at this point in the history
* enhance contentObjectProvider

* fix review issues

* fix phpstan

* Remove unnecessary constructor arguments of ContentObjectProvider class

Co-authored-by: Niklas Natter <niklas@sulu.io>
  • Loading branch information
luca-rath and niklasnatter committed Mar 25, 2020
1 parent c0f6506 commit c69ab4f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 308 deletions.
233 changes: 7 additions & 226 deletions Content/Infrastructure/Sulu/Preview/ContentObjectProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NoResultException;
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\ContentDataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Exception\ContentNotFoundException;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentProjectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderInterface;
use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Webmozart\Assert\Assert;

class ContentObjectProvider implements PreviewObjectProviderInterface
{
Expand All @@ -37,30 +31,15 @@ class ContentObjectProvider implements PreviewObjectProviderInterface
*/
private $entityManager;

/**
* @var StructureMetadataFactoryInterface
*/
private $structureMetadataFactory;

/**
* @var LegacyPropertyFactory
*/
private $propertyFactory;

/**
* @var ContentResolverInterface
*/
private $contentResolver;

/**
* @var TagFactoryInterface
*/
private $tagFactory;

/**
* @var CategoryFactoryInterface
* @var ContentDataMapperInterface
*/
private $categoryFactory;
private $contentDataMapper;

/**
* @var string
Expand All @@ -69,19 +48,13 @@ class ContentObjectProvider implements PreviewObjectProviderInterface

public function __construct(
EntityManagerInterface $entityManager,
StructureMetadataFactoryInterface $structureMetadataFactory,
LegacyPropertyFactory $propertyFactory,
ContentResolverInterface $contentResolver,
TagFactoryInterface $tagFactory,
CategoryFactoryInterface $categoryFactory,
ContentDataMapperInterface $contentDataMapper,
string $entityClass
) {
$this->entityManager = $entityManager;
$this->structureMetadataFactory = $structureMetadataFactory;
$this->propertyFactory = $propertyFactory;
$this->contentResolver = $contentResolver;
$this->tagFactory = $tagFactory;
$this->categoryFactory = $categoryFactory;
$this->contentDataMapper = $contentDataMapper;
$this->entityClass = $entityClass;
}

Expand Down Expand Up @@ -123,207 +96,15 @@ public function getId($object)
*/
public function setValues($object, $locale, array $data): void
{
// TODO: use ContentDataMapper service for setting data to the object

if ($object instanceof SeoInterface) {
$this->setSeoData($object, $data);
}

if ($object instanceof ExcerptInterface) {
$this->setExcerptData($object, $data);
}

if ($object instanceof TemplateInterface) {
$this->setTemplateData($object, $data);
}
}

/**
* @param mixed[] $data
*/
private function setSeoData(SeoInterface $object, array &$data): void
{
$seoTitle = 'seoTitle';
if (\array_key_exists($seoTitle, $data)) {
$value = $data[$seoTitle];

Assert::nullOrString($value);

$object->setSeoTitle($value);

unset($data[$seoTitle]);
}

$seoDescription = 'seoDescription';
if (\array_key_exists($seoDescription, $data)) {
$value = $data[$seoDescription];

Assert::nullOrString($value);

$object->setSeoDescription($value);

unset($data[$seoDescription]);
}

$seoKeywords = 'seoKeywords';
if (\array_key_exists($seoKeywords, $data)) {
$value = $data[$seoKeywords];

Assert::nullOrString($value);

$object->setSeoKeywords($value);

unset($data[$seoKeywords]);
}

$seoCanonicalUrl = 'seoCanonicalUrl';
if (\array_key_exists($seoCanonicalUrl, $data)) {
$value = $data[$seoCanonicalUrl];

Assert::nullOrString($value);

$object->setSeoCanonicalUrl($value);

unset($data[$seoCanonicalUrl]);
}

$seoNoIndex = 'seoNoIndex';
if (\array_key_exists($seoNoIndex, $data)) {
$value = $data[$seoNoIndex];

Assert::boolean($value);

$object->setSeoNoIndex($value);

unset($data[$seoNoIndex]);
}

$seoNoFollow = 'seoNoFollow';
if (\array_key_exists($seoNoFollow, $data)) {
$value = $data[$seoNoFollow];

Assert::boolean($value);

$object->setSeoNoFollow($value);

unset($data[$seoNoFollow]);
}

$seoHideInSitemap = 'seoHideInSitemap';
if (\array_key_exists($seoHideInSitemap, $data)) {
$value = $data[$seoHideInSitemap];

Assert::boolean($value);

$object->setSeoHideInSitemap($value);

unset($data[$seoHideInSitemap]);
}
}

/**
* @param mixed[] $data
*/
private function setExcerptData(ExcerptInterface $object, array &$data): void
{
$excerptTitle = 'excerptTitle';
if (\array_key_exists($excerptTitle, $data)) {
$value = $data[$excerptTitle];

Assert::nullOrString($value);

$object->setExcerptTitle($value);

unset($data[$excerptTitle]);
}

$excerptDescription = 'excerptDescription';
if (\array_key_exists($excerptDescription, $data)) {
$value = $data[$excerptDescription];

Assert::nullOrString($value);

$object->setExcerptDescription($value);

unset($data[$excerptDescription]);
}

$excerptMore = 'excerptMore';
if (\array_key_exists($excerptMore, $data)) {
$value = $data[$excerptMore];

Assert::nullOrString($value);

$object->setExcerptMore($value);

unset($data[$excerptMore]);
}

$excerptCategories = 'excerptCategories';
if (\array_key_exists($excerptCategories, $data)) {
$value = $data[$excerptCategories];

Assert::isArray($value);

$categories = $this->categoryFactory->create($value);

$object->setExcerptCategories($categories);

unset($data[$excerptCategories]);
}

$excerptTags = 'excerptTags';
if (\array_key_exists($excerptTags, $data)) {
$value = $data[$excerptTags];

Assert::isArray($value);

$tags = $this->tagFactory->create($value);

$object->setExcerptTags($tags);

unset($data[$excerptTags]);
}

$excerptImage = 'excerptImage';
if (\array_key_exists($excerptImage, $data)) {
$value = $data[$excerptImage];

Assert::nullOrIsArray($value);

$object->setExcerptImage($value);

unset($data[$excerptImage]);
}

$excerptIcon = 'excerptIcon';
if (\array_key_exists($excerptIcon, $data)) {
$value = $data[$excerptIcon];

Assert::nullOrIsArray($value);

$object->setExcerptIcon($value);

unset($data[$excerptIcon]);
}
}

/**
* @param mixed[] $data
*/
private function setTemplateData(TemplateInterface $object, array &$data): void
{
$object->setTemplateData($data);
$this->contentDataMapper->map($data, $object, $object);
}

/**
* @param ContentProjectionInterface $object
* @param string $locale
* @param mixed[] $context
*
* @return ContentProjectionInterface $object
*/
public function setContext($object, $locale, array $context)
public function setContext($object, $locale, array $context): ContentProjectionInterface
{
if ($object instanceof TemplateInterface) {
if (\array_key_exists('template', $context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@
use Sulu\Bundle\AdminBundle\Admin\View\PreviewFormViewBuilderInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactory;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\ContentDataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\ContentResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Admin\ContentViewBuilder;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Admin\ContentViewBuilderInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Sulu\Preview\ContentObjectProvider;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderInterface;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderRegistry;
use Sulu\Bundle\PreviewBundle\Preview\Object\PreviewObjectProviderRegistryInterface;
use Sulu\Component\Content\Compat\Structure\LegacyPropertyFactory;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

class ContentViewBuilderTest extends TestCase
{
Expand All @@ -54,20 +51,14 @@ protected function getPreviewObjectProviderRegistry(array $providers): PreviewOb

protected function getContentObjectProvider(
EntityManagerInterface $entityManager,
StructureMetadataFactoryInterface $structureMetadataFactory,
LegacyPropertyFactory $propertyFactory,
ContentResolverInterface $contentResolver,
TagFactoryInterface $tagFactory,
CategoryFactoryInterface $categoryFactory,
ContentDataMapperInterface $contentDataMapper,
string $entityClass
): ContentObjectProvider {
return new ContentObjectProvider(
$entityManager,
$structureMetadataFactory,
$propertyFactory,
$contentResolver,
$tagFactory,
$categoryFactory,
$contentDataMapper,
$entityClass
);
}
Expand Down Expand Up @@ -113,19 +104,13 @@ public function testBuild(): void
public function testBuildWithPreview(): void
{
$entityManager = $this->prophesize(EntityManagerInterface::class);
$structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class);
$propertyFactory = $this->prophesize(LegacyPropertyFactory::class);
$contentResolver = $this->prophesize(ContentResolverInterface::class);
$tagFactory = $this->prophesize(TagFactoryInterface::class);
$categoryFactory = $this->prophesize(CategoryFactoryInterface::class);
$contentDataMapper = $this->prophesize(ContentDataMapperInterface::class);

$contentObjectProvider = $this->getContentObjectProvider(
$entityManager->reveal(),
$structureMetadataFactory->reveal(),
$propertyFactory->reveal(),
$contentResolver->reveal(),
$tagFactory->reveal(),
$categoryFactory->reveal(),
$contentDataMapper->reveal(),
Example::class
);

Expand Down
Loading

0 comments on commit c69ab4f

Please sign in to comment.