Passed
Pull Request — master (#2428)
by Vincent
03:30
created

DataPersister   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A remove() 0 3 1
A supports() 0 23 5
A persist() 0 8 2
A removeElementFromCollection() 0 3 1
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\Api\OperationType;
17
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
18
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20
use ApiPlatform\Core\Util\ClassInfoTrait;
21
use Symfony\Component\Messenger\Envelope;
22
use Symfony\Component\Messenger\MessageBusInterface;
23
use Symfony\Component\Messenger\Stamp\HandledStamp;
24
25
/**
26
 * Dispatches the given resource using the message bus of Symfony Messenger.
27
 *
28
 * @experimental
29
 *
30
 * @author Kévin Dunglas <[email protected]>
31
 */
32
final class DataPersister implements ContextAwareDataPersisterInterface
33
{
34
    use ClassInfoTrait;
35
36
    private $resourceMetadataFactory;
37
    private $messageBus;
38
39
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
40
    {
41
        $this->resourceMetadataFactory = $resourceMetadataFactory;
42
        $this->messageBus = $messageBus;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supports($data, array $context = []): bool
49
    {
50
        try {
51
            $resourceMetadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $this->getObjectClass($data));
52
        } catch (ResourceClassNotFoundException $e) {
53
            return false;
54
        }
55
56
        if (null !== $operationName = $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
57
            return false !== $resourceMetadata->getTypedOperationAttribute(
58
                $context['collection_operation_name'] ?? false ? OperationType::COLLECTION : OperationType::ITEM,
59
                $operationName,
60
                'messenger',
61
                false,
62
                true
63
            );
64
        }
65
66
        if (isset($context['graphql_operation_name'])) {
67
            return false !== $resourceMetadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', false, true);
68
        }
69
70
        return false !== $resourceMetadata->getAttribute('messenger', false);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function persist($data, array $context = [])
77
    {
78
        $envelope = $this->messageBus->dispatch($data);
79
        if (null === $stamp = $envelope->last(HandledStamp::class)) {
80
            return $data;
81
        }
82
83
        return $stamp->getResult();
0 ignored issues
show
Bug introduced by
The method getResult() does not exist on Symfony\Component\Messenger\Stamp\StampInterface. It seems like you code against a sub-type of Symfony\Component\Messenger\Stamp\StampInterface such as Symfony\Component\Messenger\Stamp\HandledStamp. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        return $stamp->/** @scrutinizer ignore-call */ getResult();
Loading history...
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function remove($data, array $context = [])
90
    {
91
        $this->messageBus->dispatch(new Envelope($data, new RemoveStamp()));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function removeElementFromCollection($data, array $context = [])
98
    {
99
        $this->messageBus->dispatch(new Envelope($data, new RemoveStamp()));
100
    }
101
}
102