Completed
Push — master ( d0bde7...345612 )
by Antoine
26s queued 11s
created

DataTransformer::supportsTransformation()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 3
nop 3
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
15
16
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
19
/**
20
 * Transforms an input that implements the InputMessage interface
21
 * to itself. This gives the ability to send the Input to a
22
 * message handler and process it asynchronously.
23
 *
24
 * @author Antoine Bluchet <[email protected]>
25
 */
26
final class DataTransformer implements DataTransformerInterface
27
{
28
    private $resourceMetadataFactory;
29
30
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory)
31
    {
32
        $this->resourceMetadataFactory = $resourceMetadataFactory;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function transform($object, string $to, array $context = [])
39
    {
40
        return $object;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function supportsTransformation($data, string $to, array $context = []): bool
47
    {
48
        if (
49
            \is_object($data) // data is not normalized yet, it should be an array
50
            ||
51
            null === ($context['input']['class'] ?? null)
52
        ) {
53
            return false;
54
        }
55
56
        $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
57
58
        if (!isset($context['operation_type'])) {
59
            return 'input' === $metadata->getAttribute('messenger');
60
        }
61
62
        return 'input' === $metadata->getTypedOperationAttribute(
63
                                $context['operation_type'],
64
                                $context[$context['operation_type'].'_operation_name'] ?? '',
65
                                'messenger',
66
                                null,
67
                                true
68
                            );
69
    }
70
}
71