/
MercureProcessor.php
69 lines (61 loc) · 2.49 KB
/
MercureProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace App\State\Processor;
use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\State\ProcessorInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Mercure\Exception\InvalidArgumentException;
use Symfony\Component\Mercure\Exception\RuntimeException;
use Symfony\Component\Mercure\HubRegistry;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Serializer\SerializerInterface;
/**
* @implements ProcessorInterface<object, object>
*/
final readonly class MercureProcessor implements ProcessorInterface
{
public const DATA = 'mercure_data';
public function __construct(
private SerializerInterface $serializer,
private HubRegistry $hubRegistry,
private IriConverterInterface $iriConverter,
private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
#[Autowire('%api_platform.formats%')]
private array $formats
) {
}
/**
* @param array{item_uri_template?: string, topics?: array, mercure_data?: string} $context
*
* @throws InvalidArgumentException
* @throws ResourceClassNotFoundException
* @throws RuntimeException
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
if (isset($context['item_uri_template'])) {
$operation = $this->resourceMetadataCollectionFactory->create($data::class)->getOperation($context['item_uri_template']);
}
if (!isset($context['topics'])) {
$context['topics'] = [$this->iriConverter->getIriFromResource($data, UrlGeneratorInterface::ABS_URL, $operation)];
}
if (!isset($context[self::DATA])) {
$context[self::DATA] = $this->serializer->serialize(
$data,
key($this->formats),
($operation->getNormalizationContext() ?? []) + (isset($context['item_uri_template']) ? [
'item_uri_template' => $context['item_uri_template'],
] : [])
);
}
$this->hubRegistry->getHub()->publish(new Update(
topics: $context['topics'],
data: $context[self::DATA]
));
return $data;
}
}