vendor/api-platform/core/src/Core/Bridge/Symfony/Messenger/DataTransformer.php line 42

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\DataTransformer\DataTransformerInterface;
  13. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  14. use ApiPlatform\Exception\OperationNotFoundException;
  15. use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
  16. use ApiPlatform\Util\ClassInfoTrait;
  17. /**
  18. * Transforms an Input to itself. This gives the ability to send the Input to a
  19. * message handler and process it asynchronously.
  20. *
  21. * @author Antoine Bluchet <soyuka@gmail.com>
  22. */
  23. final class DataTransformer implements DataTransformerInterface
  24. {
  25. use ClassInfoTrait;
  26. /**
  27. * @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
  28. */
  29. private $resourceMetadataFactory;
  30. public function __construct($resourceMetadataFactory)
  31. {
  32. $this->resourceMetadataFactory = $resourceMetadataFactory;
  33. if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  34. trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
  35. }
  36. }
  37. /**
  38. * @return object
  39. */
  40. public function transform($object, string $to, array $context = [])
  41. {
  42. return $object;
  43. }
  44. public function supportsTransformation($data, string $to, array $context = []): bool
  45. {
  46. if (
  47. \is_object($data) // data is not normalized yet, it should be an array
  48. || null === ($context['input']['class'] ?? null)
  49. ) {
  50. return false;
  51. }
  52. if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
  53. try {
  54. $resourceMetadataCollection = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
  55. $operation = $resourceMetadataCollection->getOperation($context['operation_name'] ?? null);
  56. return 'input' === $operation->getMessenger();
  57. } catch (OperationNotFoundException $e) {
  58. return false;
  59. }
  60. }
  61. $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
  62. if (isset($context['graphql_operation_name'])) {
  63. return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true);
  64. }
  65. if (!isset($context['operation_type'])) {
  66. return 'input' === $metadata->getAttribute('messenger');
  67. }
  68. return 'input' === $metadata->getTypedOperationAttribute(
  69. $context['operation_type'],
  70. $context[$context['operation_type'].'_operation_name'] ?? '',
  71. 'messenger',
  72. null,
  73. true
  74. );
  75. }
  76. }