Test Failed
Pull Request — main (#152)
by Daniel
04:00
created

MercureResourcePublisher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 27
rs 10
c 2
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A collectResource() 0 2 1
A propagate() 0 2 1
A collectItems() 0 2 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[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 Silverback\ApiComponentsBundle\Mercure;
15
16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\UrlGeneratorInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Util\ResourceClassInfoTrait;
20
use Silverback\ApiComponentsBundle\HttpCache\ResourceChangedPropagatorInterface;
21
use Symfony\Component\Mercure\HubInterface;
22
use Symfony\Component\Mercure\Update;
23
use Symfony\Component\Serializer\SerializerAwareInterface;
24
use Symfony\Component\Serializer\SerializerAwareTrait;
25
26
class MercureResourcePublisher implements SerializerAwareInterface, ResourceChangedPropagatorInterface
27
{
28
    use ResourceClassInfoTrait;
29
    use SerializerAwareTrait;
30
31
    // Do we want MessageBusInterface instead ? we don't have messenger installed yet, probably just use the default hub for now
32
    public function __construct(
33
        private readonly HubInterface $hub,
34
        private readonly IriConverterInterface $iriConverter,
35
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory
36
    ) {
37
    }
38
39
    public function collectResource($entity): void
40
    {
41
        // this is not needed for Mercure.
42
        // this clears cache for endpoints getting collections etc.
43
        // Mercure will only update for individual items
44
    }
45
46
    public function collectItems($items): void
47
    {
48
        // TODO: Implement collectItems() method.
49
    }
50
51
    public function propagate(): void
52
    {
53
        // TODO: Implement propagate() method.
54
    }
55
56
//    public function publishResourceUpdate(object $object): void
57
//    {
58
//        $resourceClass = $this->getObjectClass($object);
59
//        $operation = $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation();
60
//        $mercureOptions = $operation->getMercure() ?? [];
61
//        $context = $operation->getNormalizationContext() ?? [];
62
//
63
//        $iri = $this->iriConverter->getIriFromResource($object, UrlGeneratorInterface::ABS_URL);
64
//        $data = $this->serializer->serialize($object, 'jsonld', $context);
65
//
66
//        $this->hub->publish(new Update(data: $data, topics: [$iri], private: $mercureOptions['private'] ?? false));
67
//    }
68
}
69