Skip to content

Commit

Permalink
Add teaser selection resolver (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-rath authored Apr 7, 2021
1 parent 76da5e9 commit 40215f2
Show file tree
Hide file tree
Showing 7 changed files with 640 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Content/ContentTypeResolver/TeaserSelectionResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?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\HeadlessBundle\Content\ContentTypeResolver;

use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\Serializer\TeaserSerializerInterface;
use Sulu\Bundle\PageBundle\Teaser\Teaser;
use Sulu\Bundle\PageBundle\Teaser\TeaserManagerInterface;
use Sulu\Component\Content\Compat\PropertyInterface;

class TeaserSelectionResolver implements ContentTypeResolverInterface
{
public static function getContentType(): string
{
return 'teaser_selection';
}

/**
* @var TeaserManagerInterface
*/
private $teaserManager;

/**
* @var TeaserSerializerInterface
*/
private $teaserSerializer;

public function __construct(TeaserManagerInterface $teaserManager, TeaserSerializerInterface $teaserSerializer)
{
$this->teaserManager = $teaserManager;
$this->teaserSerializer = $teaserSerializer;
}

public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
$value = array_merge(
[
'presentAs' => null,
'items' => [],
],
\is_array($data) ? $data : []
);
$items = $value['items'] ?? [];

if (!\is_array($items) || 0 === \count($items)) {
return new ContentView([], $value);
}

$teasers = $this->teaserManager->find($items, $locale);
$teasers = array_map(
function (Teaser $teaser) use ($locale) {
return $this->teaserSerializer->serialize($teaser, $locale);
},
$teasers
);

return new ContentView($teasers, $value);
}
}
103 changes: 103 additions & 0 deletions Content/Serializer/TeaserSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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\HeadlessBundle\Content\Serializer;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Bundle\PageBundle\Teaser\Teaser;
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreNotExistsException;
use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStorePoolInterface;
use Sulu\Component\Serializer\ArraySerializerInterface;

class TeaserSerializer implements TeaserSerializerInterface
{
/**
* @var ArraySerializerInterface
*/
private $arraySerializer;

/**
* @var MediaSerializerInterface
*/
private $mediaSerializer;

/**
* @var MediaManagerInterface
*/
private $mediaManager;

/**
* @var ReferenceStorePoolInterface
*/
private $referenceStorePool;

public function __construct(
ArraySerializerInterface $arraySerializer,
MediaSerializerInterface $mediaSerializer,
MediaManagerInterface $mediaManager,
ReferenceStorePoolInterface $referenceStorePool
) {
$this->arraySerializer = $arraySerializer;
$this->mediaSerializer = $mediaSerializer;
$this->mediaManager = $mediaManager;
$this->referenceStorePool = $referenceStorePool;
}

/**
* @return mixed[]
*/
public function serialize(Teaser $teaser, string $locale, ?SerializationContext $context = null): array
{
$teaserData = $this->arraySerializer->serialize($teaser, $context);
unset($teaserData['mediaId']);

$mediaId = $teaser->getMediaId();
$mediaData = null;
if ($mediaId) {
$media = $this->mediaManager->getEntityById($mediaId);
$mediaData = $this->mediaSerializer->serialize($media, $locale);
}

$teaserData['media'] = $mediaData;

$this->addToReferenceStore($teaser->getId(), $teaser->getType());

return $teaserData;
}

/**
* @param int|string $id
*/
private function addToReferenceStore($id, string $alias): void
{
if ('pages' === $alias) {
// unfortunately the reference store for pages was not adjusted and still uses content as alias
$alias = 'content';
}

if ('articles' === $alias) {
$alias = 'article';
}

try {
$referenceStore = $this->referenceStorePool->getStore($alias);
} catch (ReferenceStoreNotExistsException $e) {
// @ignoreException do nothing when reference store was not found

return;
}

$referenceStore->add($id);
}
}
25 changes: 25 additions & 0 deletions Content/Serializer/TeaserSerializerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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\HeadlessBundle\Content\Serializer;

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\PageBundle\Teaser\Teaser;

interface TeaserSerializerInterface
{
/**
* @return mixed[]
*/
public function serialize(Teaser $teaser, string $locale, ?SerializationContext $context = null): array;
}
11 changes: 11 additions & 0 deletions Resources/config/content-type-resolvers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
<tag name="sulu_headless.content_type_resolver"/>
</service>

<service
id="sulu_headless.content_resolver.teaser_selection"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\TeaserSelectionResolver"
lazy="true"
>
<argument type="service" id="sulu_page.teaser.manager"/>
<argument type="service" id="sulu_headless.serializer.teaser"/>

<tag name="sulu_headless.content_type_resolver"/>
</service>

<service
id="sulu_headless.content_resolver.text_editor"
class="Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\TextEditorResolver"
Expand Down
11 changes: 11 additions & 0 deletions Resources/config/serializers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,16 @@
<argument type="service" id="sulu_headless.serializer.media"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Content\Serializer\CategorySerializerInterface" alias="sulu_headless.serializer.category"/>

<service
id="sulu_headless.serializer.teaser"
class="Sulu\Bundle\HeadlessBundle\Content\Serializer\TeaserSerializer"
>
<argument type="service" id="sulu_core.array_serializer"/>
<argument type="service" id="sulu_headless.serializer.media"/>
<argument type="service" id="sulu_media.media_manager"/>
<argument type="service" id="sulu_website.reference_store_pool"/>
</service>
<service id="Sulu\Bundle\HeadlessBundle\Content\Serializer\TeaserSerializerInterface" alias="sulu_headless.serializer.teaser"/>
</services>
</container>
Loading

0 comments on commit 40215f2

Please sign in to comment.