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.     public function supports($data, array $context = []): bool
  47.     {
  48.         if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  49.             try {
  50.                 $resourceMetadataCollection $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
  51.                 $operation $resourceMetadataCollection->getOperation($context['operation_name'] ?? null);
  52.                 return false !== ($operation->getMessenger() ?? false);
  53.             } catch (OperationNotFoundException $e) {
  54.                 return false;
  55.             }
  56.         }
  57.         try {
  58.             $resourceMetadata $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
  59.         } catch (ResourceClassNotFoundException $e) {
  60.             return false;
  61.         }
  62.         if (null !== $operationName $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
  63.             return false !== $resourceMetadata->getTypedOperationAttribute(
  64.                 $context['collection_operation_name'] ?? false OperationType::COLLECTION OperationType::ITEM,
  65.                 $operationName,
  66.                 'messenger',
  67.                 false,
  68.                 true
  69.             );
  70.         }
  71.         if (isset($context['graphql_operation_name'])) {
  72.             return false !== $resourceMetadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger'falsetrue);
  73.         }
  74.         return false !== $resourceMetadata->getAttribute('messenger'false);
  75.     }
  76.     public function persist($data, array $context = [])
  77.     {
  78.         $envelope $this->dispatch(
  79.             (new Envelope($data))
  80.                 ->with(new ContextStamp($context))
  81.         );
  82.         $handledStamp $envelope->last(HandledStamp::class);
  83.         if (!$handledStamp instanceof HandledStamp) {
  84.             return $data;
  85.         }
  86.         return $handledStamp->getResult();
  87.     }
  88.     public function remove($data, array $context = [])
  89.     {
  90.         $this->dispatch(
  91.             (new Envelope($data))
  92.                 ->with(new RemoveStamp())
  93.         );
  94.     }
  95. }