Skip to content

Commit

Permalink
Revert "Add messenger=persist (#3617)"
Browse files Browse the repository at this point in the history
This reverts commit ce06420.
  • Loading branch information
soyuka committed Dec 17, 2020
1 parent 0a6b837 commit bb0a025
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 451 deletions.
51 changes: 0 additions & 51 deletions features/doctrine/messenger.feature

This file was deleted.

Expand Up @@ -54,7 +54,6 @@ public function supports($data, array $context = []): bool
public function persist($data, array $context = [])
{
$this->tracePersisters($data, $context);

return $this->decorated->persist($data, $context);
}

Expand All @@ -64,7 +63,6 @@ public function persist($data, array $context = [])
public function remove($data, array $context = [])
{
$this->tracePersisters($data, $context);

return $this->decorated->persist($data, $context);
}

Expand Down
1 change: 0 additions & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/messenger.xml
Expand Up @@ -10,7 +10,6 @@
<service id="api_platform.messenger.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister" public="false">
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.message_bus" />
<argument type="service" id="api_platform.data_persister" />

<tag name="api_platform.data_persister" priority="-900" />
</service>
Expand Down
69 changes: 16 additions & 53 deletions src/Bridge/Symfony/Messenger/DataPersister.php
Expand Up @@ -17,7 +17,6 @@
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Core\Util\ClassInfoTrait;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -36,42 +35,46 @@ final class DataPersister implements ContextAwareDataPersisterInterface
use DispatchTrait;

private $resourceMetadataFactory;
private $dataPersister;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus, ContextAwareDataPersisterInterface $dataPersister)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->messageBus = $messageBus;
$this->dataPersister = $dataPersister;
}

/**
* {@inheritdoc}
*/
public function supports($data, array $context = []): bool
{
if (true === ($context['messenger_dispatched'] ?? false)) {
return false;
}

try {
$resourceMetadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
} catch (ResourceClassNotFoundException $e) {
return false;
}

return false !== $this->getMessengerAttributeValue($resourceMetadata, $context);
if (null !== $operationName = $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
return false !== $resourceMetadata->getTypedOperationAttribute(
$context['collection_operation_name'] ?? false ? OperationType::COLLECTION : OperationType::ITEM,
$operationName,
'messenger',
false,
true
);
}

if (isset($context['graphql_operation_name'])) {
return false !== $resourceMetadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', false, true);
}

return false !== $resourceMetadata->getAttribute('messenger', false);
}

/**
* {@inheritdoc}
*/
public function persist($data, array $context = [])
{
if ($this->handOver($data, $context)) {
$data = $this->dataPersister->persist($data, $context + ['messenger_dispatched' => true]);
}

$envelope = $this->dispatch(
(new Envelope($data))
->with(new ContextStamp($context))
Expand All @@ -90,49 +93,9 @@ public function persist($data, array $context = [])
*/
public function remove($data, array $context = [])
{
if ($this->handOver($data, $context)) {
$this->dataPersister->remove($data, $context + ['messenger_dispatched' => true]);
}

$this->dispatch(
(new Envelope($data))
->with(new RemoveStamp())
);
}

/**
* Should this DataPersister hand over in "persist" mode?
*/
private function handOver($data, array $context = []): bool
{
try {
$value = $this->getMessengerAttributeValue($this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data)), $context);
} catch (ResourceClassNotFoundException $exception) {
return false;
}

return 'persist' === $value || (\is_array($value) && (\in_array('persist', $value, true) || (true === $value['persist'] ?? false)));
}

/**
* @return bool|string|array|null
*/
private function getMessengerAttributeValue(ResourceMetadata $resourceMetadata, array $context = [])
{
if (null !== $operationName = $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
return $resourceMetadata->getTypedOperationAttribute(
$context['collection_operation_name'] ?? false ? OperationType::COLLECTION : OperationType::ITEM,
$operationName,
'messenger',
false,
true
);
}

if (isset($context['graphql_operation_name'])) {
return $resourceMetadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', false, true);
}

return $resourceMetadata->getAttribute('messenger', false);
}
}
24 changes: 12 additions & 12 deletions src/Bridge/Symfony/Messenger/DataTransformer.php
Expand Up @@ -55,19 +55,19 @@ public function supportsTransformation($data, string $to, array $context = []):
$metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);

if (isset($context['graphql_operation_name'])) {
$attribute = $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true);
} elseif (!isset($context['operation_type'])) {
$attribute = $metadata->getAttribute('messenger');
} else {
$attribute = $metadata->getTypedOperationAttribute(
$context['operation_type'],
$context[$context['operation_type'].'_operation_name'] ?? '',
'messenger',
null,
true
);
return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true);
}

return 'input' === $attribute || (\is_array($attribute) && \in_array('input', $attribute, true));
if (!isset($context['operation_type'])) {
return 'input' === $metadata->getAttribute('messenger');
}

return 'input' === $metadata->getTypedOperationAttribute(
$context['operation_type'],
$context[$context['operation_type'].'_operation_name'] ?? '',
'messenger',
null,
true
);
}
}
95 changes: 10 additions & 85 deletions tests/Bridge/Symfony/Messenger/DataPersisterTest.php
Expand Up @@ -16,7 +16,6 @@
use ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp;
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister;
use ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp;
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
Expand All @@ -27,6 +26,7 @@
use Prophecy\Argument;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -40,7 +40,7 @@ public function testSupport()
$metadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true]));

