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
|
|
|
|