Skip to content

Commit

Permalink
Rename Mapper to DataMapper (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored and wachterjohannes committed Dec 17, 2019
1 parent a87c539 commit b2b0947
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper;
namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper;

interface MapperInterface
interface DataMapperInterface
{
/**
* @param array<string, mixed> $data
*/
public function map(
array $data,
object $dimensionContent,
?object $localizedDimensionContent = null
object $unlocalizedObject,
?object $localizedObject = null
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper;
namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;

class ExcerptMapper implements MapperInterface
class ExcerptDataMapper implements DataMapperInterface
{
/**
* @var TagFactoryInterface
Expand All @@ -37,24 +37,24 @@ public function __construct(TagFactoryInterface $tagFactory, CategoryFactoryInte

public function map(
array $data,
object $dimensionContent,
?object $localizedDimensionContent = null
object $unlocalizedObject,
?object $localizedObject = null
): void {
if (!$dimensionContent instanceof ExcerptInterface) {
if (!$unlocalizedObject instanceof ExcerptInterface) {
return;
}

if ($localizedDimensionContent) {
if (!$localizedDimensionContent instanceof ExcerptInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', ExcerptInterface::class, \get_class($localizedDimensionContent)));
if ($localizedObject) {
if (!$localizedObject instanceof ExcerptInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', ExcerptInterface::class, \get_class($localizedObject)));
}

$this->setExcerptData($localizedDimensionContent, $data);
$this->setExcerptData($localizedObject, $data);

return;
}

$this->setExcerptData($dimensionContent, $data);
$this->setExcerptData($unlocalizedObject, $data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper;
namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
Expand All @@ -21,7 +21,7 @@
use Sulu\Component\Content\Metadata\PropertyMetadata;
use Sulu\Component\Content\Metadata\StructureMetadata;

class RouteMapper implements MapperInterface
class RouteDataMapper implements DataMapperInterface
{
/**
* @var StructureMetadataFactoryInterface
Expand Down Expand Up @@ -50,23 +50,23 @@ public function __construct(

public function map(
array $data,
object $dimensionContent,
?object $localizedDimensionContent = null
object $unlocalizedObject,
?object $localizedObject = null
): void {
if (!$localizedDimensionContent || !$localizedDimensionContent instanceof RoutableInterface) {
if (!$localizedObject || !$localizedObject instanceof RoutableInterface) {
return;
}

if (!$localizedDimensionContent instanceof TemplateInterface) {
throw new \RuntimeException('ContentDimension needs to extend the TemplateInterface');
if (!$localizedObject instanceof TemplateInterface) {
throw new \RuntimeException('LocalizedObject needs to extend the TemplateInterface');
}

if (!isset($data['template'])) {
throw new \RuntimeException('Expected "template" to be set in the data array.');
}

$template = $data['template'];
$type = $localizedDimensionContent->getTemplateType();
$type = $localizedObject->getTemplateType();

$metadata = $this->factory->getStructureMetadata($type, $template);
if (!$metadata) {
Expand All @@ -78,21 +78,21 @@ public function map(
return;
}

if (!$localizedDimensionContent->getContentId()) {
if (!$localizedObject->getContentId()) {
// FIXME the code only works if the content-dimension is flushed once and has a valid id

return;
}

$locale = $localizedDimensionContent->getLocale();
$locale = $localizedObject->getLocale();
if (!$locale) {
return;
}

/** @var string $name */
$name = $property->getName();

$currentRoutePath = $localizedDimensionContent->getTemplateData()[$name] ?? null;
$currentRoutePath = $localizedObject->getTemplateData()[$name] ?? null;
if (!\array_key_exists($name, $data) && null !== $currentRoutePath) {
return;
}
Expand All @@ -101,25 +101,25 @@ public function map(
if (!$routePath) {
// FIXME this should be handled directly in the form - see pages as an example
$routePath = $this->routeGenerator->generate(
$localizedDimensionContent,
$localizedObject,
['route_schema' => '/{object.getTitle()}']
);

if ('/' === $routePath) {
return;
}

$localizedDimensionContent->setTemplateData(
$localizedObject->setTemplateData(
array_merge(
$localizedDimensionContent->getTemplateData(),
$localizedObject->getTemplateData(),
[$name => $routePath]
)
);
}

$this->routeManager->createOrUpdateByAttributes(
$localizedDimensionContent->getContentClass(),
(string) $localizedDimensionContent->getContentId(),
$localizedObject->getContentClass(),
(string) $localizedObject->getContentId(),
$locale,
$routePath
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper;
namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;

class SeoMapper implements MapperInterface
class SeoDataMapper implements DataMapperInterface
{
public function map(
array $data,
object $dimensionContent,
?object $localizedDimensionContent = null
object $unlocalizedObject,
?object $localizedObject = null
): void {
if (!$dimensionContent instanceof SeoInterface) {
if (!$unlocalizedObject instanceof SeoInterface) {
return;
}

if ($localizedDimensionContent) {
if (!$localizedDimensionContent instanceof SeoInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', SeoInterface::class, \get_class($localizedDimensionContent)));
if ($localizedObject) {
if (!$localizedObject instanceof SeoInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', SeoInterface::class, \get_class($localizedObject)));
}

$this->setSeoData($localizedDimensionContent, $data);
$this->setSeoData($localizedObject, $data);

return;
}

$this->setSeoData($dimensionContent, $data);
$this->setSeoData($unlocalizedObject, $data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper;
namespace Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;

class TemplateMapper implements MapperInterface
class TemplateDataMapper implements DataMapperInterface
{
/**
* @var StructureMetadataFactoryInterface
Expand All @@ -30,10 +30,10 @@ public function __construct(StructureMetadataFactoryInterface $factory)

public function map(
array $data,
object $dimensionContent,
?object $localizedDimensionContent = null
object $unlocalizedObject,
?object $localizedObject = null
): void {
if (!$dimensionContent instanceof TemplateInterface) {
if (!$unlocalizedObject instanceof TemplateInterface) {
return;
}

Expand All @@ -45,27 +45,27 @@ public function map(

list($unlocalizedData, $localizedData) = $this->getTemplateData(
$data,
$dimensionContent->getTemplateType(),
$unlocalizedObject->getTemplateType(),
$template
);

if ($localizedDimensionContent) {
if (!$localizedDimensionContent instanceof TemplateInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', TemplateInterface::class, \get_class($localizedDimensionContent)));
if ($localizedObject) {
if (!$localizedObject instanceof TemplateInterface) {
throw new \RuntimeException(sprintf('Expected "$localizedDimensionContent" from type "%s" but "%s" given.', TemplateInterface::class, \get_class($localizedObject)));
}

$localizedDimensionContent->setTemplateKey($template);
$localizedDimensionContent->setTemplateData($localizedData);
$localizedObject->setTemplateKey($template);
$localizedObject->setTemplateData($localizedData);
}

if (!$localizedDimensionContent) {
if (!$localizedObject) {
// Only set templateKey to unlocalizedDimension when no localizedDimension exist
$dimensionContent->setTemplateKey($template);
$unlocalizedObject->setTemplateKey($template);
}

// Unlocalized dimensions can contain data of different templates so we need to merge them together
$dimensionContent->setTemplateData(array_merge(
$dimensionContent->getTemplateData(),
$unlocalizedObject->setTemplateData(array_merge(
$unlocalizedObject->getTemplateData(),
$unlocalizedData
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper\MapperInterface;
use Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\DataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\DimensionContentCollectionFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionCollectionInterface;
Expand All @@ -33,12 +33,12 @@ class DimensionContentCollectionFactory implements DimensionContentCollectionFac
private $dimensionContentRepository;

/**
* @var iterable<MapperInterface>
* @var iterable<DataMapperInterface>
*/
private $mappers;

/**
* @param iterable<MapperInterface> $mappers
* @param iterable<DataMapperInterface> $mappers
*/
public function __construct(DimensionContentRepositoryInterface $dimensionContentRepository, iterable $mappers)
{
Expand Down
18 changes: 9 additions & 9 deletions Resources/config/mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- Mapper -->
<service id="sulu_content.template_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper\TemplateMapper">
<!-- DataMapper -->
<service id="sulu_content.template_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\TemplateDataMapper">
<argument type="service" id="sulu_page.structure.factory"/>

<tag name="sulu_content.content_dimension_mapper"/>
<tag name="sulu_content.data_mapper"/>
</service>

<service id="sulu_content.seo_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper\SeoMapper">
<tag name="sulu_content.content_dimension_mapper"/>
<service id="sulu_content.seo_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\SeoDataMapper">
<tag name="sulu_content.data_mapper"/>
</service>

<service id="sulu_content.excerpt_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper\ExcerptMapper">
<service id="sulu_content.excerpt_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\ExcerptDataMapper">
<argument type="service" id="sulu_content.tag_factory"/>
<argument type="service" id="sulu_content.category_factory"/>

<tag name="sulu_content.content_dimension_mapper"/>
<tag name="sulu_content.data_mapper"/>
</service>

<service id="sulu_content.route_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\Mapper\RouteMapper">
<service id="sulu_content.route_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\RouteDataMapper">
<argument type="service" id="sulu_page.structure.factory"/>
<argument type="service" id="sulu_route.generator.route_generator"/>
<argument type="service" id="sulu_route.manager.route_manager"/>

<tag name="sulu_content.content_dimension_mapper"/>
<tag name="sulu_content.data_mapper"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<!-- Content Dimension Factory -->
<service id="sulu_content.dimension_content_collection_factory" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DimensionContentCollectionFactory">
<argument type="service" id="sulu_content.dimension_content_repository"/>
<argument type="tagged" tag="sulu_content.content_dimension_mapper"/>
<argument type="tagged" tag="sulu_content.data_mapper"/>
</service>

<service id="Sulu\Bundle\ContentBundle\Content\Domain\Factory\DimensionContentCollectionFactoryInterface" alias="sulu_content.dimension_content_collection_factory"/>
Expand Down
Loading

0 comments on commit b2b0947

Please sign in to comment.