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(); |
|
|
|
|
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
|
|
|
|