Skip to content

Commit

Permalink
refactor #11819 [API] Refactored data transformers (lchrusciel)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.8 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         | 1.8
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | Following Sylius/Sylius#11745
| License         | MIT

I've adjusted service names to follow our conventions, removed unneeded classes, and typehinted variadic expression

Commits
-------

a19f1ac1a7f902e9183979fd414ef2ab3063ee96 [API] Adjust services names to common convention
4dbf19f7224992749c2ed3b14e43aba15866cd5c [API] Add variadic typehint
e878b1935ac351ac0214a50060b940abd43f4a8e [API] Refactored CommandAwareInputDataTransformer
  • Loading branch information
lchrusciel authored Sep 9, 2020
2 parents 3f6427a + 742d912 commit d72d5c4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 75 deletions.
11 changes: 5 additions & 6 deletions DataTransformer/CommandAwareInputDataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,17 @@
/** @experimental */
final class CommandAwareInputDataTransformer implements DataTransformerInterface
{
/** @var CommandDataTransformersChainInterface */
private $commandDataTransformersChain;
/** @var CommandDataTransformerInterface[] */
private $commandDataTransformers;

public function __construct(CommandDataTransformersChainInterface $commandDataTransformersChain)
public function __construct(CommandDataTransformerInterface ...$commandDataTransformers)
{
$this->commandDataTransformersChain = $commandDataTransformersChain;
$this->commandDataTransformers = $commandDataTransformers;
}

public function transform($object, string $to, array $context = [])
{
/** @var CommandDataTransformerInterface $transformer */
foreach ($this->commandDataTransformersChain->getCommandDataTransformers() as $transformer) {
foreach ($this->commandDataTransformers as $transformer) {
if ($transformer->supportsTransformation($object)) {
$object = $transformer->transform($object, $to, $context);
}
Expand Down
31 changes: 0 additions & 31 deletions DataTransformer/CommandDataTransformersChain.php

This file was deleted.

20 changes: 0 additions & 20 deletions DataTransformer/CommandDataTransformersChainInterface.php

This file was deleted.

8 changes: 5 additions & 3 deletions DependencyInjection/Compiler/CommandDataTransformerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sylius\Bundle\ApiBundle\DependencyInjection\Compiler;

use Sylius\Bundle\ApiBundle\DataTransformer\CommandDataTransformersChain;
use Sylius\Bundle\ApiBundle\DataTransformer\CommandAwareInputDataTransformer;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -24,16 +24,18 @@ final class CommandDataTransformerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$commandDataTransformersChainDefinition = new Definition(CommandDataTransformersChain::class);
$commandDataTransformersChainDefinition = new Definition(CommandAwareInputDataTransformer::class);

$taggedServices = $container->findTaggedServiceIds('sylius.api.command_data_transformer');

foreach ($taggedServices as $key => $value) {
$commandDataTransformersChainDefinition->addArgument(new Reference($key));
}

$commandDataTransformersChainDefinition->addTag('api_platform.data_transformer');

$container->setDefinition(
'sylius_api.command_data_transformers_chain',
'sylius.api.data_transformer.command_aware_input_data_transformer',
$commandDataTransformersChainDefinition
);
}
Expand Down
9 changes: 2 additions & 7 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,11 @@
<tag name="property_info.list_extractor" priority="-2000" />
</service>

<service class="Sylius\Bundle\ApiBundle\DataTransformer\CommandAwareInputDataTransformer" id="sylius_bundle_api.data_transformer.add_item_to_cart_input_data_transformer">
<argument type="service" id="sylius_api.command_data_transformers_chain" />
<tag name="api_platform.data_transformer" />
</service>

<service class="Sylius\Bundle\ApiBundle\DataTransformer\OrderTokenValueAwareInputCommandDataTransformer" id="sylius_bundle_api.data_transformer.order_token_value_aware_input_data_transformer">
<service id="sylius.api.data_transformer.order_token_value_aware_input_data_transformer" class="Sylius\Bundle\ApiBundle\DataTransformer\OrderTokenValueAwareInputCommandDataTransformer">
<tag name="sylius.api.command_data_transformer" />
</service>

<service class="Sylius\Bundle\ApiBundle\DataTransformer\SubresourceIdAwareCommandDataTransformer" id="sylius_bundle_api.data_transformer.subresource_id_aware_data_transformer">
<service id="sylius.api.data_transformer.subresource_id_aware_data_transformer" class="Sylius\Bundle\ApiBundle\DataTransformer\SubresourceIdAwareCommandDataTransformer">
<argument type="service" id="request_stack" />
<tag name="sylius.api.command_data_transformer" />
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ public function it_collects_tagged_command_data_transformer_services(): void

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'sylius.api.command_data_transformer.service.first',
'sylius.api.command_data_transformer'
$this->assertContainerBuilderHasServiceDefinitionWithArgument(
'sylius.api.data_transformer.command_aware_input_data_transformer',
0,
'sylius.api.command_data_transformer.service.first'
);

$this->assertContainerBuilderHasServiceDefinitionWithTag(
'sylius.api.command_data_transformer.service.second',
'sylius.api.command_data_transformer'
$this->assertContainerBuilderHasServiceDefinitionWithArgument(
'sylius.api.data_transformer.command_aware_input_data_transformer',
1,
'sylius.api.command_data_transformer.service.second'
);
}

/**
* @test
*/
public function it_adds_command_data_transformer_chain_services(): void
public function it_creates_definition_without_any_transformers(): void
{
$this->compile();

$this->assertContainerBuilderHasService('sylius_api.command_data_transformers_chain');
$this->assertContainerBuilderHasService('sylius.api.data_transformer.command_aware_input_data_transformer');
}

protected function registerCompilerPass(ContainerBuilder $container): void
Expand Down

0 comments on commit d72d5c4

Please sign in to comment.