Passed
Push — master ( 3e12b8...378904 )
by Kévin
03:06
created

DataPersister::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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\DataPersister\DataPersisterInterface;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use ApiPlatform\Core\Util\ClassInfoTrait;
19
use Symfony\Component\Messenger\Envelope;
20
use Symfony\Component\Messenger\MessageBusInterface;
21
22
/**
23
 * Dispatches the given resource using the message bus of Symfony Messenger.
24
 *
25
 * @experimental
26
 *
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
final class DataPersister implements DataPersisterInterface
30
{
31
    use ClassInfoTrait;
32
33
    private $resourceMetadataFactory;
34
    private $messageBus;
35
36
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus)
37
    {
38
        $this->resourceMetadataFactory = $resourceMetadataFactory;
39
        $this->messageBus = $messageBus;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function supports($data): bool
46
    {
47
        return true === $this->resourceMetadataFactory->create($this->getObjectClass($data))->getAttribute('messenger');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function persist($data)
54
    {
55
        $this->messageBus->dispatch($data);
56
57
        return $data;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function remove($data)
64
    {
65
        $this->messageBus->dispatch(new Envelope($data, new RemoveStamp()));
66
    }
67
}
68