vendor/api-platform/core/src/Core/Bridge/Symfony/Messenger/DataPersister.php line 45

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
  12. use ApiPlatform\Core\Api\OperationType;
  13. use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
  14. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  15. use ApiPlatform\Exception\OperationNotFoundException;
  16. use ApiPlatform\Exception\ResourceClassNotFoundException;
  17. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  18. use ApiPlatform\Symfony\Messenger\DispatchTrait;
  19. use ApiPlatform\Util\ClassInfoTrait;
  20. use Symfony\Component\Messenger\Envelope;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. use Symfony\Component\Messenger\Stamp\HandledStamp;
  23. /**
  24.  * Dispatches the given resource using the message bus of Symfony Messenger.
  25.  *
  26.  * @experimental
  27.  *
  28.  * @author Kévin Dunglas <dunglas@gmail.com>
  29.  */
  30. final class DataPersister implements ContextAwareDataPersisterInterface
  31. {
  32.     use ClassInfoTrait;
  33.     use DispatchTrait;
  34.     /**
  35.      * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
  36.      */
  37.     private $resourceMetadataFactory;
  38.     public function __construct($resourceMetadataFactoryMessageBusInterface $messageBus)
  39.     {
  40.         $this->resourceMetadataFactory $resourceMetadataFactory;
  41.         $this->messageBus $messageBus;
  42.         if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  43.             trigger_deprecation('api-platform/core''2.7'sprintf('Use "%s" instead of "%s".'ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  44.         }
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function supports($data, array $context = []): bool
  50.     {
  51.         if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  52.             try {
  53.                 $resourceMetadataCollection $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
  54.                 $operation $resourceMetadataCollection->getOperation($context['operation_name'] ?? null);
  55.                 return false !== ($operation->getMessenger() ?? false);
  56.             } catch (OperationNotFoundException $e) {
  57.                 return false;
  58.             }
  59.         }
  60.         try {
  61.             $resourceMetadata $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
  62.         } catch (ResourceClassNotFoundException $e) {
  63.             return false;
  64.         }
  65.         if (null !== $operationName $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
  66.             return false !== $resourceMetadata->getTypedOperationAttribute(
  67.                 $context['collection_operation_name'] ?? false OperationType::COLLECTION OperationType::ITEM,
  68.                 $operationName,
  69.                 'messenger',
  70.                 false,
  71.                 true
  72.             );
  73.         }
  74.         if (isset($context['graphql_operation_name'])) {
  75.             return false !== $resourceMetadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger'falsetrue);
  76.         }
  77.         return false !== $resourceMetadata->getAttribute('messenger'false);
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function persist($data, array $context = [])
  83.     {
  84.         $envelope $this->dispatch(
  85.             (new Envelope($data))
  86.                 ->with(new ContextStamp($context))
  87.         );
  88.         $handledStamp $envelope->last(HandledStamp::class);
  89.         if (!$handledStamp instanceof HandledStamp) {
  90.             return $data;
  91.         }
  92.         return $handledStamp->getResult();
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function remove($data, array $context = [])
  98.     {
  99.         $this->dispatch(
  100.             (new Envelope($data))
  101.                 ->with(new RemoveStamp())
  102.         );
  103.     }
  104. }