$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal(), $this->prophesize(ContextAwareDataPersisterInterface::class)->reveal());
$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal());
$this->assertTrue($dataPersister->supports(new Dummy()));
}

Expand All @@ -50,132 +50,57 @@ public function testSupportWithContext()
$metadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true]));
$metadataFactoryProphecy->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal(), $this->prophesize(ContextAwareDataPersisterInterface::class)->reveal());
$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal());
$this->assertTrue($dataPersister->supports(new DummyCar(), ['resource_class' => Dummy::class]));
$this->assertFalse($dataPersister->supports(new DummyCar()));
}

public function testSupportWithContextAndMessengerDispatched()
{
$metadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true]));
$metadataFactoryProphecy->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal(), $this->prophesize(ContextAwareDataPersisterInterface::class)->reveal());
$this->assertFalse($dataPersister->supports(new DummyCar(), ['resource_class' => Dummy::class, 'messenger_dispatched' => true]));
}

public function testPersist()
{
$dummy = new Dummy();

$metadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true]));
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => 'input']));
$metadataFactory->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$chainDataPersister = $this->prophesize(ContextAwareDataPersisterInterface::class);
$chainDataPersister->persist($dummy, ['messenger_dispatched' => true])->shouldNotBeCalled();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();

$dataPersister = new DataPersister($metadataFactory->reveal(), $messageBus->reveal(), $chainDataPersister->reveal());
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
$this->assertSame($dummy, $dataPersister->persist($dummy));
}

public function testRemove()
{
$dummy = new Dummy();

$metadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true]));
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => 'input']));
$metadataFactory->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$chainDataPersister = $this->prophesize(ContextAwareDataPersisterInterface::class);
$chainDataPersister->remove($dummy, ['messenger_dispatched' => true])->shouldNotBeCalled();

$messageBus = $this->prophesize(MessageBusInterface::class);

$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(RemoveStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();

$dataPersister = new DataPersister($metadataFactory->reveal(), $messageBus->reveal(), $chainDataPersister->reveal());
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
$dataPersister->remove($dummy);
}

public function testPersistWithHandOver()
public function testHandle()
{
$dummy = new Dummy();

$metadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => 'persist']));
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => ['persist', 'input']]));
$metadataFactory->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$chainDataPersister = $this->prophesize(ContextAwareDataPersisterInterface::class);
$chainDataPersister->persist($dummy, ['messenger_dispatched' => true])->willReturn($dummy)->shouldBeCalled();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();
}))->willReturn((new Envelope($dummy))->with(new HandledStamp($dummy, 'DummyHandler::__invoke')))->shouldBeCalled();

$dataPersister = new DataPersister($metadataFactory->reveal(), $messageBus->reveal(), $chainDataPersister->reveal());
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
$this->assertSame($dummy, $dataPersister->persist($dummy));
}

public function testPersistWithExceptionOnHandOver()
{
$dummy = new Dummy();

$metadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactory->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());
$metadataFactory->create(Dummy::class)->willThrow(new ResourceClassNotFoundException());
$metadataFactory->create(Dummy::class)->willThrow(new ResourceClassNotFoundException());

$chainDataPersister = $this->prophesize(ContextAwareDataPersisterInterface::class);
$chainDataPersister->persist(Argument::any(), Argument::any())->shouldNotBeCalled();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(ContextStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();

$dataPersister = new DataPersister($metadataFactory->reveal(), $messageBus->reveal(), $chainDataPersister->reveal());
$this->assertSame($dummy, $dataPersister->persist($dummy));
}

public function testRemoveWithHandOver()
{
$dummy = new Dummy();

$metadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => 'persist']));
$metadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => ['persist', 'input']]));
$metadataFactory->create(DummyCar::class)->willThrow(new ResourceClassNotFoundException());

$chainDataPersister = $this->prophesize(ContextAwareDataPersisterInterface::class);
$chainDataPersister->remove($dummy, ['messenger_dispatched' => true])->shouldBeCalled();

$messageBus = $this->prophesize(MessageBusInterface::class);
$messageBus->dispatch(Argument::that(function (Envelope $envelope) use ($dummy) {
return $dummy === $envelope->getMessage() && null !== $envelope->last(RemoveStamp::class);
}))->willReturn(new Envelope($dummy))->shouldBeCalled();

$dataPersister = new DataPersister($metadataFactory->reveal(), $messageBus->reveal(), $chainDataPersister->reveal());
$dataPersister->remove($dummy);
}

public function testSupportWithGraphqlContext()
{
$metadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
$metadataFactoryProphecy->create(Dummy::class)->willReturn((new ResourceMetadata(null, null, null, null, null, []))->withGraphQl(['create' => ['messenger' => 'input']]));

$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal(), $this->prophesize(ContextAwareDataPersisterInterface::class)->reveal());
$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal());
$this->assertTrue($dataPersister->supports(new DummyCar(), ['resource_class' => Dummy::class, 'graphql_operation_name' => 'create']));
}
}

0 comments on commit bb0a025

Please sign in to comment.