Passed
Pull Request — main (#152)
by Daniel
06:28
created

PublishableAwareHub::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 0
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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\Exception\ItemNotFoundException;
18
use Silverback\ApiComponentsBundle\Helper\Publishable\PublishableStatusChecker;
19
use Symfony\Component\Mercure\HubInterface;
20
use Symfony\Component\Mercure\Jwt\TokenFactoryInterface;
21
use Symfony\Component\Mercure\Jwt\TokenProviderInterface;
22
use Symfony\Component\Mercure\Update;
23
24
class PublishableAwareHub implements HubInterface
25
{
26
    public function __construct(private HubInterface $decorated, private PublishableStatusChecker $publishableStatusChecker, private IriConverterInterface $iriConverter)
27
    {
28
    }
29
30
    public function getUrl(): string
31
    {
32
        return $this->decorated->getUrl();
33
    }
34
35
    public function getPublicUrl(): string
36
    {
37
        return $this->decorated->getPublicUrl();
38
    }
39
40
    public function getProvider(): TokenProviderInterface
41
    {
42
        return $this->decorated->getProvider();
43
    }
44
45
    public function getFactory(): ?TokenFactoryInterface
46
    {
47
        return $this->decorated->getFactory();
48
    }
49
50
    public function publish(Update $update): string
51
    {
52
        if ($update->getData() && $data = json_decode($update->getData(), associative: true)) {
53
            try {
54
                $resource = $this->iriConverter->getResourceFromIri($data['@id']);
55
            } catch (ItemNotFoundException $e) {
56
                return $this->decorated->publish($update);
57
            }
58
59
            if ($this->publishableStatusChecker->getAnnotationReader()->isConfigured($resource) && !$this->publishableStatusChecker->isActivePublishedAt($resource)) {
60
                $update = new Update(topics: $update->getTopics(), data: $update->getData(), private: true, id: $update->getId(), type: $update->getType(), retry: $update->getRetry());
61
            }
62
        }
63
64
        return $this->decorated->publish($update);
65
    }
66
}
67